zoukankan      html  css  js  c++  java
  • Mybatis学习--spring和Mybatis整合

    • 简介

      在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进行封装,如:获取SqlSessionFactory、SqlSession、SqlSession的关闭等,我们只需要实现具体的业务处理。另外,spring还利用其IOC将Dao或者Mapper接口的放入到容器中进行管理,更好的实现了解耦。

    • Spring和MyBatis整合

      1、整合思路:

      需要spring通过单例方式管理SqlSessionFactoryspringmybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(springmybatis整合自动完成)持久层的mapper都需要由spring进行管理。

      2、整合环境

      创建一个新的java工程,将Mybatis和Spring整合的jar包导入到工程中。

      3、sqlSessionFactory

      在applicationContext.xml配置sqlSessionFactory和数据源:

     1 <!-- 加载配置文件 -->
     2     <context:property-placeholder location="classpath:db.properties" />
     3 
     4     <!-- 数据源,使用dbcp -->
     5     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
     6         destroy-method="close">
     7         <property name="driverClassName" value="${jdbc.driver}" />
     8         <property name="url" value="${jdbc.url}" />
     9         <property name="username" value="${jdbc.username}" />
    10         <property name="password" value="${jdbc.password}" />
    11         <property name="maxActive" value="10" />
    12         <property name="maxIdle" value="5" />
    13     </bean>
    14 
    15 
    16     <!-- sqlSessinFactory -->
    17     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    18         <!-- 加载mybatis的配置文件 -->
    19         <property name="configLocation" value="mybatis/SqlMapConfig.xml" />
    20         <!-- 数据源 -->
    21         <property name="dataSource" ref="dataSource" />
    22 </bean>
    • 原始Dao开发

      1、其他都基本相同,就是Dao的实现类有所变化:

    1 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
    2     @Override
    3     public User findUserById(int id) throws Exception {
    4         SqlSession sqlSession = this.getSqlSession();//获取sqlSession
    5         User user = sqlSession.selectOne("user.findUserById", id);
    6         return user;
    7     }
    8 }

      实现类继承SqlSessionDaoSupport,这样spring就通过模板方法将 获取sqlSessionFactory、获取sqlSession、事务管理、关闭sqlSession进行了封装,我们只需要负责业务逻辑即可。

      2、配置dao

      在applicationContext.xml中配置dao

    1 <!-- 配置userDaoImpl -->
    2     <bean id="userDao" class="com.luchao.mybatis.first.daoimpl.UserDaoImpl">
    3         <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    4     </bean>

      3、测试程序

     1 public class MyBatis_mapper_test {
     2     private ApplicationContext applicationContext;
     3     @Before
     4     public void init() throws IOException {
     5         this.applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
     6     }
     7     @Test
     8     public void testFindUserById() throws Exception {
     9         // 创建UserMapper对象
    10         UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
    11         // 调用userMapper的方法
    12         User user = userMapper.findUserById(10);
    13         // 打印客户信息
    14         System.out.println(user);
    15     }
    16 }
    • Mapper代理开发

      1、在Spring中配置Mapper接口

      (1)、使用org.mybatis.spring.mapper.MapperFactoryBean,根据mapper接口生成代理对象

    1 <bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
    2     <property name="mapperInterface" value="mapper接口地址"/>
    3   <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    4 </bean>

      需要针对每个mapper进行配置,比较麻烦。

      (2)、通过MapperScannerConfigurer进行mapper扫描

    1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    2  <property name="basePackage" value="mapper接口包地址"></property>
    3 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    4 </bean>

      basePackage:扫描包路径,中间可以用逗号或分号分隔定义多个包。

      这种方式mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录。如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在sqlMapConfig.xml中进行配置。

      可以看出将Mybatis整合到Spring中,可以减少模板代码的书写,并且将Dao和Mapper通过配置放入到Spring容器中,可以实现代码解耦。

  • 相关阅读:
    最大熵模型
    python安装深度学习包theano,Pylearn2,scikit-neuralnetwork
    sklearn之learningcurve
    sklearn之validationcurve
    SVM
    一年7篇CVPR和4篇ICCV——女神
    CUDA大作业_进行图像特征匹配V2.0
    CUDA大作业_进行图像特征匹配V1.0
    从K近邻算法、距离度量谈到KD树、SIFT+BBF算法
    Ubuntu 开机出现 grub rescue> 终端模式修复方法
  • 原文地址:https://www.cnblogs.com/lcngu/p/5487658.html
Copyright © 2011-2022 走看看