Spring Boot Tutorial (6): Spring Boot integrates mybatis

1. Precondition

Create a simple spring boot project first, you can refer to the source code of the previous chapter or create one yourself.

2. Create a database

create database

create database springboot;

Create user table tbl_userand insert test data

CREATE TABLE `tbl_user` (
  `user_id` bigint(20) NOT NULL,
  `user_name` varchar(50) DEFAULT NULL,
  `user_age` int(11) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tbl_user` VALUES (1, '张三', 27);
INSERT INTO `tbl_user` VALUES (2, '李四', 30);
INSERT INTO `tbl_user` VALUES (3, '王五', 20);

The script sql is in the db folder of the source code.

3. Introduce mybatis dependencies

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

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

Fourth, write code

1. Create an entity

package com.songguoliang.mybatis.entity;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 14:59
 */
public class User {
    private Long userId;
    private String userName;
    private Integer userAge;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getUserAge() {
        return userAge;
    }

    public void setUserAge(Integer userAge) {
        this.userAge = userAge;
    }
}

2. Create Mapper file

Create a resourcesfolder under the mapperfolder, and then create it in the mapperfolder with the UserMapper.xmlfollowing contents:

<?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.songguoliang.mybatis.mapper.UserMapper">

    <select id="getUsers" resultType="User">
        SELECT * FROM tbl_user
    </select>

</mapper>

3. Create Mapper(Dao) interface

package com.songguoliang.mybatis.mapper;

import com.songguoliang.mybatis.entity.User;

import java.util.List;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 15:02
 */
public interface UserMapper {

    List<User> getUsers();
}

4. Create Service

package com.songguoliang.mybatis.service;

import com.songguoliang.mybatis.entity.User;
import com.songguoliang.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 15:01
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> getUsers() {
        return userMapper.getUsers();
    }
}

5. Create a controller

package com.songguoliang.mybatis.controller;

import com.songguoliang.mybatis.entity.User;
import com.songguoliang.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 14:59
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public List<User> lists() {
        return userService.getUsers();
    }

}

Five, configure mybatis

1. Configure the data source

Add the following code to the application.propertiesconfiguration file:

## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2. Add mybatis configuration

Add the configuration of mybatis to the application.propertiesconfiguration file, as follows:

# mybatis
mybatis.type-aliases-package=com.songguoliang.mybatis.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
  • mybatis.type-aliases-package: The package where the alias class (entity class) is located
  • mybatis.mapper-locations: The folder where the mapper file is located
  • mybatis.configuration.map-underscore-to-camel-case: Convert using camel case naming

3. Scan Mapper interface

Add annotations to the startup class @MapperScan("com.songguoliang.mybatis.mapper"), pay attention MapperScanto the package path ( org.mybatis.spring.annotation.MapperScan), and the imported package will be modified when integrating with Mapper4 in the following chapters.

package com.songguoliang.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 14:51
 */
@SpringBootApplication
@MapperScan("com.songguoliang.mybatis.mapper")
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

6. Test

Start the service, enter the browser, http://localhost:8080/usersand you can see the following:
write picture description here





Source code:
github
code cloud

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326683013&siteId=291194637