后端来说的话毕竟只进行了CRUD所以代码量还是很少,很简单的,后端有一些需要特别注意的点!
首页第一步连接数据库配置myabatisPlus需要导入的pom依赖是:
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
导入依赖之后在application.properties文件里面写数据库连接等等:
server.port=9090
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/travelmanage?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=travelmanage
spring.datasource.password=travelmanage
mybatisPlus需要一个配置类mybatisPlusConfig
package cn.com.zzn.common;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("cn.com.zzn.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
Result封装类
package cn.com.zzn.common;
public class Result<T> {
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result(){
}
public Result(T data){
this.data = data;
}
public static Result success(){
Result result = new Result<>();
result.setCode("0");
result.setMsg("成功");
return result;
}
public static <T> Result<T> success(T data){
Result<T> result = new Result<>(data);
result.setCode("0");
result.setMsg("成功");
return result;
}
public static Result error(String code,String msg){
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
实体类层
package cn.com.zzn.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("user")
@Data
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
@TableField("userId")
private String userId;
@TableField("userPwd")
private String userPwd;
private String nickName;
private Integer age;
private String sex;
private String address;
}
实习crud的mapper层
@Repository
public interface UserMapper extends BaseMapper<User> {
}
controller层
import cn.com.zzn.common.Result;
import cn.com.zzn.entity.User;
import cn.com.zzn.mapper.UserMapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
UserMapper userMapper;
@PostMapping
public Result<?> save(@RequestBody User user) {
if (user.getUserPwd() == null){
user.setUserPwd("123456");
}
userMapper.insert(user);
return Result.success();
}
@GetMapping
public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(defaultValue = "") String search) {
LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
if (StrUtil.isNotBlank(search)){
wrapper.like(User::getNickName,search);
}
Page<User> userPage = userMapper.selectPage(new Page<>(pageNum,pageSize ),wrapper);
return Result.success(userPage);
}
}
sql文件
-- auto-generated definition
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`userId` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '用户名',
`userPwd` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '密码',
`nick_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '昵称',
`age` int(11) NULL DEFAULT NULL COMMENT '年龄',
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '性别',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户信息表' ROW_FORMAT = Dynamic;
INSERT INTO `user` VALUES (1, 'admin', 'admin', '管理员', 32, '男', '北京市');
INSERT INTO `user` VALUES (5, '2', NULL, '2', 2, '女', '2');
INSERT INTO `user` VALUES (6, '3', '123456', '3', 3, '未知', '3');
INSERT INTO `user` VALUES (7, '11', NULL, '11', 11, '男', '11');
INSERT INTO `user` VALUES (8, '2', NULL, '2', 2, '女', '2');
INSERT INTO `user` VALUES (10, '11', NULL, '11', 11, '男', '11');
INSERT INTO `user` VALUES (11, '2', NULL, '2', 2, '女', '2');
INSERT INTO `user` VALUES (13, 'zhang', '123', NULL, NULL, NULL, NULL);
|