pom配置

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.mybatis.spring.boot</groupId>
  8. <artifactId>mybatis-spring-boot-starter</artifactId>
  9. <version>2.1.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.projectlombok</groupId>
  13. <artifactId>lombok</artifactId>
  14. <version>1.18.10</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <scope>runtime</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-test</artifactId>
  24. <scope>test</scope>
  25. </dependency>
  26. </dependencies>

数据库连接信息

通过docker创建MySQL实例,创建名称是docker-compose-mysql.yml文件

  1. version: "3.0"
  2. services:
  3. mysql:
  4. network_mode: "bridge"
  5. container_name: mysql
  6. image: "mysql:5.7"
  7. volumes:
  8. - "./mysql:/var/lib/mysql"
  9. environment:
  10. MYSQL_ROOT_PASSWORD: "123456"
  11. MYSQL_USER: "spring"
  12. MYSQL_PASSWORD: "654321"
  13. MYSQL_DATABASE: "bot"
  14. ports:
  15. - "3306:3306"

运行MySQL实例

docker-compose -f docker-compose-mysql.yml up

在application.properties中添加数据量的连接信息


spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bot?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=spring
spring.datasource.password=654321
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

创建user表

通过docker的MySQL客户端 登录数据库

 docker exec -it mysql  mysql -uspring -h127.0.0.1 -p

在bot数据库中创建user表

CREATE TABLE `user` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` VARCHAR(128) NOT NULL COMMENT '姓名',
  `age` tinyint UNSIGNED NOT NULL COMMENT '年龄',
  `balance` DECIMAL(9,2) DEFAULT 0 COMMENT '账户余额',
  PRIMARY KEY (`id`)
);

Bean

创建实体Bean

package com.github.springboot.bean;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private int age;
    private double balance;
}

Mapper

编写Mapper,通过注解方式

package com.github.springboot.dao;

import com.github.springboot.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {

    @Select("SELECT id,name,age,balance FROM user LIMIT 10")
    List<User> selectAllUser();

    @Insert("INSERT INTO user(name,age,balance) VALUES(#{name},#{age},#{balance})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertUser(User user);

}

注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中

package com.github.springboot.controller;

import com.github.springboot.bean.User;
import com.github.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("list")
    public List<User>users(){
        return userService.selectAllUser();
    }

    @PostMapping("add")
    public User addUser(@RequestBody User user){
         user.setId(userService.insertUser(user));
         return user;
    }
}

在启动项中增加MapperScan

@SpringBootApplication
@MapperScan("com.github.springboot.dao")
public class SpringbootMybatisAnnotationApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisAnnotationApplication.class, args);
    }
}

创建用户

curl -X POST 'http://127.0.0.1:8080/user/add' -d '{"name":"qiang","age":30,"balance":10000}' -H 'Content-Type: application/json'
{"id":1,"name":"qiang","age":30,"balance":10000.0}

配置文件方式

配置Mapper映射文件
src\main\resources\mapper路径下加入UserMapper.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.github.springboot.dao.UserDao">

    <select id="selectUserByName" parameterType="String" resultType="com.github.springboot.bean.User">
        SELECT id,name,age,balance FROM user WHERE name = #{name} LIMIT 1
    </select>
</mapper>

在application.properties中添加mybatis的信息

#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

在userDao.java 增加

User selectUserByName(String name);

在UserService

    public User selectUserByName(String name) {
        return userDao.selectUserByName(name);
    }

在UserService 增加

    @GetMapping ("/search")
    public User selectUserByName(@RequestParam("name") String name){
        return userService.selectUserByName(name);
    }

参考

https://mybatis.io/downloads.html