spring-mybatis整合

spring-mybatis整合

需要导入的包

junit
lombok(可选)
mybatis-spring
spring-webmvc
aspectjweaver
mysql-connector-java
mybatis
spring-jdbc

整合方法一

pojo类+mapper+mapper.xml不变

添加一个接口实现类,私有化sqlSessionTemplate

public class StudentMapperImpl implements StudentMapper{
    private SqlSessionTemplate sqlSession;

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

    public List<Student> getStudent() {
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        return mapper.getStudent();
    }
}

mybatis-config.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>
        <typeAlias alias="student" type="com.chao.pojo.Student"/>
    </typeAliases>
</configuration>

spring-dao.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"
       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.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test1?useUnicode=true&amp;
                characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai"/>
        <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"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/chao/mapper/*.xml"/>
    </bean>
    <!--注册sqlSessionTemplate,关联sqlSessionFactory;-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <import resource="spring-dao.xml"/>

    <bean id="StudentMapper" class="com.chao.mapper.StudentMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>

测试类

@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentMapper studentMapper = context.getBean("StudentMapper", StudentMapper.class);
    List<Student> students = studentMapper.getStudent();
    for (Student student : students) {
        System.out.println(student);
    }
}

整合方式二

在原来的基础上修改接口实现类和applicationContext.xml中注册它的配置

public class StudentMapperImpl extends SqlSessionDaoSupport implements StudentMapper{

    public List<Student> getStudent() {
        return getSqlSession().getMapper(StudentMapper.class).getStudent();
    }
}
<bean id="StudentMapper" class="com.chao.mapper.StudentMapperImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

spring-dao.xml可以去除掉

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

dao继承Support类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate.

SqlSessionDaoSupport是对SqlSessionTemplate的处理,需要的是sqlSessionFactory,因此可以去掉sqlSession的相关配置。

上一篇:myBatis输出参数


下一篇:mybatis安装和配置