SpringBoot+Mybatis增删该查()
1、xml基础配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootMybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootMybatis</name>
<description>SpringBootMybatis</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<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>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置文件properties
spring.application.name=SpringBootMybatis
server.port=8088
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis.config-location=classpath:mybatis/mybatis-config.xml
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.mapper-locations=classpath:mapper/*.xml
3、项目目录
4、代码
User
package com.example.springbootmybatis.domain;
public class User {
private Long id; // 主键ID
private String name; // 姓名
private String password; // 密码
private Integer age; // 年龄
private String email; // 邮箱
private String updateTime; // 修改时间
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", email='" + email + '\'' +
", updateTime='" + updateTime + '\'' +
'}';
}
}
UserMapper
package com.example.springbootmybatis.mapper;
import com.example.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//@Mapper
public interface UserMapper {
// @Select("select * from user")
List<User> getList();
int deleteById(Long id);
int insert(User user);
User getById(Long id);
int update(User user);
}
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.example.springbootmybatis.mapper.UserMapper">
<select id="getList" resultType="com.example.springbootmybatis.domain.User">
select * from user
</select>
<delete id="deleteById" parameterType="java.lang.Long">
delete from user where id = #{id}
</delete>
<insert id="insert" parameterType="com.example.springbootmybatis.domain.User">
insert into user(id,name,password,age,email) values(#{id},#{name},#{password},#{age},#{email})
</insert>
<update id="update" parameterType="com.example.springbootmybatis.domain.User">
update user set name = #{name},password = #{password},age = #{age},email = #{email} where id = #{id}
</update>
<select id="getById" resultType="com.example.springbootmybatis.domain.User">
select * from user where id = #{id}
</select>
</mapper>
UserController
package com.example.springbootmybatis.controller;
import com.example.springbootmybatis.domain.User;
import com.example.springbootmybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("test")
public String test(){
return "hello world";
}
@GetMapping("getList")
public List<User> getList(){
return userMapper.getList();
}
@GetMapping("getById/{id}")
public User getById(@PathVariable Long id){
System.out.println(id);
return userMapper.getById(id);
}
@PostMapping("insert")
public int insert(@RequestBody User user){
System.out.println("--------------"+user.toString());
return userMapper.insert(user);
}
@PostMapping("update")
public int update(@RequestBody User user){
System.out.println("--------------"+user.toString());
return userMapper.update(user);
}
@DeleteMapping("deleteById/{id}")
public int deleteById(@PathVariable Long id){
System.out.println(id);
return userMapper.deleteById(id);
}
}
SpringBootMybatisApplication
package com.example.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springbootmybatis.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
5、请求
{
"id": 55,
"name": "John Doe",
"password": "password123",
"age": 30,
"email": "[email protected]",
"updateTime": "2023-10-10 12:00:00"
}
GET http://localhost:8088/getList
DELETE http://localhost:8088/deleteById/1
### 根据ID查询用户信息
GET http://localhost:8088/getById/3
标签:String,name,该查,spring,boot,id,增删,public,SpringBoot
From: https://www.cnblogs.com/2580p/p/18457389