package com.example.demo333333; import com.example.demo333333.controller.Controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.List; @SpringBootApplication public class Demo333333Application { public static void main(String[] args) { var root = SpringApplication.run(Demo333333Application.class, args); } }
首先是controller
package com.example.demo333333.controller; import com.example.demo333333.SysUserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import com.example.demo333333.SysUser; import java.util.List; @RestController @RequestMapping("/") public class Controller { @Autowired SysUserMapper mapper; @GetMapping("/") public String gohelp(){ System.out.println("go to /index"); return "go to /index"; } @GetMapping("/index") public String test(){ System.out.println("hhhh"); return "hhhhhh"; } @GetMapping("/sys_users") public List<SysUser> getAllSysUsers() { return mapper.selectAll(); } @GetMapping("/sys_user/{id}") public SysUser getSysUserById(@PathVariable int id) { return mapper.selectUserById(id); } @GetMapping("/sys_user/{id}/{column}") public Object getUserEmail(@PathVariable String column, @PathVariable Long id) { return mapper.getColumnValueById(column, id); } // PUT 请求,更新用户信息 @PutMapping("/sys_user/{id}") public void updateUser(@PathVariable int id, @RequestBody SysUser user) { user.setUser_id(id); mapper.updateUser(user); } // POST 请求,创建新用户 @PostMapping("/sys_user") public void createUser(@RequestBody SysUser user) { mapper.insertUser(user); } // DELETE 请求示例 @DeleteMapping("/sys_user/{id}") public void deleteUser(@PathVariable Long id) { mapper.deleteUser(id); } }
然后是mapper
package com.example.demo333333; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.List; //@Repository @Mapper public interface SysUserMapper { @Select("SELECT * FROM sys_user") List<SysUser> selectAll(); @Select("SELECT * FROM sys_user WHERE user_id = #{id}") SysUser selectUserById(int id); @Select("SELECT ${columnName} FROM sys_user WHERE user_id = #{user_id}") String getColumnValueById(@Param("columnName") String columnName, @Param("user_id") long user_id); @Update("UPDATE sys_user SET nick_name = #{nick_name}, phonenumber = #{phonenumber} WHERE user_id = #{user_id}") void updateUser(SysUser user); @Insert("INSERT INTO sys_user (user_id, user_name, nick_name) VALUES (#{user_id}, #{user_name}, #{nick_name})") void insertUser(SysUser user); @Delete("DELETE FROM sys_user WHERE user_id = #{id}") void deleteUser(@Param("id") long user_id); }
然后是entity
package com.example.demo333333; public class SysUser { int user_id; int dept_id; String user_name; String nick_name; String user_type; String email; String phonenumber; char sex; public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public int getDept_id() { return dept_id; } public void setDept_id(int dept_id) { this.dept_id = dept_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getNick_name() { return nick_name; } public void setNick_name(String nick_name) { this.nick_name = nick_name; } public String getUser_type() { return user_type; } public void setUser_type(String user_type) { this.user_type = user_type; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } @Override public String toString() { return "SysUser{" + "user_id=" + user_id + ", dept_id=" + dept_id + ", user_name='" + user_name + '\'' + ", nick_name='" + nick_name + '\'' + ", user_type='" + user_type + '\'' + ", email='" + email + '\'' + ", phonenumber='" + phonenumber + '\'' + ", sex=" + sex + '}'; } }
yml
spring: application: name: demo333333 datasource: url: jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=UTC username: 用户名 password: 密码 driver-class-name: com.mysql.cj.jdbc.Driver
pom.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.2.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo333333</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo333333</name> <description>demo333333</description> <properties> <java.version>22</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <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>
暂无关于此日志的评论。