赞
踩
链接:https://pan.baidu.com/s/1-9fLmKHZw3gilF6bv9ohxQ
提取码:3cds
整合SSM的jar包也在里面
首先准备数据库 ssmdb
如果没有安装数据库,请安装mysql-server
创建数据库
create database ssmdb;
接着准备表Category只有2个字段id和name
use ssmdb;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
use ssmdb;
insert into category_ values(null,"category1");
insert into category_ values(null,"category2");
insert into category_ values(null,"category3");
insert into category_ values(null,"category4");
insert into category_ values(null,"category5");
select * from category_
在eclipse中新建项目ssm_conformity,使用dynamic web project的方式。
附带的源码里有这些jar包, 解压后复制到 己项目的lib目录下就行
package com.lqg.pojo;
public class Category {
private int id;
private String name;
//省略getter和setter
}
package com.lqg.mapper; import java.util.List; import com.how2java.pojo.Category; public interface CategoryMapper { public int add(Category category); public void delete(int id); public Category get(int id); public int update(Category category); public List<Category> list(); public int count(); } 步骤 11 : Category.xml
Category.xml需要和CategoryMapper放在同一个包下面,并且namespace必须写CategoryMapper的完整类名
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lqg.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
package com.lqg.service;
import java.util.List;
import com.how2java.pojo.Category;
public interface CategoryService {
List<Category> list();
}
CategoryServiceImpl被注解**@Service**标示为一个Service
并且装配了categoryMapper
package com.lqg.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.lqg.mapper.CategoryMapper; import com.lqg.pojo.Category; import com.lqg.service.CategoryService; @Service public class CategoryServiceImpl implements CategoryService{ @Autowired CategoryMapper categoryMapper; public List<Category> list(){ return categoryMapper.list(); } }
CategoryController被@Controller标示为了控制器
自动装配了categoryService
通过@RequestMapping映射访问路径/listCategory路径到方法listCategory()。
在listCategory()方法中,通过categoryService获取后,然后存放在"cs"这个key上。
package com.how2java.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.lqg.pojo.Category; import com.lqg.service.CategoryService; // 告诉spring mvc这是一个控制器类 @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(){ ModelAndView mav = new ModelAndView(); List<Category> cs= categoryService.list(); // 放入转发参数 mav.addObject("cs", cs); // 放入jsp路径 mav.setViewName("listCategory"); return mav; } }
在WEB-INF目录下新增加web.xml,这个web.xml有两个作用:
通过ContextLoaderListener在web app启动的时候,获取contextConfigLocation配置文件的文件名applicationContext.xml,并进行Spring相关初始化工作
有任何访问,都被DispatcherServlet所拦截,这就是Spring MVC那套工作机制了。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc核心:分发servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在src目录下新建applicationContext.xml文件,这是Spring的配置文件,其作用
<context:annotation-config />
<context:component-scan base-package="com.how2java.service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
5.完整的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com.how2java.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/ssmdb?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.lqg.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/lqg/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lqg.mapper"/> </bean> </beans>
在src目录下新建springMVC.xml
<context:annotation-config/>
<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
详细的SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:annotation-config/> <context:component-scan base-package="com.lqg.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <!-- 视图定位 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
在WEB-INF下创建jsp目录,并创建文件listCategory.jsp。
在这个jsp文件中,通过forEach标签,遍历CategoryController传递过来的集合数据。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table>
部署在Tomcat中,重启tomcat,然后访问地址,观察效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。