pom配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
数据库连接信息
通过docker创建MySQL实例,创建名称是docker-compose-mysql.yml文件
version: "3.0"
services:
mysql:
network_mode: "bridge"
container_name: mysql
image: "mysql:5.7"
volumes:
- "./mysql:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: "123456"
MYSQL_USER: "spring"
MYSQL_PASSWORD: "654321"
MYSQL_DATABASE: "bot"
ports:
- "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\mappe
r路径下加入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);
}