SpringBoot-Integrate Mybatis framework

1. Build the SpringBoot skeleton 

1. Quickly build SpringBoot skeleton through Spring Intializr 

2. Modify the dependent inheritance package version in the pom file to 2.0.1

<!-- 所有的 springboot 工程都必须继承 spring-boot-starter-parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

3. Add other related dependencies

<!-- mybatis 起步依赖-->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.1.1</version>
</dependency>
<!-- MySQL 连接驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

2. Create the User table in the database

CREATE DATABASE IF NOT EXISTS spring_boot;
USE spring_boot;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT NULL,
`gender` VARCHAR(50) DEFAULT NULL,
`age` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', '张三', '男', '18');
INSERT INTO `user` VALUES ('2', '李四', '女', '16');

Three. Create the corresponding entity package class

public class User implements Serializable {

    private Integer id;
    private String name;
    private String gender;
    private String age;

    // getter/setter 方法略....

}

Four. Write Mapper

Create UserMapper interface under java/com/cast/mapper

@Mapper
public interface UserMapper {

    //查询全部
    public List<User> userList();

}

Five. Write the sql configuration file for operating the database

Create UserMapper.xml under resources/mapper

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

6. Configure the relevant information of the connection database and the relevant information of the entity class and the mapping file

#数据库连接信息
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_boot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码

# spring 集成 Mybatis 环境
# pojo 别名扫描包
mybatis.type-aliases-package=com.cast.domain
#加载 Mybatis 映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

Seven. Write the controller layer 

@Controller
public class MybatisController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> queryUserList(){
        List<User> users = userMapper.userList();
        return users;
    }

}

test

1. Start SpringbootMybatisApplication,

2. Enter http://localhost:8080/findAll in the browser  

Display the results in the database:

 

 

Source code download:  https://pan.baidu.com/s/1CwrtYMJ64IfW6_YSKAPbYA

 

Guess you like

Origin blog.csdn.net/weixin_42629433/article/details/84845489