ssm框架整合

springmvc+mybaits的系统架构:

第一步:整合dao层

         mybatis和spring整合,通过spring管理mapper接口。

         使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

第二步:整合service层

         通过spring管理 service接口。

         使用配置方式将service接口配置在spring配置文件中。

         实现事务控制。

第三步:整合springmvc

         由于springmvc是spring的模块,不需要整合。

 

 整合dao

mybatis和spring进行整合。

sqlMapConfig.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 
 6 <configuration>
 7     <!-- 全局setting配置,根据需要添加 -->
 8     
 9     <!-- 配置别名 -->
10     <typeAliases>
11         <!-- 批量扫描别名 -->
12         <package name="cn.cuibusi.ssm.po"/>
13     </typeAliases>
14 
15     <!-- 配置mapper
16     由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
17     必须遵循:mapper.xml和mapper.java文件同名且在一个目录 
18      -->
19 
20     <!-- <mappers>
21         <package name="cn.cuibusi.ssm.mapper"/>
22     </mappers> -->
23 </configuration>

applicationContext-dao.xml

配置:

数据源

SqlSessionFactory

mapper扫描器

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
16 
17     <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
18     <context:property-placeholder location="classpath:db.properties" />
19     <!-- 配置数据源 ,dbcp -->
20 
21     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
22         destroy-method="close">
23         <property name="driverClassName" value="${jdbc.driver}" />
24         <property name="url" value="${jdbc.url}" />
25         <property name="username" value="${jdbc.username}" />
26         <property name="password" value="${jdbc.password}" />
27         <property name="maxActive" value="30" />
28         <property name="maxIdle" value="5" />
29     </bean>
30     
31     <!-- sqlSessionFactory -->
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <!-- 数据库连接池 -->
34         <property name="dataSource" ref="dataSource" />
35         <!-- 加载mybatis的全局配置文件 -->
36         <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
37     </bean>
38     
39     <!-- mapper扫描器 -->
40     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
41         <property name="basePackage" value="cn.cuibusi.ssm.mapper"/>
42         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
43     </bean>
44 </beans>

逆向工程生成po类及mapper(单表增删改查)

将生成的文件拷贝至工程中。

手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义map

 ItemsMapperCustom.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="cn.cuibusi.ssm.mapper.ItemsMapperCustom" >
 4 
 5    <!-- 定义商品查询的sql片段,就是商品查询条件 -->
 6    <sql id="query_items_where">
 7        <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
 8        <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->
 9            <if test="itemsCustom!=null">
10                <if test="itemsCustom.name!=null and itemsCustom.name!=''">
11                    items.name LIKE '%${itemsCustom.name}%'
12                </if>
13            </if>
14    </sql>
15       
16       <!-- 商品列表查询 -->
17       <!-- parameterType传入包装对象(包装了查询条件)
18           resultType建议使用扩展对象
19        -->
20       <select id="findItemsList" parameterType="cn.cuibusi.ssm.po.ItemsQueryVo"
21            resultType="cn.cuibusi.ssm.po.ItemsCustom">
22           SELECT items.* FROM items  
23           <where>
24               <include refid="query_items_where"></include>
25           </where>
26       </select>
27   
28 </mapper>

ItemsMapperCustom.java

1 public interface ItemsMapperCustom {
2     
3     //商品查询列表
4     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
5 }

整合service

让spring管理service接口。

 定义service接口

public interface ItemsService {

    //商品查询列表
        public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

public class ItemsServiceImpl implements ItemsService {
    
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
        
        return itemsMapperCustom.findItemsList(itemsQueryVo);
    }

}

 在spring容器配置service(applicationContext-service.xml)

创建applicationContext-service.xml,文件中配置service。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
16 
17     <bean id="itemsService" class="cn.cuibusi.ssm.service.impl.ItemsServiceImpl"></bean>
18 </beans>

事务控制(applicationContext-transaction.xml)

在applicationContext-transaction.xml中使用spring声明式事务控制方法。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
10         http://www.springframework.org/schema/context 
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
14         http://www.springframework.org/schema/tx 
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
16 
17     <!-- 事务管理器 
18     对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
19 -->
20 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
21     <!-- 数据源
22     dataSource在applicationContext-dao.xml中配置了
23      -->
24     <property name="dataSource" ref="dataSource"/>
25 </bean>
26 
27 <!-- 通知 -->
28 <tx:advice id="txAdvice" transaction-manager="transactionManager">
29     <tx:attributes>
30         <!-- 传播行为 -->
31         <tx:method name="save*" propagation="REQUIRED"/>
32         <tx:method name="delete*" propagation="REQUIRED"/>
33         <tx:method name="insert*" propagation="REQUIRED"/>
34         <tx:method name="update*" propagation="REQUIRED"/>
35         <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
36         <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
37         <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
38     </tx:attributes>
39 </tx:advice>
40 <!-- aop -->
41 <aop:config>
42     <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.cuibusi.ssm.service.impl.*.*(..))"/>
43 </aop:config>
44 </beans>

整合springmvc

springmvc.xml

创建springmvc.xml文件,配置处理器映射器、适配器、视图解析器。

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
15 
16     <!-- 可以扫描controller、service、...
17     这里让扫描controller,指定controller的包
18      -->
19     <context:component-scan base-package="cn.cuibusi.ssm.controller"></context:component-scan>
20     
21         
22     <!--注解映射器 -->
23     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
24     <!--注解适配器 -->
25     <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
26     
27     <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
28     mvc:annotation-driven默认加载很多的参数绑定方法,
29     比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
30     实际开发时使用mvc:annotation-driven
31      -->
32     <mvc:annotation-driven></mvc:annotation-driven>
33     <!-- conversion-service="conversionService" -->
34 
35     <!-- 视图解析器
36     解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
37      -->
38     <bean
39         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
40         <!-- 配置jsp路径的前缀 -->
41         <property name="prefix" value="/WEB-INF/jsp/"/>
42         <!-- 配置jsp路径的后缀 -->
43         <property name="suffix" value=".jsp"/>
44     </bean>
45     
46     <!-- 自定义参数绑定 -->
47     <!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
48         转换器
49         <property name="converters">
50             <list>
51                 日期类型转换
52                 <bean class="cn.cuibusi.ssm.controller.converter.CustomDateConverter"/>
53             </list>
54         </property>
55     </bean> -->
56     
57 </beans>

 配置前端控制器

 编写Controller(就是Handler)

 1 @Controller
 2 public class ItemsController {
 3     @Autowired
 4     private ItemsService itemsService;
 5     
 6     //将方法和url进行映射,一个方法对应一个url
 7         @RequestMapping("/queryItems")
 8         public ModelAndView queryItems() throws Exception{
 9             //调用service查找 数据库,查询商品列表,这里使用静态数据模拟
10             List<ItemsCustom> itemsList = itemsService.findItemsList(null);
11             ModelAndView modelAndView =  new ModelAndView();
12             //相当 于request的setAttribut,在jsp页面中通过itemsList取数据
13             modelAndView.addObject("itemsList", itemsList);
14             //指定视图
15             modelAndView.setViewName("items/itemsList");
16             return modelAndView;
17         }
18 }

加载spring容器

建议使用通配符加载上边的配置文件。

在web.xml中,添加spring容器监听器,加载spring容器。

 1 <!-- 加载spring容器 -->
 2      <context-param>
 3         <param-name>contextConfigLocation</param-name>
 4         <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
 5     </context-param>
 6     <listener>
 7         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 8     </listener>
 9 
10 
11     <!-- springmvc前端控制器 -->
12     <servlet>
13         <servlet-name>springmvc</servlet-name>
14         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
15         <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
16         <init-param>
17             <param-name>contextConfigLocation</param-name>
18             <param-value>classpath:spring/springmvc.xml</param-value>
19         </init-param>
20     </servlet>
21 
22     <servlet-mapping>
23         <servlet-name>springmvc</servlet-name>
24         <!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 
25             使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 -->
26         <url-pattern>*.action</url-pattern>
27     </servlet-mapping>
28 
29     <!-- post乱码过虑器 -->
30     <filter>
31         <filter-name>CharacterEncodingFilter</filter-name>
32         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33         <init-param>
34             <param-name>encoding</param-name>
35             <param-value>utf-8</param-value>
36         </init-param>
37     </filter>
38     <filter-mapping>
39         <filter-name>CharacterEncodingFilter</filter-name>
40         <url-pattern>/*</url-pattern>
41     </filter-mapping>

 编写jsp

 1 <c:forEach items="${itemsList }" var="item">
 2 <tr>
 3     <td>${item.name }</td>
 4     <td>${item.price }</td>
 5     <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
 6     <td>${item.detail }</td>
 7     
 8     <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
 9 
10 </tr>
11 </c:forEach>

 

posted @ 2017-05-13 22:34  崔布斯  阅读(715)  评论(0编辑  收藏  举报