目錄
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);
}