• mybatis和spring整合

    • 需要spring通过单例方式管理SqlSessionFactory
    • spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession
    • 持久层的mapper都需要由spring进行管理
    • 需要导入的jar包
    • 需要建立的文件

    • 需要建立的包
    • 在applicationContext.xml配置sqlSessionFactory
      • 创建applicationContext.xml

      • 配置applicationContext.xml
        • <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 -->
          <context:property-placeholder location="classpath:db.properties"/>
          <!-- 数据源,使用dbcp -->
          <!--
          driverClassName:
          url:
          username:
          password:
          maxActive:
          maxIdle:
          -->
          <bean id="dataSource" class="org.apache.commons.dbcp2.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>
          <!-- 配置SqlSessionFactory -->
          <!--
          class:设置为mybatis-spring.jar中的类
          id:唯一表示标识
          -->
          <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 加载mybatis的配置文件 -->
          <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
          <!-- 加载数据源 -->
          <property name="dataSource" ref="dataSource"/> </bean>
          </beans>
    •  整合代码及其bug
      • 若报找不到org.springframework.dao.support.DaoSupport的类文件

        • 导入spring-tx-4.3.5.RELEASE.jar
      • 若报java.lang.ClassNotFoundException: org.apache.commons.pool2.PooledObjectFactory

        • 导入commons-pool2-2.6.1.jar
      • 若报java.lang.NoClassDefFoundError: org/springframework/aop/support/AopUtils

        • 导入spring-aop-4.3.5.RELEASE.jar
      • 若报nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
        • 导入spring-jdbc-4.3.5.RELEASE.jar
        • 原生dao
          /**
          * dao接口实现类需要注入SqlSessionFactory,通过spring进行注入
          * 这里通过spring声明配置方式,配置dao的bean
          */
          public interface UserDao { public abstract List<User> selectAllUser(); } /**
          * UserDao的实现类
          */
          public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override
          public List<User> selectAllUser() {
          //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSession
          SqlSession connection = this.getSqlSession();
          List<User> users = connection.selectList("user.selectAllUser"); //方法结束就自动关闭,所以不需要close();
          //connection.close();
          return users;
          }
          } <!-- 在applicationContext.xml配置 --> <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 -->
          <context:property-placeholder location="classpath:db.properties"/>
          <!-- 数据源,使用dbcp -->
          <!--
          driverClassName:
          url:
          username:
          password:
          maxActive:
          maxIdle:
          -->
          <bean id="dataSource" class="org.apache.commons.dbcp2.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>
          <!-- 配置SqlSessionFactory -->
          <!--
          class:设置为mybatis-spring.jar中的类
          id:唯一表示标识
          -->
          <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 加载mybatis的配置文件 -->
          <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
          <!-- 加载数据源 -->
          <property name="dataSource" ref="dataSource"/> </bean> <!-- 原始dao接口 -->
          <bean id="userDao" class="cn.muriel.ssm.dao.impl.UserDaoImpl">
          <property name="sqlSessionFactory" ref="SqlSessionFactory"/>
          </bean> </beans> <!-- 在userMapper.xml配置 -->   <?xml version="1.0" encoding="UTF-8" ?>
            <!-- 此时您可能想知道SqlSession或Mapper类究竟执行了什么。
             MyBatis提供的全套功能可以通过使用多年来MyBatis流行的基于XML的映射语言来实现。
             如果您以前使用过MyBatis,那么您将很熟悉这个概念,但是对XML映射文档进行了大量改进,以后会很清楚。 -->
            <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
            <!-- namespace命名空间。作用对sql进行分类化管理,理解sql隔离
          注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
              <mapper namespace="cn.muriel.ssm.dao.UserDao">      <select id="selectAllUser" resultType="cn.muriel.mybatis.po.User"> select * from user   </select>
            </mapper> <!-- 测试 -->
          public class TestUtil { private static ApplicationContext applicationContext; //在setUp这个方法得到spring容器
          public static void main(String[] args) { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao");
                  
          JSONArray sendJson = JSONArray.fromObject(userDao.selectAllUser());
          System.out.println(sendJson + ""); }
          }
        • 代理mapper
          /**
          * dao接口实现类需要注入SqlSessionFactory,通过spring进行注入
          * 这里通过spring声明配置方式,配置dao的bean
          */
          public interface UserMapper { public abstract List<User> selectAllUser(); } /**
          * UserDao的实现类
          */
          public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper { @Override
          public List<User> selectAllUser() {
          //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSession
          SqlSession connection = this.getSqlSession();
          List<User> users = connection.selectList("user.selectAllUser"); //方法结束就自动关闭,所以不需要close();
          //connection.close();
          return users;
          }
          } <!-- 在applicationContext.xml配置 --> <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 -->
          <context:property-placeholder location="classpath:db.properties"/>
          <!-- 数据源,使用dbcp -->
          <!--
          driverClassName:
          url:
          username:
          password:
          maxActive:
          maxIdle:
          -->
          <bean id="dataSource" class="org.apache.commons.dbcp2.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>
          <!-- 配置SqlSessionFactory -->
          <!--
          class:设置为mybatis-spring.jar中的类
          id:唯一表示标识
          -->
          <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 加载mybatis的配置文件 -->
          <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
          <!-- 加载数据源 -->
          <property name="dataSource" ref="dataSource"/> </bean>    <!-- mapper配置 -->
          <!--
          class为mybatis-spring.jar下的类
          MapperFactoryBean:根据mapper接口生成代理对象
          -->
          <!-- UserMapper.java和userMapper.xml在同一个包下 -->
          <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
          <property name="mapperInterface" value="cn.muriel.ssm.mapper.UserMapper"/>
              <!-- 若ref改为value,则报ConversionNotSupportedException -->
          <property name="sqlSessionFactory" rel="SqlSessionFactory"/>
          </bean> </beans> <!-- 在userMapper.xml配置 -->   <?xml version="1.0" encoding="UTF-8" ?>
            <!-- 此时您可能想知道SqlSession或Mapper类究竟执行了什么。
             MyBatis提供的全套功能可以通过使用多年来MyBatis流行的基于XML的映射语言来实现。
             如果您以前使用过MyBatis,那么您将很熟悉这个概念,但是对XML映射文档进行了大量改进,以后会很清楚。 -->
            <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
            <!-- namespace命名空间。作用对sql进行分类化管理,理解sql隔离
          注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
              <mapper namespace="cn.muriel.ssm.mapper.UserMapper">      <select id="selectAllUser" resultType="cn.muriel.mybatis.po.User"> select * from user   </select>
            </mapper> <!-- 测试 -->
          public class TestUtil { private static ApplicationContext applicationContext; //在setUp这个方法得到spring容器
          public static void main(String[] args) { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
                  
          JSONArray sendJson = JSONArray.fromObject(userMapper.selectAllUser());
          System.out.println(sendJson + ""); }
          }
        • mapper的扫描配置
          /**
          * dao接口实现类需要注入SqlSessionFactory,通过spring进行注入
          * 这里通过spring声明配置方式,配置dao的bean
          */
          public interface UserMapper { public abstract List<User> selectAllUser(); } /**
          * UserDao的实现类
          */
          public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper { @Override
          public List<User> selectAllUser() {
          //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSession
          SqlSession connection = this.getSqlSession();
          List<User> users = connection.selectList("user.selectAllUser"); //方法结束就自动关闭,所以不需要close();
          //connection.close();
          return users;
          }
          } <!-- 在applicationContext.xml配置 --> <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 -->
          <context:property-placeholder location="classpath:db.properties"/>
          <!-- 数据源,使用dbcp -->
          <!--
          driverClassName:
          url:
          username:
          password:
          maxActive:
          maxIdle:
          -->
          <bean id="dataSource" class="org.apache.commons.dbcp2.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>
          <!-- 配置SqlSessionFactory -->
          <!--
          class:设置为mybatis-spring.jar中的类
          id:唯一表示标识
          -->
          <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 加载mybatis的配置文件 -->
          <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
          <!-- 加载数据源 -->
          <property name="dataSource" ref="dataSource"/> </bean>    <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创 建代理对象并且在spring容器中注册 -->
          <!-- 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) -->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <!--
          指定扫描的包名
          指定mapper接口的包名,mybatis自动扫描包下边所以mapper接口进行加载
          遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中
          如果扫描多个包,每个包中间使用逗号分隔
          -->
          <property name="basePackage" value="cn.muriel.ssm.mapper"/>
          <!-- 数据源 -->
          <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
          </bean> </beans> <!-- 在userMapper.xml配置 -->   <?xml version="1.0" encoding="UTF-8" ?>
            <!-- 此时您可能想知道SqlSession或Mapper类究竟执行了什么。
             MyBatis提供的全套功能可以通过使用多年来MyBatis流行的基于XML的映射语言来实现。
             如果您以前使用过MyBatis,那么您将很熟悉这个概念,但是对XML映射文档进行了大量改进,以后会很清楚。 -->
            <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
            <!-- namespace命名空间。作用对sql进行分类化管理,理解sql隔离
          注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->
              <mapper namespace="cn.muriel.ssm.mapper.UserMapper">      <select id="selectAllUser" resultType="cn.muriel.mybatis.po.User"> select * from user   </select>
            </mapper> <!-- 测试 -->
          public class TestUtil { private static ApplicationContext applicationContext; //在setUp这个方法得到spring容器
          public static void main(String[] args) { applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
                  
          JSONArray sendJson = JSONArray.fromObject(userMapper.selectAllUser());
          System.out.println(sendJson + ""); }
          }

    

最新文章

  1. Tomcat server.xml配置示例
  2. [转]spring beans.xml
  3. sublimetext3官网安装
  4. 20155215 2016-2017-2 《Java程序设计》第5周学习总结
  5. 解密Lazy&lt;T&gt;
  6. iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD
  7. HTML/CSS 常用单词整理
  8. [BJOI2019] 删数
  9. 我所知道的JavaScript中判断数据类型
  10. Java线程小刀牛试
  11. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 客户常用问题回答
  12. windows下安装Mysql—图文详解
  13. Spring的Java配置方式—@Configuration和@Bean实现Java配置
  14. centos7发行版号对应基于RHEL Source(版本)对照表
  15. godaddy.com 都转到 www.dnspod.cn
  16. update moudle 调用方式
  17. Java重写toString和泛型的使用
  18. Android UiAutomator UiDevice API
  19. PHP搞笑注释代码-佛祖配美女
  20. 定时模块app_timer用法及常见问题—nRF5 SDK模块系列二

热门文章

  1. WS_窗口风格常量
  2. SQL DISTINCT去掉重复的数据统计方法【转】
  3. ASP.NET Core 2.1对GDPR的支持
  4. [译]async/await中阻塞死锁
  5. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree
  6. [Swift]LeetCode992. K 个不同整数的子数组 | Subarrays with K Different Integers
  7. mysql+postgresql备份与恢复
  8. Firefox 多行标签的解决方案分享
  9. python glob的安装和使用
  10. mysql主从集群配置