java-mybaits-00701-与spring整合

1.1     整合思路
 
需要spring通过单例方式管理SqlSessionFactory。
spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
持久层的mapper都需要由spring进行管理。
 
1.2     整合环境
创建一个新的java工程(接近实际开发的工程结构)
 
jar包:
 
mybatis3.2.7的jar包
spring3.2.0的jar包
mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。
 
.3     Mybatis配置文件
在classpath下创建mybatis/SqlMapConfig.xml
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>
 
—使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers -->
<mappers>
<package name="cn.itcast.mybatis.mapper" />
mappers>
configuration> 
1.4 Spring配置文件
在classpath下创建applicationContext.xml,定义数据库链接池、SqlSessionFactory。
<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>  

   
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      
      <property name="dataSource" ref="dataSource" />
      
      <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
   bean> 
beans>
注意:在定义sqlSessionFactory时指定数据源dataSource和mybatis的配置文件。
 
 
1.4     原始dao开发(和spring整合后)
 
1.4.1     User.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="test">
    <select id="findUserById" parameterType="int" resultType="com.ds365.ssm.po.User">
        select
        * from User where
        id=#{id}
    select>
mapper>
在SqlMapconfig.xml中加载User.xml
    <mappers>
        <mapper resource="sqlMap/User.xml" />
    mappers>
1.4.2     dao(实现类继承SqlSessionDaoSupport)
public interface UserDao {
    // 获取用户
    public User findUserById(int id) throws Exception;
}
dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。
这里spring声明配置方式【当然也可以自动注入】,配置dao的bean:
 
让UserDaoImpl实现类继承SqlSessionDaoSupport
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Override
    public User findUserById(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();
        User user = sqlSession.selectOne("test.findUserById", 1);
        return user;
    }
}
  开启sqlsession,关闭sqlsession 由spring代替操作
1.4.3     配置dao
在applicationContext.xml中配置dao。
    
    <bean id="userDao" class="com.ds365.ssm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory">property>
    bean>
1.4.4     测试程序
    private ApplicationContext applicationContext;
    // 在setup中得到spring容器
    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }

    @Test
    public void testFindUserById() throws Exception {
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        User user = userDao.findUserById(1);
        System.out.println(user);
    }
1.5     mapper代理开发
 
1.5.1     mapper.xml和mapper.java
在同一包下
 public interface UserMapper {
    // 获取用户
    public User findUserById(int id) throws Exception;
}
<mapper namespace="com.ds365.ssm.mapper.UserMapper">
    <select id="findUserById" parameterType="int" resultType="com.ds365.ssm.po.User">
        select * from User where id=#{id}
    select>
mapper>
1.5.2     通过MapperFactoryBean创建代理对象
    
    
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.ds365.ssm.mapper.UserMapper">property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory">property>
    bean>
此方法问题:
需要针对每个mapper进行配置,麻烦。
 
1.5.3     使用mapper扫描器
通过MapperScannerConfigurer进行mapper扫描(建议使用)
    
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.ds365.ssm.mapper">property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
        
        
        
    bean>
这时可以去掉sqlmapconfig。xml中的mappers配置的扫描【扫描接口与xml对应的文件】
使用扫描器后从spring容器中获取mapper的实现对象
          如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在sqlMapConfig.xml中进行配置
1.5.4     测试代码
    private ApplicationContext applicationContext;

    // 在setup中得到spring容器
    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }

    @Test
    public void testFindUserById() throws Exception {
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        User user = userMapper.findUserById(1);
        System.out.println(user);
    }

常见错误

 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\WORK\CODE\Javaworkspace16\mybatis\spring_mybatis02\bin\com\ds365\ssm\mapper\UserMapper.class]; nested exception is java.lang.IllegalArgumentException
 
此错误是spring3与java8造成 更换成最新的spring 4 即可
 
 
 
 
 
 
 

转载于:https://www.cnblogs.com/bjlhx/p/6869447.html

你可能感兴趣的:(java-mybaits-00701-与spring整合)