1,新建项目
2,选择Spring Initializr
3,默认配置
4,默认配置一路点击到最后完成。
5,找到pom.xml增加相关依赖,注意看屏幕右下角,点击自动导入
<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>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
6,添加数据库连接。
7,如下图填写,并点击ok
8,如下图所示,复制代码,创建用户表
CREATE TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` varchar(30) DEFAULT NULL COMMENT '姓名',
`user_age` int(11) DEFAULT NULL COMMENT '年龄',
`user_email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4
9,如下图,去到自动生成实体类的脚本目录。
10,打开脚本,并修改生成的实体类的包名,如果此时没有这个脚本,则右键Database Tools And SQL ,并进行重置
箭头处入下图改为com.example.demo.demain; 注意必须带分好结尾。
11,新建实体类包 com.example.demo.domain
12,点击打开生成实体类窗口
13,填写生成后的文件路径。点击ok
14,生成后如下图所示
15,增加mapper目录新建UserMapper.java并编写sql
package com.example.demo.mapper;
import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
@Insert("INSERT INTO user (`user_name`, `user_age`, `user_email`) values (#{userName}, #{userAge}, #{userEmail})")
int addUser(String userName, int userAge, String userEmail);
@Delete("Delete FROM user where user_name = #{userName}")
int delUser(String userName);
@Update("Update user set `user_age` = #{userAge} where user_name = #{userName}")
int updateUser(String userName, int userAge);
@Select("Select * from user")
List<User> list();
}
16,DemoApplication增加mapper扫描 @MapperScan("com.example.demo.mapper")
17,新增controller目录,并编写api接口代码
package com.example.demo.controller;
import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
@Autowired
UserMapper userMapper;
// 增
@GetMapping("add/{userName}/{userAge}/{userEmail}")
int addUser(@PathVariable("userName") String userName, @PathVariable("userAge") int userAge, @PathVariable("userEmail") String userEmail) {
return userMapper.addUser(userName, userAge, userEmail);
}
// 删
@GetMapping("del/{userName}")
int delUser(@PathVariable("userName") String userName) {
return userMapper.delUser(userName);
}
// 改
@GetMapping("update/{userName}/{userAge}")
int updateUser(@PathVariable("userName") String userName, @PathVariable("userAge") int userAge) {
return userMapper.updateUser(userName, userAge);
}
// 查
@GetMapping("list")
List<User> list() {
return userMapper.list();
}
}
18,配置数据库连接和属性命名方式
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test?userUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#让数据库中带底杠的字段对应到实体类中驼峰方式命名的属性
mybatis.configuration.map-underscore-to-camel-case=true
19,打开浏览器测试
新增:http://localhost:8080/user/add/%E5%B0%8F%E6%9D%8E/18/xiaoli@a.com
修改:http://localhost:8080/user/update/%E5%B0%8F%E6%9D%8E/20