Spring整合Mybatis两种方式

方式一:

创建spring-dao.xml文件取代mybatis配置文件

<?xml version="1.0" encoding="UTF8"?>
<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/ljqdb"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--整合mybatis配置文件-->
    <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:Mapper/*/*.xml"/>
    </bean>
    <!--创建sqlSession实例-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

并非完全取代,setting属性和一些别名设置还是在mybatis配置文件中

<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--核心配置文件-->
<configuration>
<!--    别名-->
    <typeAliases>
        <package name="pojo"/>
    </typeAliases>
    
</configuration>

applicationContext.xml中为真实Bean配置

<?xml version="1.0" encoding="UTF8"?>
<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="userMapperImpl" class="Mapper.User.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

    <bean id="userMapper2" class="Mapper.User.UserMapper2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

在UserMapper的实现类中建立sqlSession属性 在bean配置中生成是注入sqlSession

package Mapper.User;
import org.mybatis.spring.SqlSessionTemplate;
import pojo.User;
import java.util.List;
/**
 * @author:LeeGaki
 * @date:2022/1/14
 */
public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    @Override
    public List<User> findAllUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.findAllUser();
    }
}

方式二:

省去了注入配置Bean中的sqlSession和sqlSession的注入

用实现类继承SqlSessionDaoSupport类 配置实现类bean的时候

将sqlSessionFactory作为属性的值传进去

    <bean id="userMapper2" class="Mapper.User.UserMapper2">
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

实现类中则更为简单,只需一行代码

public class UserMapper2 extends SqlSessionDaoSupport implements UserMapper{
    @Override
    public List<User> findAllUser() {
        return getSqlSession().getMapper(UserMapper.class).findAllUser();
    }
}

测试类的编写 

public class MyTest {

    @Test
    public void findAll(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper2 userMapper2 = context.getBean("userMapper2", UserMapper2.class);
        for (User user : userMapper2.findAllUser()) {
            System.out.println(user);
        }
    }
}

上一篇:分享Nginx在Windows下的管理命令(bat文件)


下一篇:新浪微博开放平台API