Spring整合Mybatis

Myabatis基础搭建

步骤:

  1. 导入相关jar包
    ① junit
    ②mybatis
    ③spring
    ④mysql
    ⑤aop
    ⑥mybatis-spring
    ⑦lombok
    ⑧资源过滤
<dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.1.10.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.1.10.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.4version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.2version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.16version>
        dependency>
    dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>
  1. 编写配置文件
  • mybatis-config
  • mybatisUtils
  • 实体类
  • 接口
  • 接口实现类
  1. 测试

Myabatis整合Spring

步骤:

  1. 编写数据配置源
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 给接口增加实现类
  5. 将实现类注入到spring中
  6. 测试

对比:

  • 可以用applicationContext.xml配置,替换掉mybatis中的mybatisUtils.class和mybatis-config.xml文件
  • 增加的实现类可以通过spring注入,拿到SqlSession并把创建的权力交给spring,不用手动创建SqlSession对象了
  • 在实现类里操作mybatis里测试时需要拿到的SqlSession对象及getMapper方法
  • 总的来说,就三个地方的修改:
    • 用数据源替代mybatisUtils.class和mybatis-config.xml文件
    • 在spring中拿到sqlSessionFactory
    • 在spring中拿到sqlSession

实例:

  • 实体类
@Data
public class User {
     
    private int id;
    private String name;
    private String pwd;
}
  • 接口
public interface UserMapper {
     
    public List<User> queryUsers();
}
  • 接口映射类




<mapper namespace="com.qk.mapper.UserMapper">
    <select id="queryUsers" resultType="user">
        select * from mybatis.student;
    select>
mapper>
  • 增加的接口实现类
public class UserMapperImpl implements UserMapper{
     
    //在原来。我们所有的操作都使用sqlSession来执行,现在都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSession;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSession) {
     
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> queryUsers() {
     
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.queryUsers();
    }
}
  • 数据源配置,所有类的注册,配置文件绑定,拿到sqlSessionFactory和sqlSession

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8">property>
        <property name="username" value="root">property>
        <property name="password" value="root">property>
    bean>

       
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource">property>
        
        

        <property name="mapperLocations" value="classpath:com/qk/mapper/UserMapper.xml">property>
        <property name="typeAliasesPackage" value="com.qk.pojo">property>
    bean>

    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        
        <constructor-arg index="0" ref="sqlSessionFactory">constructor-arg>
    bean>

    <bean id="usermapperimpl" class="com.qk.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession">property>
    bean>


beans>
  • 测试
@Test
    public void test01(){
     
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper usermapperimpl = context.getBean("usermapperimpl", UserMapper.class);
        List<User> userList = usermapperimpl.queryUsers();
        for (User user : userList) {
     
            System.out.println(user);
        }
    }

第二种方式

  • SqlSessionDaoSupport 是一个抽象的支持类,用来为你提供 SqlSession。调用 getSqlSession() 方法你会得到一个 SqlSessionTemplate
  • 通过继承SqlSessionDaoSupport 类,在bean里面注入SqlSessionFactory,就可以拿到SqlSession,免去了set方法注入SqlSessionTemplate
  • 所以可以在拿到SqlSessionFactory以后不用再拿SqlSession,直接把SqlSessionFactory注入到实现类
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
     
    @Override
    public List<User> queryUsers() {
     
        return getSqlSession().getMapper(UserMapper.class).queryUsers();
    }
<bean id="userMapperImpl2" class="com.qk.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory">property>
    bean>

Spring整合Mybatis_第1张图片


关于实现类的作用和写法,可以参照spring-mybatis官网学习:
http://mybatis.org/spring/zh/sqlsession.html

你可能感兴趣的:(Spring学习)