【SSM-MyBatis框架】Spring整合Mybatis

来源:互联网 发布:启凡网络 编辑:程序博客网 时间:2024/05/19 20:39

1. 整合思路:

    需要spring通过单例管理mybatis的SQLSessionFactory。

   spring和mybatis整合生成代理对象,使用SQLSessionFactory创建sqlSession,(spring和mybatis整合自动完成。)。

   持久层的mapper都需要spring管理。

2.sqlSessionFactory:

   使用mybatis管理sqlSessionFactory:

   在applicationContext.xml中配置sqlSessionFactory和数据源(原先sqlMapConfig.xml中数据库部分可去除)。

  

<!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池 --><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="10"/><property name="maxIdle" value="5"/></bean><!-- mapper配置 --><!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml" /></bean>


  3.mybatis配置文件:(SQLMapConfig.xml)

    

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 批量设置(推荐使用)指定包名,mybatis自动扫描扫描po类,自动定义别名。默认为类名(首字母大写或小写) --><typeAliases><package name="cn.edu.hpu.ssm.po"/></typeAliases><mappers><!-- 批量扫描 --><package name="cn.edu.hpu.ssm.mapper"/></mappers></configuration>


    4.Spring配置文件:(applicationConfig.xml)

 

<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 "><!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池 --><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="10"/><property name="maxIdle" value="5"/></bean><!-- mapper配置 --><!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml" /></bean><!-- 单个mapper代理 --><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface"  value="cn.edu.hpu.ssm.mapper.UserMapper"/><property name="sqlSessionFactory"  ref="sqlSessionFactory"/></bean><!-- 批量代理,代理出来的mapper对象,引用时为mapper接口名称,首字母必须小写 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.edu.hpu.ssm.mapper"></property><property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory" /></bean></beans>

     5.Mapper代理(使用MapperFactoryBean):

       使用MapperFactoryBean实现单个Mapper的注入:

       mapper.java:

       

public interface UserMapper {//返回值类型必须与mapper.xml中resultType类型相同//形参必须与mapper.xml中的parameterType类型相同//mapper.java接口中的方法名和mapper.xml中statement的id一致public User findUserById(int id) throws Exception;}

     mapper.xml:

    

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace:命名空间,作用就是对sql进行分类化管理,sql隔离注意:在使用mapper的代理方法开发时,有重要作用  namespace命名必须与Mapper接口的路径相同 --><mapper namespace="cn.edu.hpu.ssm.mapper.UserMapper"><!-- 按照id查询 --><!-- 将sql语句封装到MapperStatement中,所以也将id 成为statement的Id --><!-- paramerType:指定输入参数的类型。#{ } :代表占位符。#{id }:表示接受输入参数id的值,如果输入参数是简单类型,#{ }中的参数名可以任意 ,可以是value或是其他值 resultType:表示sql输出结果的所映射的Javabean的对象类型,resultType指定将单条记录映射成Java对像--><select id="findUserById" parameterType="int"  resultType="user">SELECT * FROM USER WHERE id = #{value}</select></mapper>


     test.java:

    单元测试,setUp方法执行在测试方法之前,这里手动将application.xml文件注解。

    

public class UserMapperTest {private ApplicationContext applicationContext;@Beforepublic void setUp() throws Exception {applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");}@Testpublic void testFindUserById() throws Exception {UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");User u =userMapper.findUserById(1);System.out.println(u);}}


   6.Mapper代理(使用MapperScannerConfigurer 批量处理mapper接口):

      使用MapperScannerConfigurer必须满足其特定的要求,即:mapper.java与mapper.xml必须放在同一目录,mapper.java接口中的方法名必须与mapper.xml中的statement的ID相同。mapper.java在注解工程中,使用首字母小写。

      其他同上类似。



1 0
原创粉丝点击