Spring08:整合MyBatis

整合MyBatis

步骤
1、导入相关jar包
junit
   junit    junit    4.12
mybatis
   org.mybatis    mybatis    3.5.2
mysql-connector-java
   mysql    mysql-connector-java    5.1.47
spring相关
   org.springframework    spring-webmvc    5.1.10.RELEASE    org.springframework    spring-jdbc    5.1.10.RELEASE
aspectJ AOP 织入器
   org.aspectj    aspectjweaver    1.9.4
mybatis-spring整合包 【重点】
   org.mybatis    mybatis-spring    2.0.2
配置Maven静态资源过滤问题!
                       src/main/java                            **/*.properties                **/*.xml                        true            
2、编写配置文件
3、代码实现
回忆MyBatis
编写pojo实体类
package com.kuang.pojo;
public class User {    private int id;  //id    private String name;   //姓名    private String pwd;   //密码 }
实现mybatis的配置文件
           [InvalidCharacterError: "PACKAGE NAME="COM.KUANG.POJO"" did not match the Name production]
   [InvalidCharacterError: "ENVIRONMENTS DEFAULT="DEVELOPMENT"" did not match the Name production]
           [InvalidCharacterError: "PACKAGE NAME="COM.KUANG.DAO"" did not match the Name production]
UserDao接口编写
public interface UserMapper {    public List selectUser(); }
接口对应的Mapper映射文件
[InvalidCharacterError: "MAPPER NAMESPACE="COM.KUANG.DAO.USERMAPPER"" did not match the Name production]
   [InvalidCharacterError: "SELECT ID="SELECTUSER" RESULTTYPE="USER"" did not match the Name production]
测试类
@Test public void selectUser() throws IOException {
   String resource = "mybatis-config.xml";    InputStream inputStream = Resources.getResourceAsStream(resource);    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    SqlSession sqlSession = sqlSessionFactory.openSession();
   UserMapper mapper = sqlSession.getMapper(UserMapper.class);
   List userList = mapper.selectUser();    for (User user: userList){        System.out.println(user);   }
   sqlSession.close(); }
MyBatis-Spring学习
引入Spring之前需要了解mybatis-spring包中的一些重要类;
http://www.mybatis.org/spring/zh/index.html
什么是 MyBatis-Spring?
MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。
知识基础
在开始使用 MyBatis-Spring 之前,你需要先熟悉 Spring 和 MyBatis 这两个框架和有关它们的术语。这很重要
MyBatis-Spring 需要以下版本:
MyBatis-SpringMyBatisSpring 框架Spring BatchJava2.03.5+5.0+4.0+Java 8+1.33.4+3.2.2+2.1+Java 6+
如果使用 Maven 作为构建工具,仅需要在 pom.xml 中加入以下代码即可:
   org.mybatis    mybatis-spring    2.0.2
要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。
在 MyBatis-Spring 中,可使用SqlSessionFactoryBean来创建 SqlSessionFactory。要配置这个工厂 bean,只需要把下面代码放在 Spring 的 XML 配置文件中:
[InvalidCharacterError: "BEAN ID="SQLSESSIONFACTORY" CLASS="ORG.MYBATIS.SPRING.SQLSESSIONFACTORYBEAN"" did not match the Name production]
注意:SqlSessionFactory需要一个 DataSource(数据源)。这可以是任意的 DataSource,只需要和配置其它 Spring 数据库连接一样配置它就可以了。
在基础的 MyBatis 用法中,是通过 SqlSessionFactoryBuilder 来创建 SqlSessionFactory 的。而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建。
在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession。一旦你获得一个 session 之后,你可以使用它来执行映射了的语句,提交或回滚连接,最后,当不再需要它的时候,你可以关闭 session。
SqlSessionFactory有一个唯一的必要属性:用于 JDBC 的 DataSource。这可以是任意的 DataSource 对象,它的配置方法和其它 Spring 数据库连接是一样的。
一个常用的属性是 configLocation,它用来指定 MyBatis 的 XML 配置文件路径。它在需要修改 MyBatis 的基础配置非常有用。通常,基础配置指的是 < settings> 或 < typeAliases>元素。
需要注意的是,这个配置文件并不需要是一个完整的 MyBatis 配置。确切地说,任何环境配置(),数据源()和 MyBatis 的事务管理器()都会被忽略。SqlSessionFactoryBean 会创建它自有的 MyBatis 环境配置(Environment),并按要求设置自定义环境的值。
SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession。
模板可以参与到 Spring 的事务管理中,并且由于其是线程安全的,可以供多个映射器类使用,你应该总是用 SqlSessionTemplate 来替换 MyBatis 默认的 DefaultSqlSession 实现。在同一应用程序中的不同类之间混杂使用可能会引起数据一致性的问题。
可以使用 SqlSessionFactory 作为构造方法的参数来创建 SqlSessionTemplate 对象。
[InvalidCharacterError: "BEAN ID="SQLSESSION" CLASS="ORG.MYBATIS.SPRING.SQLSESSIONTEMPLATE"" did not match the Name production]
现在,这个 bean 就可以直接注入到你的 DAO bean 中了。你需要在你的 bean 中添加一个 SqlSession 属性,就像下面这样:
public class UserDaoImpl implements UserDao {
 private SqlSession sqlSession;
 public void setSqlSession(SqlSession sqlSession) {    this.sqlSession = sqlSession; }
 public User getUser(String userId) {    return sqlSession.getMapper...; } }
按下面这样,注入 SqlSessionTemplate:
[InvalidCharacterError: "BEAN ID="USERDAO" CLASS="ORG.MYBATIS.SPRING.SAMPLE.DAO.USERDAOIMPL"" did not match the Name production]
整合实现一
1、引入Spring配置文件beans.xml
[InvalidCharacterError: "BEANS XMLNS="HTTP:" did not match the Name production]
2、配置数据源替换mybaits的数据源
[InvalidCharacterError: "BEAN ID="DATASOURCE" CLASS="ORG.SPRINGFRAMEWORK.JDBC.DATASOURCE.DRIVERMANAGERDATASOURCE"" did not match the Name production]
3、配置SqlSessionFactory,关联MyBatis
[InvalidCharacterError: "BEAN ID="SQLSESSIONFACTORY" CLASS="ORG.MYBATIS.SPRING.SQLSESSIONFACTORYBEAN"" did not match the Name production]
4、注册sqlSessionTemplate,关联sqlSessionFactory;
[InvalidCharacterError: "BEAN ID="SQLSESSION" CLASS="ORG.MYBATIS.SPRING.SQLSESSIONTEMPLATE"" did not match the Name production]
5、增加Dao接口的实现类;私有化sqlSessionTemplate
public class UserDaoImpl implements UserMapper {
   //sqlSession不用我们自己创建了,Spring来管理    private SqlSessionTemplate sqlSession;
   public void setSqlSession(SqlSessionTemplate sqlSession) {        this.sqlSession = sqlSession;   }
   public List selectUser() {        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        return mapper.selectUser();   }     }
6、注册bean实现
[InvalidCharacterError: "BEAN ID="USERDAO" CLASS="COM.KUANG.DAO.USERDAOIMPL"" did not match the Name production]
7、测试
   @Test    public void test2(){        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        UserMapper mapper = (UserMapper) context.getBean("userDao");        List user = mapper.selectUser();        System.out.println(user);   }
结果成功输出!现在我们的Mybatis配置文件的状态!发现都可以被Spring整合!
           [InvalidCharacterError: "PACKAGE NAME="COM.KUANG.POJO"" did not match the Name production]
整合实现二
mybatis-spring1.2.3版以上的才有这个 .
官方文档截图 :
dao继承Support类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate , 而且对事务的支持更加友好 . 可跟踪源码查看
测试:
1、将我们上面写的UserDaoImpl修改一下
public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {    public List selectUser() {        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);        return mapper.selectUser();   } }
2、修改bean的配置
[InvalidCharacterError: "BEAN ID="USERDAO" CLASS="COM.KUANG.DAO.USERDAOIMPL"" did not match the Name production]
3、测试
@Test public void test2(){    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");    UserMapper mapper = (UserMapper) context.getBean("userDao");    List user = mapper.selectUser();    System.out.println(user); }
总结 : 整合到spring以后可以完全不要mybatis的配置文件,除了这些方式可以实现整合之外,我们还可以使用注解来实现,这个等我们后面学习SpringBoot的时候还会测试整合!