Spring和mybatis的整合

十三、mybatis与spring的整合

直接上图:

整合步骤:

  1. 编写数据源,是因为没有数据源,但是我们可以使用spring的数据源类,来配置我们的连接信息;
  2. 获得我们的数据工厂
  3. 获得mybatis中的sqlsession,在spring中是sqlSessionTemplate
  4. 获得我们的mapper对象,可以使用一个注入,使用setter注入;
  5. 编写实体类,直接对数据结果进行一个返回;使用注入,将sqlsession注入我们的实体类,简单一点;
  6. 测试

这一点可以把我们的spring的特点反映出来了,就是一切交给spring托管,我们不需要在写一些重复的实例化对象的语句,配置地狱就是这么来的

applicationContext:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- more bean definitions go here -->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://com:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true"/>
        <property name="username" value="saxon"/>
        <property name="password" value="1234567/>
    </bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="datasource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/saxon/Dao/UserDao.xml"/>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="user" class="com.saxon.Dao.UserDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <context:annotation-config/>
</beans>

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>

</configuration>

userDaomapper:

<?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.saxon.Dao.UserDao">
<select id="getUser"  resultType="com.saxon.pojo.User">
    select * from saxon where id=#{id};
</select>
</mapper>

其他方法:

继承一个类SqlSessionDaoSupport直接获得sqlsession;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    @Override
    public User getUser (int id) {

        return this.getSqlSession ().getMapper (UserDao.class).getUser (1);
    }
}

注入:

由于SqlSessionDaoSupport需要一个参数就给他注入一个参数:

<bean id="user" class="com.saxon.Dao.UserDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>

不注入的话就使用默认自己创建一个,就会出错;

This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored.

自学总结
学习地址:狂神说Java

posted @ 2020-08-15 19:31  SaxonMo  阅读(91)  评论(0编辑  收藏  举报