SpringBoot series - integration Mybatis (XML configuration)

This article describes the integration process under SpringBoot Mybatis (XML configuration) of.

First, what is MyBatis?

MyBatis is an excellent persistence framework that supports custom SQL, stored procedures and advanced mappings. MyBatis avoids almost all JDBC code and manual setting parameters and obtaining the result set. MyBatis can use simple XML configuration and mapping annotations or native types, interfaces and Java POJO (Plain Old Java Objects, plain old Java object) is recorded in the database.

Second, integrated approach

SpringBoot integration Mybatis there are two ways, namely XML configuration and annotation mode, the main advantage points are as follows:

  1. Notes ways: the code more streamlined and convenient.
  2. XML configuration: isolation sql and business code, clear expression sql, especially for longer sql.

XML mapping file is very simple, only a few top-level elements:

  • cache - cache configuration for a given namespace.
  • cache-ref - cache references to other namespace configurations.
  • resultMap - is the most complex and powerful element that describes how to load from the database result object.
  • sql - reusable statement block can be referenced by other statements.
  • insert - mapping insert statements.
  • update - A mapped UPDATE statement.
  • delete - delete mapped statement.
  • select - mapping query.

This article describes the XML configuration, the follow-up article will introduce annotation mode.

Third, actual

Create a spring boot item spring-boot-mybatis-xml, follow the steps below.

1.pom.xml introduced in jar

Integrated core MyBatis is dependent MyBatis-Spring-Boot-Starter, which provides:

  • Automatic detection of existing DataSource.
  • Will create and register instances SqlSessionFactory of this example using the DataSource SqlSessionFactoryBean passed as input.
  • You will create and register the instance SqlSessionTemplate obtained from the SqlSessionFactory.
  • Automatically scan your mappers, link them to SqlSessionTemplate and registering Spring context, in order to inject them into your bean.

pom.xml important part as follows:

<!-- mybatis-starter  -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- MySQL 连接驱动依赖 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2.application.yml add configuration

application.yml add data sources and mybatis configuration, as follows:

spring:
  #数据源
  datasource:
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
  typeAliasesPackage: com.example.springboot.mybatisxml.entity
  mapperLocations: classpath:mapper/*.xml
  config-location: classpath:mybatis-config.xml

3. Add User mapping file

UserMapper.xml reads as follows:

<?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.example.springboot.mybatisxml.dao.mapper.UserMapper" >
    <resultMap id ="UserMap" type="com.example.springboot.mybatisxml.entity.User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="sex" property="sex"/>
        <result column="password" property="password"/>
        <result column="des" property="des"/>
    </resultMap>

    <select id = "queryAllUsers" resultType= "com.example.springboot.mybatisxml.entity.User">
      select * from user
    </select>
</mapper>

4. Add the interface dao

The same name and the name of the file mapping interface, the same id name tag interface methods and mapping files to be called in.

UserMapper.java code is as follows:

public interface UserMapper {

    List<User> queryAllUsers();
}

5. Add access control layer

UserController code is as follows:

/**
 * UserController
 *
 * @Author: java_suisui
 *
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询 所有用户
     *
     */
    @GetMapping("/queryAllUsers")
    public List<User> queryAllUsers(){
        return userService.queryAllUsers();
    }
}

Fourth, the test

Local open your browser, visit http: // localhost: 8080 / user / queryAllUsers, after successfully returned the following results:

[{"id":1,"name":"张三","password":"123456","sex":0,"des":"无备注"},
{"id":2,"name":"李四","password":"123456","sex":0,"des":"无备注"}]

This SpringBoot integration Mybatis (XML configuration) functions have all been achieved, there are questions please leave a message to communicate Oh!

Complete Source Address: https://github.com/suisui2019/springboot-study

Recommended Reading

In 1.Java print log, four points is very important!

2.SpringBoot integrated JWT achieve certification authority

3. take you one minute JWT certification!

4.SpringBoot how to read yml elegant profile?

In 5.SpringBoot how flexible implementation of data encryption and decryption function interface?


Limited Time receive a free Java-related information, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo / Kafka, Hadoop, Hbase, Flink high concurrent distributed, big data and machine learning techniques.
Watch below the number to receive a free public:

Java annoying chatters public No.

Guess you like

Origin www.cnblogs.com/haha12/p/11839872.html
Recommended