Spring-05-整合Mybatis-Spring

124 阅读1分钟

Spring 整合mybatis

方式一 SqlSessionTemplate

导入依赖

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

spring连接数据库所需依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.1</version>
    <scope>compile</scope>
</dependency>

创建数据源

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>

</bean>

创建SqlSessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--绑定mapper配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:mapper/UserMapper.xml"/>
</bean>

获取SqlSession

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!-- SqlSessionTemplate 无set方法,通过构造器注入-->
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

实现类

package mapper;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import pojo.User;

import java.util.List;

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
    private SqlSessionTemplate sqlSession;

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

    public List<User> getUserList() {
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        return userMapper.getUserList();
    }
}
<bean id="UserMapperImpl" class="mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"></property>
</bean>

测试

import mapper.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;

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("mybatis-spring.xml");
        UserMapper userMapperImpl = context.getBean("UserMapperImpl", UserMapper.class);
        List<User> userList = userMapperImpl.getUserList();
        for (User user:userList){
            System.out.println(user);
        }
        
    }
}

方式二 SqlSessionDaoSupport

继承SqlSessionDaoSupport

package mapper;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import pojo.User;

import java.util.List;

public class UserMapperImpl extends SqlSessionDaoSupport {

    public List<User> getUserList() {
        return getSqlSession().getMapper(UserMapper.class).getUserList();
    }
}

注册实现类

<bean id="usermapperimpl2" class="mapper.UserMapperImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>