Skip to content
标签
欢迎扫码关注公众号

MyBatis-Spring SqlSession

官方文档 第五章 使用 SqlSession 的示例。

在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession。一旦你获得一个 session 之后,你可以使用它来执行映射语句,提交或回滚连接。
最后,当不再需要它的时候, 你可以关闭 session

使用 MyBatis-Spring 之后, 你不再需要直接使用 SqlSessionFactory 了,因为你的 bean 可以通过一个线程安全的 SqlSession 来注入,基于 Spring 的事务配置 来自动提交,回滚,关闭 session

注意:通常不必直接使用 SqlSession。

在大多数情况下 MapperFactoryBean, 将会在 bean 中注入所需要的映射器。下一章节中的 MapperFactoryBean(6.1 节) 会解释这个细节。

SqlSessionTemplate

SqlSessionTemplateMyBatis-Spring 的核心。这个类负责管理 MyBatis 的 SqlSession, 调用 MyBatis 的 SQL 方法, 翻译异常。

SqlSessionTemplate 是线程安全的, 可以被多个 DAO 所共享使用。

SqlSessionTemplate 实现了 SqlSession 接口, 这就是说,在代码中无需对 MyBatis 的 SqlSession 进行替换。

SqlSessionTemplate 通常是被用来替代默认的 MyBatis 实现的 DefaultSqlSession , 因为模板可以参与到 Spring 的事务中并且被多个注入的映射器类所使用时也是线程安全的。

相同应用程序中两个类之间的转换可能会引起数据一致性的问题。

  1. UserDao.java

    java
    package liujiajia.me.learning.mybatis;
    
    public interface UserDao {
        User getUser(String account);
    }
  2. UserDaoImpl.java

    java
    package liujiajia.me.learning.mybatis;
    
    import org.apache.ibatis.session.SqlSession;
    
    public class UserDaoImpl implements UserDao {
        private SqlSession sqlSession;
    
        public void setSqlSession(SqlSession sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        @Override
        public User getUser(String account) {
            return (User) sqlSession.selectOne("liujiajia.me.learning.mybatis.UserMapper.getUser", account);
        }
    }
  3. UserMapper.java

    java
    package liujiajia.me.learning.mybatis;
    
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    /**
    * Created by liujiajia on 2019/1/23.
    */
    public interface UserMapper {
        @Select("SELECT * FROM User WHERE Account = #{account}")
        User getUser(@Param("account") String account);
    }
  4. applicationContext.xml

    配置各种 bean

    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.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=TEST"/>
            <property name="username" value="username"/>
            <property name="password" value="password"/>
        </bean>
        <!--Sql Session Factory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--Mapper-->
        <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="liujiajia.me.learning.mybatis.UserMapper"/>
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        <!--Sql Session-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
        <!--Dao-->
        <bean id="userDao" class="liujiajia.me.learning.mybatis.UserDaoImpl">
            <property name="sqlSession" ref="sqlSession"/>
        </bean>
    </beans>
  5. UserController.java

    java
    package liujiajia.me.learning.mybatis;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    /**
    * Created by liujiajia on 2019/1/23.
    */
    @RestController
    @RequestMapping("api/user")
    public class UserController {
        @Resource
        private UserDao userDao;
    
        @RequestMapping(value = "{account}", method = RequestMethod.GET)
        public User getUser(@PathVariable("account") String account) {
            return userDao.getUser(account);
        }
    }

SqlSessionDaoSupport

SqlSessionDaoSupport 是一个抽象的支持类, 用来为你提供 SqlSession 。调用 getSqlSession() 方法你会得到一个 SqlSessionTemplate, 之后可以用于执行 SQL 方法,

  1. UserDaoWithSupportImpl.java

    DAO 继承 SqlSessionDaoSupport 类。调用 getSqlSession() 方法你获得一个 SqlSessionTemplate

    java
    package liujiajia.me.learning.mybatis;
    
    import org.mybatis.spring.support.SqlSessionDaoSupport;
    
    public class UserDaoWithSupportImpl extends SqlSessionDaoSupport implements UserDao {
        @Override
        public User getUser(String account) {
            return (User) getSqlSession().selectOne("liujiajia.me.learning.mybatis.UserMapper.getUser", account);
        }
    }
  2. applicationContext.xml 中修改 userDao bean 的 classproperty

    xml
    <!--Dao-->
    <bean id="userDao" class="liujiajia.me.learning.mybatis.UserDaoWithSupportImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.