Spring整合Mybatis

整合Mybatis——方式一

步骤:

  1. 导入相关jar包
    • junit
    • mybatis
    • mysql数据库
    • spring相关的
    • aop织入
    • mybatis-spring[新包]
  2. 编写配置文件
  3. 测试

回顾Mybatis

  1. 编写实体类

    package net.cqwu.pojo;
    
    import lombok.Data;
    
    @Data
    public class User {
        private int sid;
        private String sname;
        private String pwd;
    }
    
  2. 编写核心配置文件

    <?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核心配置文件-->
    <configuration>
    
        <typeAliases>
            <package name="net.cqwu.pojo"/>
        </typeAliases>
    
    
        <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
        <!--整合后注释掉-->
    <!--    <mappers>-->
    <!--        <mapper class="net.cqwu.mapper.UserMapper"/>-->
    <!--    </mappers>-->
    </configuration>
    
  3. 编写接口

    package net.cqwu.mapper;
    
    import net.cqwu.pojo.User;
    
    import java.util.List;
    
    public interface UserMapper {
        public List<User> selectUser();
    }
    
  4. 编写Mapper.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 = 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="net.cqwu.mapper.UserMapper">
    
    <select id="selectUser" resultType="user">
        select * from mybatis.user;
    </select>

</mapper>
  1. 测试

Mybatis-spring

  1. 编写数据源配置

  2. sqlSessionFactory

  3. sqlSessionTemplate

    <?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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                    https://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/aop
                    https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--DataSource:使用spring的数据源替换mybatis的配置
            使用Spring提供的JDBC:org.springframework.jdbc.datasource-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
        <!--SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!--绑定MyBatis配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:net/cqwu/mapper/*.xml"/>
        </bean>
    
    
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    
            <!--无Set方法,只能使用构造器注入sqlSessionFactory-->
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    
    
    </beans>
    
    applicationContext.xml

    为之后的spring-mvc准备

    <?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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                    https://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/aop
                    https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
        <import resource="spring.xml"/>
    
        <bean id="userMapper" class="net.cqwu.mapper.UserMapperImpl">
            <property name="sqlSession" ref="sqlSession"/>
        </bean>
    
    </beans>
    
  4. 需要给接口加实现类【UserMapperImpl】

    package net.cqwu.mapper;
    
    import net.cqwu.pojo.User;
    import org.mybatis.spring.SqlSessionTemplate;
    
    import java.util.List;
    
    public class UserMapperImpl implements UserMapper {
    
        //所有操作,之前都使用sqlSession执行,现在使用sqlSessionTemplate
        private SqlSessionTemplate sqlSession;
    
        public void setSqlSession(SqlSessionTemplate sqlSession) {
            this.sqlSession = sqlSession;
        }
        public List<User> selectUser() {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
    
  5. 将自己写的实现类,注入到Spring中

  6. 测试

    import net.cqwu.mapper.UserMapper;
    import net.cqwu.pojo.User;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    public class MyTest {
        @Test
        public void test() throws IOException {
    
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
            for (User user : userMapper.selectUser()) {
                System.out.println(user);
            }
        }
    }
    

    测试结果

    User(sid=1, sname=feliks, pwd=123456)
    User(sid=2, sname=MaxPark, pwd=123123)
    User(sid=3, sname=afei, pwd=111111)
    User(sid=4, sname=slugger, pwd=654321)
    User(sid=5, sname=spiderman, pwd=222222)
    
    Process finished with exit code 0
    

方式二:SqlSessionDaoSupport

  1. 创建实现类
package net.cqwu.mapper;

import net.cqwu.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
    public List<User> selectUser() {
//        SqlSession sqlSession = getSqlSession();
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        return mapper.selectUser();
        //三行精简成一行
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}
  1. applicationContext.xml中新增
<bean id="userMapper2" class="net.cqwu.mapper.UserMapperImpl2">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
  1. 测试
public class MyTest {
    @Test
    public void test() throws IOException {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}

结果相同

😢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值