目录
1.引入依赖
2.数据库表的创建
3.数据源的配置
4.编写pojo类
5.编写controller类
6.编写接口
7.编写接口的实现类
8.编写mapper
1.引入依赖
在pom.xml引入依赖
<!-- mysql--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency>
<!--mybaits--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency>
<!-- lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
2.数据库表的创建
create database SpringbootDB;
use SpringbootDB;CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(50) NOT NULL UNIQUE,password VARCHAR(255) NOT NULL,phone VARCHAR(15) NOT NULL UNIQUE,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);INSERT INTO users (username, password, phone) VALUES('张三', 'hashed_abc123', '13800138000'),('李四', 'hashed_def456', '13800138001');
3.数据源的配置
在yml文件配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springbootdbusername: rootpassword: 1234
4.编写pojo类
package com.leo.springboothd.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data // Lombok annotation to generate all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects), including getters, setters, equals, hash, and toString methods.
@NoArgsConstructor // Lombok annotation to create an empty constructor.
@AllArgsConstructor // Lombok annotation to create a constructor with all properties as arguments.
public class User {private String username; // 用户名private String password; // 密码private String phone; // 电话号码// 注意:这里没有包含'id'和'created_at'字段,因为它们在原始请求中被省略了。// 如果需要与数据库中的表进行映射,请确保这些字段的存在与否符合你的需求。
}
package com.leo.springboothd.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {private Integer code;//响应码,1 代表成功; 0 代表失败private String msg; //响应信息 描述字符串private Object data; //返回的数据//增删改 成功响应public static Result success(){return new Result(0,"success",null);}//查询 成功响应public static Result success(Object data){return new Result(0,"success",data);}//失败响应public static Result error(String msg){return new Result(1,msg,null);}
}
5.编写controller类
package com.leo.springboothd.Controller;import com.leo.springboothd.pojo.Result;
import com.leo.springboothd.pojo.User;
import com.leo.springboothd.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j
@RestControllerpublic class UserController {@Autowiredprivate UserService userService;@PostMapping("/register")public Result add(@RequestBody User user) {log.info("注册信息{}",user);userService.add(user);System.out.println("用户新增成功"+user);return Result.success();}@PostMapping("/login")public Result login(@RequestBody User user) {log.info("登录信息{}",user);User userResult=userService.login(user);if(userResult==null){System.out.println("没有此用户");return Result.error("没有此用户");}else{System.out.println("用户登录成功"+user);return Result.success("登录成功");}}@GetMapping("/getall")public Result getall() {log.info("执行查询所有的Users操作");List<User> users= userService.getall();return Result.success(users);}}
6.编写接口
package com.leo.springboothd.service.impl;import com.leo.springboothd.mapper.UserMapper;
import com.leo.springboothd.pojo.User;
import com.leo.springboothd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl1 implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic void add(User user) {userMapper.add(user);}@Overridepublic List<User> getall() {return userMapper.getall();}@Overridepublic User login(User user) {return userMapper.login(user);}
}
7.编写接口的实现类
package com.leo.springboothd.service.impl;import com.leo.springboothd.mapper.UserMapper;
import com.leo.springboothd.pojo.User;
import com.leo.springboothd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl1 implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic void add(User user) {userMapper.add(user);}@Overridepublic List<User> getall() {return userMapper.getall();}@Overridepublic User login(User user) {return userMapper.login(user);}
}
8.编写mapper
package com.leo.springboothd.mapper;import com.leo.springboothd.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface UserMapper {@Insert("insert into users (username, password, phone) values (#{username}, #{password}, #{phone})")void add(User user);@Select("select * from users")List<User> getall();@Select("select * from users where username=#{username} and password=#{password}")User login(User user);
}