Mybatis学习笔记(十四)——mybatis与spring整合

本博客源码下载:戳我一下

Mybatis学习笔记汇总:戳我一下

一、与spring整合

实现mybatisspring进行整合,通过spring管理SqlSessionFactorymapper接口。

二、导入jar包

mybatis官方提供的mybatisspring整合jar包:
这里写图片描述
还包括其它jar包:

  • spring3.2.0
  • mybatis3.2.7
  • dbcp连接池
  • 数据库驱动

三、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>


    <!-- 别名的定义 -->
    <typeAliases>
        <package name="com.jiayifan.ssm.po"/>
    </typeAliases>

    <!-- 加载映射文件 -->
    <mappers>
        <!-- 通过resource方法一次加载一个映射文件 -->
        <mapper resource="sqlmap/User.xml"/>
        <!-- 批量加载 
            指定mapper接口的包名,mybatis自动的扫描包下的所有mapper接口
            并加载,但是必须遵循 “通过mapper接口加载单个映射文件”的规范
        -->
        <package name="com.jiayifan.ssm.mapper"/>
    </mappers>
</configuration>

四、Spring配置文件:

classpath下创建spring/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> 
    <!-- mapper配置 -->
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="mybatis/SqlMapConfig.xml" />

        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

五、原始DAO开发

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">
<!-- namespace命名空间,作用是对sql进行分类化管理
注意:如果使用mapper代理方法开发,namespace有特殊的作用
 -->
<mapper namespace="test">

    <select id="findUserById" parameterType="int"
     resultType="com.jiayifan.ssm.po.User">
        SELECT * FROM USER WHERE id=#{id}
    </select>
</mapper>

2、编写DAO和DAO实现类(实现类继承SqlSessionDaoSupport)
DAO接口:

package com.jiayifan.ssm.dao;

import com.jiayifan.ssm.po.User;

/**
 * dao接口,用户管理
 * @author 贾一帆
 *
 */
public interface UserDao {
    //根据ID查询用户信息
    public User findUserById(int id) throws Exception;
}

DAO实现类:

package com.jiayifan.ssm.dao;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.jiayifan.ssm.po.User;

/**
 * dao接口实现类
 * @author 贾一帆
 *
 */
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",id);
        return user;
    }
}

3、配置DAO
applicationContext.xml中配置dao。

<bean id="userDao" class="com.jiayifan.ssm.dao.UserDaoImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

4、测试程序

package com.jiayifan.ssm.dao;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jiayifan.ssm.po.User;

public class UserDaoImplTest {
    private ApplicationContext applicationContext;
    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

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

}

五、Mapper代理开发DAO(使用MapperFactoryBean创建代理对象)

1、UserMapper接口

package com.jiayifan.ssm.mapper;

import java.util.List;

import com.jiayifan.ssm.po.User;

/**
 * mapper接口
 * @author 贾一帆
 *
 */
public interface UserMapper {
    //根据ID查询用户信息
    public User findUserById(int id) throws Exception;

}

2、UserMapper配置文件

<?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">
<!-- namespace命名空间,作用是对sql进行分类化管理
注意:如果使用mapper代理方法开发,namespace有特殊的作用,namespace等于mapper接口地址
 -->
<mapper namespace="com.jiayifan.ssm.mapper.UserMapper">

    <select id="findUserById" parameterType="int"
     resultType="com.jiayifan.ssm.po.User">
        SELECT * FROM USER WHERE id=#{id}
    </select>

</mapper>

3、使用Spring创建Mapper对象

<!-- mapper配置
    MapperFactoryBean:可以根据mapper接口生成代理对象
     -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- mapperInterface:指定mapper接口 -->
        <property name="mapperInterface" value="com.jiayifan.ssm.mapper.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

4、测试代码`

    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

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

5、此方法的问题
需要针对每个mapper进行配置,麻烦。

六、Mapper代理开发DAO(使用MapperScannerConfigurer进行mapper扫描)

这里只是把applicationContext.xml配置文件修改一下

    <!-- mapper配置
    MapperFactoryBean:可以根据mapper接口生成代理对象
     -->
    <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        mapperInterface:指定mapper接口
        <property name="mapperInterface" value="com.jiayifan.ssm.mapper.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean> -->
    <!-- mapper的批量扫描 
    从mapper的包中扫描出mapper接口,自动的创建代理对象并在
    spring容器中注册
    遵循一些规范:需要将mapper接口的类名和mapper.xml映射文件名保持一致
           且在一个目录中。
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 
        指定扫描的包名
        如果扫描多个包,每个包名中间用半角逗号隔开
         -->
        <property name="basePackage" value="com.jiayifan.ssm.mapper"></property>

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

测试代码不变,注意如果你使用的jdk是jdk1.8版本,而spring是3.0版本,会出现下面这样的bug

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [I:\java\MybatisAndSpring\bin\com\jiayifan\ssm\mapper\UserMapper.class]; nested exception is java.lang.IllegalArgumentException
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:281)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
    at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:155)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:220)
    at org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:315)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.jiayifan.ssm.mapper.UserMapperTest.setUp(UserMapperTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:257)
    ... 32 more

这里只需要把jdk降为1.7版本就可以了,或者把spring升级一下。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页