3.配置文件org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web 2.5.6 org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java org.projectlombok lombok org.springframework.boot spring-boot-starter-test test
(1)application.yml
#激活开发配置文件
spring:
profiles:
active: dev
(2)application-dev.yml
#端口号8888
server:
port: 8888
#连接数据库
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
#绑定mapper包下的xml文件
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.kuang.pojo
4.新建User类
package com.kuang.pojo;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String password;
}
5.在mapper包中新建UserMapper接口
package com.kuang.mapper;
import com.kuang.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
//根据id查询用户信息
User getUserInfo(int id);
//插入用户
int insert (User user);
//更新用户
int update (User user);
//根据id删除用户
int deleteById(int id);
}
6.在service包中新建实现类UserService.java
package com.kuang.service;
import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public User getUserInfo(int id) {
return userMapper.getUserInfo(id);
}
public User insert(User user) {
userMapper.insert(user);
return user;
}
public int update(User user) {
return userMapper.update(user);
}
public int deleteById(int id) {
return userMapper.deleteById(id);
}
}
7.在controller包中新建访问类UserController.java
package com.kuang.controller;
import com.kuang.pojo.User;
import com.kuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
//通过用户id获取用户所有信息
@RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)
public String GetUser(@PathVariable int id) {
return userService.getUserInfo(id).toString();
}
//通过用户id删除用户
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String delete(int id) {
int result = userService.deleteById(id);
if (result >= 1) {
return "删除成功";
} else {
return "删除失败";
}
}
//根据用户id更新用户信息
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(User user) {
int result = userService.update(user);
if (result >= 1) {
return "修改成功";
} else {
return "修改失败";
}
}
//插入新用户
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public User insert(User user) {
return userService.insert(user);
}
}
8.在src/main/resources/mapper文件夹下新建UserMapper的映射文件UserMapper.xml
9.启动入口类扫描包
package com.kuang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//扫描mapper包
@MapperScan("com.kuang.mapper")
@SpringBootApplication
public class SpringBootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTestApplication.class, args);
}
}
10.测试
(1)根据id查询用户
(2)根据id删除用户
(3)更新用户
(4)插入用户



