创建Spring Boot 项目(2)

tech2022-07-14  160

创建Spring Boot 项目(2)

一、 添加持久层Mybatis Plus(1)添加相关依赖(2)添加相关配置 二、创建业务相关类创建系统用户实体类;创建系统用户持久层;创建系统用户业务层;创建系统用户控制层;

一、 添加持久层Mybatis Plus

(1)添加相关依赖

第一步、在项目pom.xml文件中添加依赖

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.0</version> </dependency>

(2)添加相关配置

在resource文件夹下的application.yml中添加配置

mybatis-plus: # 本地mapper路径 mapper-locations: classpath:mapper/*.xml # 指定实体类路径 typeAliasesPackage: com.*.*.entity global-config: db-config: # 指定自增ID类型 id-type: ASSIGN_ID

二、创建业务相关类

创建系统用户实体类;

第一步、创建系统用户实体类:

package com.example.Shixun1.entity; import lombok.Data; import java.util.Date; @Data public class SysUser { private Long id; private String userName; private String passWord; private Date createTime; private Integer userAge; private String remark; }

创建系统用户持久层;

第二步、创建系统用户持久层;

package com.example.Shixun1.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.Shixun1.entity.SysUser; import org.apache.ibatis.annotations.Mapper; @Mapper public interface SysUserMapper extends BaseMapper<SysUser> { }

创建系统用户业务层;

第三步、创建系统用户业务层;

package com.example.Shixun1.controller; import com.example.Shixun1.entity.SysUser; import com.example.Shixun1.service.SysUserService; 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.PutMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.List; @RestController public class SysUserController { @Autowired private SysUserService sysUserService; @GetMapping("/") public String index(){ return "欢迎访问"; } @GetMapping("/getUser") public SysUser getUser(Long id){ return sysUserService.getById(id); } @PostMapping("/addUser") public String addUser(SysUser sysUser){ sysUser.setCreateTime(new Date()); return sysUserService.save(sysUser)? "保存成功":"保存失败"; } @PutMapping("/updateUser") public String updateUser(SysUser sysUser){ return sysUserService.updateById(sysUser) ? "修改成功":"修改失败"; } @GetMapping("/list") public List<SysUser> list(){ return sysUserService.list(); } }

创建系统用户控制层;

第四步、创建系统用户控制层;

package com.example.Shixun1.controller; import com.example.Shixun1.entity.SysUser; import com.example.Shixun1.service.SysUserService; 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.PutMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.List; @RestController public class SysUserController { @Autowired private SysUserService sysUserService; @GetMapping("/") public String index(){ return "欢迎访问"; } @GetMapping("/getUser") public SysUser getUser(Long id){ return sysUserService.getById(id); } @PostMapping("/addUser") public String addUser(SysUser sysUser){ sysUser.setCreateTime(new Date()); return sysUserService.save(sysUser)? "保存成功":"保存失败"; } @PutMapping("/updateUser") public String updateUser(SysUser sysUser){ return sysUserService.updateById(sysUser) ? "修改成功":"修改失败"; } @GetMapping("/list") public List<SysUser> list(){ return sysUserService.list(); } }
最新回复(0)