码迷,mamicode.com
首页 > 编程语言 > 详细

04_SSM框架整合(Spring+SpringMVC+MyBatis)

时间:2016-09-10 17:51:55      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

【SSM的系统架构】

技术分享

【整合概述】

第一步:

  MyBatis和Spring整合,通过Spring管理mapper接口。

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

第二步:

  通过Spring管理Service接口。

  使用配置方式将Service接口配置在Spring配置文件中。

  实现事务控制。

第三步:

  由于SpringMVC是Spring的模块,无需整合这两个。

 

【工程截图】

技术分享

【数据库的items】

[ 表结构 ]

技术分享

 

[ 表内数据 ]

技术分享

 

【1.整合dao】
将Mybatis和Spring进行整合

【1.1 sqlMapConfig.xml】MyBatis的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 全局setting配置 ,根据需要再添加-->

    <!-- 配置别名 -->
    <typeAliases>
        <!-- 批量扫描别名 -->
        <package name="cn.Higgin.ssm.po"/>
    </typeAliases>

    <!-- 配置mapper
     由于使用spring和mybatis的整合包进行整合,这了无需配置
     但必须遵循:mapper.xml和mapper.xml文件同名且在同一目录-->
    <!--<mappers></mappers>  -->
</configuration>

【1.2 db.properties】数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=

【1.3 applicationContext-dao.xml】

需要配置:数据源、SqlSessionFactory、mapper扫描器

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源,dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="5"/>
    </bean>

    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    </bean>
    
    <!-- mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
        <property name="basePackage" value="cn.Higgin.ssm.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
</beans>

【1.4 逆向工程生成PO类以及mapper】(对应表的增删改查)

技术分享

【1.5 手动定义商品查询mapper】

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

【1.5.1 ItemsMapperCustom.java】

package cn.Higgin.ssm.po;

/**
 *  商品信息的扩展
 */
public class ItemsCustom extends Items {
    
    //添加商品信息扩展的属性

}

【1.5.2 ItemsMapperCustom.xml】

<?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="cn.Higgin.ssm.mapper.ItemsMapperCustom" >

   <!-- 定义商品查询的sql片段,就是商品查询条件 -->
   <sql id="query_items_where">
       <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
       <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->
           <if test="itemsCustom!=null">
               <if test="itemsCustom.name!=null and itemsCustom.name!=‘‘">
                   items.name LIKE ‘%${itemsCustom.name}%‘
               </if>
           </if>
   </sql>
      
      <!-- 商品列表查询 -->
      <!-- parameterType:传入包装对象(包装了查询条件)
          resultType:建议使用扩展对象
       -->
      <select id="findItemsList" parameterType="cn.Higgin.ssm.po.ItemsQueryVo"
           resultType="cn.Higgin.ssm.po.ItemsCustom">
          SELECT items.* FROM items  
          <where>
              <include refid="query_items_where"></include>
          </where>
      </select>
      
</mapper>

 

04_SSM框架整合(Spring+SpringMVC+MyBatis)

标签:

原文地址:http://www.cnblogs.com/HigginCui/p/5859788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!