SqlSession used by mybatis spring

Posted by graziano on Sat, 15 Jan 2022 16:33:16 +0100

In MyBatis, you can use SqlSessionFactory to create SqlSession. Once you get a session, you can use it to execute mapped statements, commit or rollback connections, and finally close the session when it is no longer needed. After using MyBatis Spring, you no longer need to directly use SqlSessionFactory, because your bean can be injected into a thread safe SqlSession, which can automatically commit, rollback and close sessions based on Spring's transaction configuration.

This is the description in the official document. The translated words are: in mybatis spring, use the SqlSessionTemplate to create SqlSession

catalogue

catalogue

1. Using SqlSessionTemplate

        1.1 Guide Package

        1.2 create User class

        1.3 create UserMapper interface

        1.4. Create usermapper XML file

        1.5 configure mybatis config xml

        1.6 configuring spring Dao xml

2,SqlSessionTemplate

1. Using SqlSessionTemplate

1.1 Guide Package

All dependent packages required for this tutorial are as follows

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.8</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

        <!--spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.8</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>


    </dependencies>

1.2 create User class

Three attributes, id, name and pwd, are defined, and they are all private attributes. Lombok is used to generate set, get and toString methods.

package com.jason.pojo;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class User {
    private long id;
    private String name;
    private String pwd;
}

1.3 create UserMapper interface

Define a method queryUsers() to query all users

package com.jason.mapper;

import com.jason.pojo.User;

import java.util.List;

public interface UserMapper {
    public List<User> queryUsers();
}

1.4. Create usermapper XML file

Write the query statement in the previous step. The id of the select statement is the method name in the UserMapper interface, and the resultType is mybatis config Configuration in XML

<?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.jason.mapper.UserMapper">
    <select id="queryUsers" resultType="User">
        select *
        from user
    </select>
</mapper>

1.5 configure mybatis config xml

mybatis-config. The XML file is mainly responsible for placing some alias configurations and mapping configurations in this project. The specific database connection information is placed in spring Dao. XML in the next section XML configuration. mybatis-config. The specific configuration of XML is as follows:

<?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>
    <!--alias-->
    <typeAliases>
        <typeAlias type="com.jason.pojo.User" alias="User"/>
    </typeAliases>

    <!--mapping-->
    <mappers>
        <package name="com.jason.mapper"/>
<!--        <mapper class="com.jason.mapper.UserMapper"/>-->
    </mappers>

</configuration>

If there are few mapped interface files, you can also use the following mapper class for configuration. However, if there are many interface files, it is more convenient to use the above package name for unified mapping,

<!--mapping-->
<mappers>
    <mapper class="com.jason.mapper.UserMapper"/>
</mappers>

1.6 configuring spring Dao xml

spring-dao.xml is the focus of this article, which involves the core of mybatis spring - SqlSessionTemplate.

  • First, configure the DataSource data source, which is the key to Spring (mybatis Spring) linking the database
<!--use Spring Data source replacement for Mybatis Configuration of-->
<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/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
    <property name="username" value="root"/>
    <property name="password" value="zs123456"/>
</bean>
  • Then use SqlSessionFactory bean to create SqlSessionFactory
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--binding Mybatis Profiles, setting aliases and mappings-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

Since we mentioned in the previous section that we need to use the alias and mapping of mybatis, we need to bind the configuration file of mybatis - mybatis config xml. The mapping can also be configured using the maperlocations attribute, but note that you need to set mybatis config The mapping information of the response in XML is removed, otherwise it will be repeated.

<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--binding Mybatis Profiles, setting aliases and mappings-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/jason/mapper/*.xml"/>
</bean>

Maperlocations added in the above configuration is to add all under the com/jason/mapper package All files at the end of xml are mapped.

2,SqlSessionTemplate

Topics: Java Mybatis Spring