spring整合mybatis

一:spring整合mybatis的思想是什么?

二:拥有条件:spring:ioc思想,mybatis:mybatis-config配置文件,对应的映射配置文件:UserMapper.xml,以及SqlSessionFactory,SqlSessionTemplate;如果是“我”该怎么做?

三:把SqlSession注入到bean中

方式一:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private int id;
    private Date birthday;
    private String sex;
    private String address;
}
public interface UserMapper {
    List<User> query();
}
package com.nbg.dao.impl;

import com.nbg.dao.UserMapper;
import com.nbg.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * 创建这个类的思想何在?
 */
public class UserMapperImpl implements UserMapper {

    /**
     * 原来我们使用SqlSession 现在使用SqlSessionTemplate
     */
    private SqlSessionTemplate sessionTemplate;

    /**
     * set方法用于注入SqlSessionTemplate
     *
     * @param sessionTemplate
     */
    public void setSessionTemplate(SqlSessionTemplate sessionTemplate) {
        this.sessionTemplate = sessionTemplate;
    }

    @Override
    public List<User> query() {
        UserMapper mapper = sessionTemplate.getMapper(UserMapper.class);
        return mapper.query();
    }
}
<?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>
        <package name="com.nbg.pojo"/>
    </typeAliases>

    <mappers>
        <mapper resource="com/nbg/dao/UserMapper.xml"/>
    </mappers>

</configuration>
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <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"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <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:com/nbg/dao/*.xml"/>-->
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--没有set方法,只能使用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userMapperImpl" class="com.nbg.dao.impl.UserMapperImpl">
        <property name="sessionTemplate" ref="sqlSession"/>
    </bean>

</beans>
<?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">
<mapper namespace="com.nbg.dao.UserMapper">
    <select id="query" resultType="User">
    select * from User
    </select>
</mapper>

 方式二:使用SqlSessionDaoSupport类,该类是一个抽象的支持类,用于提供sqlSession,使用个体Sqlsession()方法得到SqlSessionTemplate;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
    @Override
    public List<User> query() {
        return getSqlSession().getMapper(UserMapper.class).query();
    }
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<import resource="spring-dao.xml"/>
<!--方式一 自己配置sqlSession-->
<bean id="userMapperImpl" class="com.nbg.dao.impl.UserMapperImpl">
<property name="sessionTemplate" ref="sqlSession"/>
</bean>
<!--方式二 使用已经配置好的-->
<bean id="umi2" class="com.nbg.dao.impl.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
posted @ 2020-12-12 22:04  nbg  阅读(93)  评论(0编辑  收藏  举报