mybatis system learning (3) - using spring+mybatis to achieve simple operations

illustrate

According to the learning plan, I learned how to simply operate the mysql database with pure mybatis before. But in fact, I don't know how this practice is applied, because there is no such practice in my previous projects, almost all of them are spring+mybatis.
Therefore, I decided to start with the integration of spring and mybatis in this third article.

rely

Since it is spring+mybatis, it is natural that the related packages of spring and mybatis need to be imported. The configuration of maven is as follows:

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>

The above configuration needs to be specially explained that spring-context is to introduce the config.properties file in the spring configuration file; spring-jdbc is mainly to configure the data source; and mybatis-spring is obviously the main dependency of spring to integrate mybatis; the other two One is the basic dependency of using mybatis, which has already appeared in the previous article.

Integrate

Using spring to integrate mybatis, in addition to the foundation of mybatis, the basic knowledge of spring is naturally required. Usually, in the process of using spring, there is an entry file, usually called application.xml or spring.xml.
When we integrate mybatis here, we need the following basic 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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    <context:property-placeholder location="classpath:config.properties"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="study.tuzongxun.mapper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
</beans>

The description of the above configuration is as follows:
first, this file has a file header representing the xml file, not much to say here;
then it is the file header of the spring configuration file, that is, the content in the beans, which can actually be understood as a java code guide package; just Just like the functions of other classes that need to be used in java classes require a pilot package, some specific functions can only be used later after the corresponding introduction;
for example, use context:property-placeholder to introduce config. Like the .properties file, this function can only be used if there is the configuration of spring-context in the file header above;
after the configuration file is introduced, it is the configuration of the data source, and a specific processing class needs to be specified here, which is precisely because of this, Therefore, spring-jdbc was introduced; however, in general, the corresponding processing class of the connection pool may be used in the project;
after the configuration of the data source, a SqlSessionFactoryBean is declared. In fact, as mentioned in the previous article, SqlSessionFactoryBean is mybatis The key; there is a parameter that must be specified in this SqlSessionFactoryBean, which is dataSource, so there must be a dataSource configuration before this, whether it is placed in other files, introduced before this paragraph, or like the above example, in this file Statement; as for the mybatis.xml inside, it is not necessary from a functional point of view, but it may be generally required in actual projects, because there will be more detailed performance-related configurations, such as the configuration of printing mybatis logs;
then finally There is also a bean on the side, which depends on sqlSessionFactory, which is a data mapping bean that specifies the interface for operating the database; however, it should be noted that the implementation of this place is not unique, and there are other implementation methods, which will be discussed later.

mybatis configuration

As mentioned above, a dedicated mybatis configuration is generally used. In the above example, a log is actually configured, 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>
  <settings>
    <!-- The console prints logs such as sql -->
    <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>

java code

All the above are configurations. Since there are interfaces and entity classes, there are naturally java codes. However, in the current example, the same code as the previous article is used, but the configuration has been changed, so it is no longer listed. Refer to what was written before:
https://blog.csdn.net/tuzongxun/article/details/80097321

test

In order to prove that the above configuration has the same effect as the original pure mybatis use, a test class is needed to run the test results. The code of this class is as follows:

package study.tuzongxun.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import study.tuzongxun.mapper.UserMapper;
import study.tuzongxun.model.UserModel;
/**
 * test
 * @author Tu Zongxun
 * @date May 8, 2018
 */
public class SpringTest {
   public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
    List list = userMapper.findUsers();
    for (UserModel user : list) {
        System.out.println(user);
    }
   }
}

Project reference address:
https://github.com/tuzongxun/mybatis-study

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981875&siteId=291194637