• 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 + ""); }
          }

    

mybatis基础(下)的更多相关文章

  1. myBatis 基础测试 表关联关系配置 集合 测试

    myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...

  2. JAVA之Mybatis基础入门--框架搭建与简单查询

    JAVA中,操作数据库有JDBC.hibernate.Mybatis等技术,今天整理了下,来讲一讲下Mybatis.也为自己整理下文档: hibernate是一个完全的ORM框架,是完全面向对象的.但 ...

  3. mybatis基础系列(四)——关联查询、延迟加载、一级缓存与二级缓存

    关本文是Mybatis基础系列的第四篇文章,点击下面链接可以查看前面的文章: mybatis基础系列(三)——动态sql mybatis基础系列(二)——基础语法.别名.输入映射.输出映射 mybat ...

  4. mybatis基础系列(三)——动态sql

    本文是Mybatis基础系列的第三篇文章,点击下面链接可以查看前面的文章: mybatis基础系列(二)--基础语法.别名.输入映射.输出映射 mybatis基础系列(一)--mybatis入门 动态 ...

  5. mybatis基础系列(二)——基础语法、别名、输入映射、输出映射

    增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...

  6. mybatis基础系列(一)——mybatis入门

    好久不发博客了,写博文的一个好处是能让心静下来,整理下之前学习过的一些知识一起分享,大神路过~ mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. ...

  7. MyBatis基础入门《十七》动态SQL

    MyBatis基础入门<十七>动态SQL 描述: >> 完成多条件查询等逻辑实现 >> 用于实现动态SQL的元素主要有: > if > trim > ...

  8. MyBatis基础入门《十六》缓存

    MyBatis基础入门<十六>缓存 >> 一级缓存 >> 二级缓存 >> MyBatis的全局cache配置 >> 在Mapper XML文 ...

  9. MyBatis基础入门《十四》ResultMap子元素(association )

    MyBatis基础入门<十四>ResultMap子元素(association ) 1. id: >> 一般对应数据库中改行的主键ID,设置此项可以提高Mybatis的性能 2 ...

  10. MyBatis基础入门《三》Select查询集合

    MyBatis基础入门<三>Select查询集合 描述: 代码新增了一个MybatisUtil工具类,查询数据库返回集合的时候,接收数据的三种方式.由于代码会渐渐增多,未涉及改动过的文件不 ...

随机推荐

  1. 对于bilibili主页head部分的代码的总结以及疑问。

    1.lang=zh-Hans: lang代表languages的意思,VSCode创建出来的网页模板是lang=en,en代表english的意思,zh代表中文,单一的zh是废弃的语法,Hans代表中 ...

  2. koa2学习(二) 中间件router

    中间件 koa-router 安装 npm install --save koa-router 使用 const Koa = require('koa'); const Router = requir ...

  3. 发现了学校教务处官网的两个BUG

    许久没有写博客了,感觉自己技术还差的好多-_-好像没啥好写的,之前学完了某易的WEB安全基础视频教程,自认对WEB安全入了门,忍不住就想拿学校教务处官网来练练手 教务处登录界面如图所示(为保护隐私,部 ...

  4. lua语言自学知识点----简单了解

    零碎知识点: lua:用lua写UI,更新UI,因为lua可直接跨平台解析,不需要编译,方便更新------>热更新. c#反射也可以达到更新,但非常麻烦,切不支持iOS. 在lua中一个人汉字 ...

  5. ABP入门系列(5)——展现层实现增删改查

    ABP入门系列目录--学习Abp框架之实操演练 这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP ...

  6. 回顾4180天在腾讯使用C#的历程,开启新的征途

    今天是2018年8月8日,已经和腾讯解除劳动关系,我的公司正式开始运营,虽然还有很多事情需要理清,公司官网也没有做,接下来什么事情都需要自己去完成了,需要一步一个脚印去完善,开启一个新的征途,我将在博 ...

  7. [Swift]LeetCode9. 回文数 | Palindrome Number

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  8. [Swift]LeetCode786. 第 K 个最小的素数分数 | K-th Smallest Prime Fraction

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  9. 8.Django缓存和信号

    缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将某个views的返回值保存至内存或者memcache中, ...

  10. Java核心技术及面试指南 多线程并发部分的面试题总结以及答案

    7.2.10.1有T1.T2.T3三个线程,如何保证T2在T1执行完后执行,T3在T2执行完后执行? 用join语句,在t3开始前join t2,在t2开始前join t1. 不过,这会破坏多线程的并 ...