Browse Source

1、修改了NOW()代替LocalDataTime.now()

2、新增了用例树的增删改查操作
chubin
郑楚滨 3 months ago
parent
commit
f44640f692
  1. 21
      demo/demo/README.md
  2. 108
      demo/demo/src/main/java/demo/Controller/CaseTreeController.java
  3. 16
      demo/demo/src/main/java/demo/Controller/RoleController.java
  4. 8
      demo/demo/src/main/java/demo/Controller/UserController.java
  5. 4
      demo/demo/src/main/java/demo/Entity/CaseInfo.java
  6. 41
      demo/demo/src/main/java/demo/Entity/CaseTree.java
  7. 12
      demo/demo/src/main/java/demo/Entity/CaseTreeVO.java
  8. 39
      demo/demo/src/main/java/demo/Mapper/CaseTreeMapper.java
  9. 8
      demo/demo/src/main/java/demo/Mapper/RoleMapper.java
  10. 8
      demo/demo/src/main/java/demo/Mapper/UserMapper.java
  11. 4
      demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java
  12. 70
      demo/demo/src/main/java/demo/Service/CaseInfoService.java
  13. 73
      demo/demo/src/main/java/demo/Service/CaseTreeService.java
  14. 8
      demo/demo/src/main/java/demo/Service/RoleService.java
  15. 8
      demo/demo/src/main/java/demo/Service/UserRoleService.java
  16. 12
      demo/demo/src/main/java/demo/Service/UserService.java

21
demo/demo/README.md

@ -18,4 +18,23 @@
3、修改时,直接修改即可,关联表只是存储角色ID,不可变
4、查询时,直接查询即可
4、查询时,直接查询即可
用例树表
增:传入参数有【节点名称、父节点ID、创建者(先传入,后面直接获取当前用户信息)】,自动生成节点ID(唯一)
删:传入参数有【节点ID】或者【节点名称】
改:传入参数有【节点ID、节点名称、父节点ID、修改者(先传入,后面直接获取当前用户信息)】
查:传入参数有【节点ID、节点名称】(至少一个)
1、主键类型改为tinyint,自增
2、默认新增节点为叶子节点
3、将IS_LEAF改为tinyint类型,默认值为0,当非0的时候表明非叶子节点
commit
修改了NOW()代替LocalDateTime.now()

108
demo/demo/src/main/java/demo/Controller/CaseTreeController.java

@ -0,0 +1,108 @@
package demo.Controller;
import demo.Entity.CaseTree;
import demo.Entity.CaseTreeVO;
import demo.Entity.Result;
import demo.Service.CaseTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/casetree")
@Validated
public class CaseTreeController {
@Autowired
private CaseTreeService caseTreeService;
@PostMapping("/insert")
public Result<String> insertNode(@Valid CaseTreeVO caseTreeVO) {
//确认新建节点名称未被占用
CaseTree caseTree = caseTreeService.findByNodeName(caseTreeVO.getNodeName());
if(caseTree == null) {
caseTreeService.addNode(caseTreeVO.getNodeName(), caseTreeVO.getParentNodeId(), caseTreeVO.getCreator());
return Result.success("新建节点成功");
}
else{
return Result.error(401,"节点已存在");
}
}
@DeleteMapping("/delete")
public Result<String> deleteNode(@Valid CaseTreeVO caseTreeVO) {
if(caseTreeVO.getId() != null){
CaseTree caseTreeByNodeId = caseTreeService.findByNodeId(caseTreeVO.getId());
if(caseTreeByNodeId == null){
return Result.error(401,"节点不存在");
}
if(caseTreeByNodeId.getIsLeaf() > 0){
return Result.error(401,"节点非叶子节点,无法删除");
}
else{
caseTreeService.deleteNode(caseTreeByNodeId);
return Result.success("删除节点成功");
}
}
else if(caseTreeVO.getNodeName() != null){
CaseTree caseTreeByNodeName = caseTreeService.findByNodeName(caseTreeVO.getNodeName());
if(caseTreeByNodeName == null){
return Result.error(401,"节点不存在");
}
if(caseTreeByNodeName.getIsLeaf() > 0){
return Result.error(401,"节点非叶子节点,无法删除");
}
else{
caseTreeService.deleteNode(caseTreeByNodeName);
return Result.success("删除节点成功");
}
}
else{
return Result.error(401,"节点不存在");
}
}
@PutMapping("/update")
public Result<String> updateNode(@Valid CaseTreeVO caseTreeVO) {
CaseTree caseTree = caseTreeService.findByNodeId(caseTreeVO.getId());
caseTree.setNodeName(caseTreeVO.getNodeName() == null? caseTree.getNodeName() : caseTreeVO.getNodeName());
caseTree.setParentNodeId(caseTreeVO.getParentNodeId() == null? caseTree.getParentNodeId() : caseTreeVO.getParentNodeId());
caseTree.setEditor(caseTreeVO.getEditor() == null? caseTree.getEditor() : caseTreeVO.getEditor());
if(caseTree == null) {
return Result.error(401,"节点不存在");
}
else{
caseTreeService.updateNode(caseTreeVO);
return Result.success("修改节点成功");
}
}
@GetMapping("/query")
public Result<List<CaseTree>> queryNode() {
List<CaseTree> caseTreeList = caseTreeService.query();
return Result.success(caseTreeList);
}
@PostMapping("/query")
public Result<CaseTree> queryByNode(@Valid CaseTreeVO caseTreeVO) {
CaseTree caseTree = new CaseTree();
if(caseTreeVO.getId() != null){
caseTree = caseTreeService.findByNodeId(caseTreeVO.getId());
if(caseTreeVO.getNodeName() != null && !caseTreeVO.getNodeName().equals(caseTree.getNodeName())){
return Result.error(401,"节点不存在");
}
}
else if(caseTreeVO.getNodeName() != null){
caseTree = caseTreeService.findByNodeName(caseTreeVO.getNodeName());
}
else{
return Result.error(401,"节点不存在");
}
if(caseTree == null) {
return Result.error(401,"节点不存在");
}
else return Result.success(caseTree);
}
}

16
demo/demo/src/main/java/demo/Controller/RoleController.java

@ -42,13 +42,7 @@ public class RoleController {
Role role = roleService.findByRoleName(roleVO.getRoleName());
if (role==null){
// 没有占用,新增
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// 将Instant转换为Date
Date date = Date.from(instant);
roleService.insert(roleVO.getRoleName(), roleVO.getRoleCode(), date);
roleService.insert(roleVO.getRoleName(), roleVO.getRoleCode());
return Result.success("添加成功");
}else {
return Result.error(401, "角色已有");
@ -83,13 +77,7 @@ public class RoleController {
if (role==null){
return Result.error(401, "角色不存在");
}else {
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// 将Instant转换为Date
Date date = Date.from(instant);
roleService.update(roleVO, date);
roleService.update(roleVO);
return Result.success("修改成功");
}
}

8
demo/demo/src/main/java/demo/Controller/UserController.java

@ -104,13 +104,7 @@ public class UserController {
User user = userService.findByUserName(userVo.getLoginName());
if (user!=null){
// 存在,进行更新
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// 将Instant转换为Date
Date date = Date.from(instant);
userService.updateByUserName(userVo.getLoginName(), userVo.getUserChnName(), date, userVo.getPassword());
userService.updateByUserName(userVo.getLoginName(), userVo.getUserChnName(), userVo.getPassword());
UserRole userRole = userroleService.findByUserId(user.getId());
if(userRole!=null){
userroleService.update(user.getId(), roleId);

4
demo/demo/src/main/java/demo/Entity/CaseInfo.java

@ -0,0 +1,4 @@
package demo.Entity;
public class CaseInfo {
}

41
demo/demo/src/main/java/demo/Entity/CaseTree.java

@ -0,0 +1,41 @@
package demo.Entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Entity
@Data
@Table(name = "case_tree")
public class CaseTree {
@Id
@Column(length = 32, nullable = false)
private int id;
@Column(length = 50)
private String nodeName;
@Column(length = 50)
private int parentNodeId;
@Column(length = 32, nullable = false)
private int isLeaf;
@Column(length = 350)
private String idPath;
@Column(length = 50)
private String creator;
@Temporal(TemporalType.TIMESTAMP)
@Column()
private Date createTime;
@Column(length = 50)
private String editor;
@Temporal(TemporalType.TIMESTAMP)
@Column()
private Date updateTime;
}

12
demo/demo/src/main/java/demo/Entity/CaseTreeVO.java

@ -0,0 +1,12 @@
package demo.Entity;
import lombok.Data;
@Data
public class CaseTreeVO {
private Integer id;
private String nodeName;
private Integer parentNodeId;
private String creator;
private String editor;
}

39
demo/demo/src/main/java/demo/Mapper/CaseTreeMapper.java

@ -0,0 +1,39 @@
package demo.Mapper;
import demo.Entity.CaseTree;
import org.apache.ibatis.annotations.*;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
@Mapper
public interface CaseTreeMapper {
//根据id查找是否存在
@Select("SELECT * FROM case_tree WHERE ID = #{id}")
CaseTree findByNodeId(@Param("id") int id);
//根据节点名称找id
@Select("SELECT * FROM case_tree WHERE NODE_NAME = #{nodeName}")
CaseTree findByNodeName(@Param("nodeName") String nodeName);
@Insert("INSERT INTO case_tree (NODE_NAME, ID_PATH, CREATOR, CREATE_TIME, EDITOR, UPDATE_TIME) VALUES (#{nodeName}, NULL, #{creator}, NOW(), #{creator}, NOW())")
void addNode(@Param("nodeName") String nodeName, @Param("creator") String creator);
@Update("UPDATE case_tree SET PARENT_NODE_ID = #{parentNodeId}, IS_LEAF = #{isLeaf} WHERE ID = #{id}")
void set(@Param("id") Integer id, @Param("parentNodeId") Integer parentNodeId, @Param("isLeaf") Integer isLeaf);
@Update("UPDATE case_tree SET IS_LEAF = #{isLeaf} WHERE ID = #{id}")
void setIsLeaf(@Param("id") Integer id, @Param("isLeaf") Integer isLeaf);
@Update("UPDATE case_tree SET ID_PATH = #{idPath} WHERE ID = #{id}")
void setPath(@Param("id") int id, @Param("idPath") String path);
@Update("UPDATE case_tree SET NODE_NAME = #{nodeName}, PARENT_NODE_ID = #{parentNodeId}, EDITOR = #{editor}, UPDATE_TIME = NOW() WHERE ID = #{id}")
void updateNode(@Param("id") int id, @Param("nodeName") String nodeName, @Param("parentNodeId") Integer parentNodeId, @Param("editor") String editor);
@Delete("DELETE FROM case_tree WHERE ID = #{id}")
void deleteNodeByNodeId(@Param("id") Integer id);
@Select("SELECT * FROM case_tree")
List<CaseTree> findAll();
}

8
demo/demo/src/main/java/demo/Mapper/RoleMapper.java

@ -11,8 +11,8 @@ import java.util.List;
@Mapper
public interface RoleMapper {
@Insert("insert into role(ROLE_NAME, ROLE_CODE, CREATE_TIME, UPDATE_TIME) values(#{roleName}, #{roleCode}, #{date}, #{date})")
void insert(@Param("roleName") String roleName, @Param("roleCode") String roleCode, @Param("date") Date date);
@Insert("insert into role(ROLE_NAME, ROLE_CODE, CREATE_TIME, UPDATE_TIME) values(#{roleName}, #{roleCode}, NOW(), NOW()")
void insert(@Param("roleName") String roleName, @Param("roleCode") String roleCode);
@Select("select * from role where ROLE_NAME = #{roleName}")
Role findByRoleName(@Param("roleName") String roleName);
@ -20,8 +20,8 @@ public interface RoleMapper {
@Delete("delete from role where ROLE_NAME = #{roleName}")
void deleteByRoleName(@Param("roleName") String roleName);
@Update("update role set ROLE_NAME = #{roleName}, ROLE_CODE = #{roleCode}, UPDATE_TIME = #{date} where ROLE_NAME = #{roleName}")
void update(@Param("roleName") String roleName, @Param("roleCode") String roleCode, @Param("date") Date date);
@Update("update role set ROLE_NAME = #{roleName}, ROLE_CODE = #{roleCode}, UPDATE_TIME = NOW() where ROLE_NAME = #{roleName}")
void update(@Param("roleName") String roleName, @Param("roleCode") String roleCode);
@Select("select * from role")
List<Role> findAll();

8
demo/demo/src/main/java/demo/Mapper/UserMapper.java

@ -13,8 +13,8 @@ public interface UserMapper {
@Select("SELECT * FROM user WHERE LOGIN_NAME = #{loginName} and PASSWORD = #{password}")
User findByMessage(String loginName, String password);
@Insert("INSERT INTO user (ID, LOGIN_NAME, USER_CHN_NAME, CREATE_TIME, UPDATE_TIME, PASSWORD) VALUES (NULL, #{loginName}, #{userChnName}, #{date}, #{date}, #{password})")
void register(@Param("loginName") String loginName, @Param("userChnName") String userChnName, @Param("date") Date date, @Param("password") String password);
@Insert("INSERT INTO user (ID, LOGIN_NAME, USER_CHN_NAME, CREATE_TIME, UPDATE_TIME, PASSWORD) VALUES (NULL, #{loginName}, #{userChnName}, NOW(), NOW(), #{password})")
void register(@Param("loginName") String loginName, @Param("userChnName") String userChnName, @Param("password") String password);
@Select("SELECT * FROM user WHERE LOGIN_NAME = #{loginName}")
User findByUsername(String loginName);
@ -22,8 +22,8 @@ public interface UserMapper {
@Delete("DELETE FROM user WHERE LOGIN_NAME = #{loginName}")
void deleteByUserName(String loginName);
@Update("UPDATE user SET USER_CHN_NAME = #{userChnName}, UPDATE_TIME = #{updateTime}, PASSWORD = #{password} WHERE LOGIN_NAME = #{loginName}")
void updateByUserName(@Param("loginName") String loginName, @Param("userChnName") String userChnName, @Param("updateTime") Date updateTime, @Param("password") String password);
@Update("UPDATE user SET USER_CHN_NAME = #{userChnName}, UPDATE_TIME = NOW(), PASSWORD = #{password} WHERE LOGIN_NAME = #{loginName}")
void updateByUserName(@Param("loginName") String loginName, @Param("userChnName") String userChnName, @Param("password") String password);
@Select("SELECT user.ID, user.LOGIN_NAME, user.USER_CHN_NAME, role.ROLE_NAME, role.ROLE_CODE FROM user, role, user_role_relation WHERE user.ID = user_role_relation.USER_ID and role.ID = user_role_relation.ROLE_ID")
List<UserDTO> findAll();

4
demo/demo/src/main/java/demo/Mapper/UserRoleMapper.java

@ -9,8 +9,8 @@ import java.util.List;
@Mapper
public interface UserRoleMapper {
@Insert("INSERT INTO user_role_relation (USER_ID, ROLE_ID, CREATE_TIME) values (#{userId}, #{roleId}, #{createTime})")
void insert(@Param("userId") int userId, @Param("roleId") int roleId, @Param("createTime") Date createTime);
@Insert("INSERT INTO user_role_relation (USER_ID, ROLE_ID, CREATE_TIME) values (#{userId}, #{roleId}, NOW())")
void insert(@Param("userId") int userId, @Param("roleId") int roleId);
@Delete("DELETE FROM user_role_relation WHERE USER_ID = #{userId}")
void delete(@Param("userId") int userId);

70
demo/demo/src/main/java/demo/Service/CaseInfoService.java

@ -0,0 +1,70 @@
package demo.Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@Service
public class CaseInfoService {
// @PersistenceContext
// private EntityManager entityManager;
//
// public void addCase(String caseName, String nodeId, String creator) {
// // 确认节点为叶子节点
// Optional<CaseTree> optionalNode = entityManager.createQuery(
// "SELECT n FROM CaseTree n WHERE n.id = :id", CaseTree.class)
// .setParameter("id", nodeId).getResultList().stream().findFirst();
//
// if (optionalNode.isPresent() && "1".equals(optionalNode.get().getIsLeaf())) {
// String id = generateNewId();
// CaseInfo newCase = new CaseInfo();
// newCase.setId(id);
// newCase.setCaseName(caseName);
// newCase.setNodeId(nodeId);
// newCase.setCreator(creator);
// newCase.setCreateTime(new Date());
// newCase.setEditor(creator);
// newCase.setUpdateTime(new Date());
//
// entityManager.persist(newCase);
// } else {
// throw new IllegalArgumentException("节点不是叶子节点,不能新增用例");
// }
// }
//
// @Transactional
// public void deleteCase(String id) {
// CaseInfo caseInfo = entityManager.find(CaseInfo.class, id);
// if (caseInfo != null) {
// entityManager.remove(caseInfo);
// }
// }
//
// public void updateCase(String id, String caseName, String editor) {
// Optional<CaseInfo> optionalCase = entityManager.createQuery(
// "SELECT c FROM CaseInfo c WHERE c.id = :id", CaseInfo.class)
// .setParameter("id", id).getResultList().stream().findFirst();
//
// optionalCase.ifPresent(caseInfo -> {
// caseInfo.setCaseName(caseName);
// caseInfo.setEditor(editor);
// caseInfo.setUpdateTime(new Date());
// entityManager.merge(caseInfo);
// });
// }
//
// public List<CaseInfo> getCases() {
// return entityManager.createQuery("FROM CaseInfo", CaseInfo.class).getResultList();
// }
//
// private String generateNewId() {
// // 实现生成唯一ID的逻辑
// return UUID.randomUUID().toString().replace("-", "");
// }
}

73
demo/demo/src/main/java/demo/Service/CaseTreeService.java

@ -0,0 +1,73 @@
package demo.Service;
import demo.Entity.CaseTree;
import demo.Entity.CaseTreeVO;
import demo.Mapper.CaseTreeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
@Service
public class CaseTreeService {
@Autowired
private CaseTreeMapper caseTreeMapper;
@Transactional(rollbackOn = Exception.class)
public void addNode(String nodeName, Integer parentNodeId, String creator) {
if(parentNodeId == null) {
caseTreeMapper.addNode(nodeName, creator);
CaseTree caseTree = caseTreeMapper.findByNodeName(nodeName);
caseTreeMapper.set(caseTree.getId(), caseTree.getId(), 0);
caseTreeMapper.setPath(caseTree.getId(), caseTree.getId() + "");
}
else {
caseTreeMapper.addNode(nodeName, creator);
CaseTree caseTree = caseTreeMapper.findByNodeName(nodeName);
caseTreeMapper.set(caseTree.getId(), parentNodeId, 0);
CaseTree parentNode = caseTreeMapper.findByNodeId(parentNodeId);
caseTreeMapper.setIsLeaf(parentNodeId, parentNode.getIsLeaf() + 1);
caseTreeMapper.setPath(caseTree.getId(), parentNode.getIdPath() + "/" + caseTree.getId());
}
}
public CaseTree findByNodeId(Integer id){
return caseTreeMapper.findByNodeId(id);
}
public CaseTree findByNodeName(String nodeName) {
return caseTreeMapper.findByNodeName(nodeName);
}
@Transactional(rollbackOn = Exception.class)
public void deleteNode(CaseTree caseTree){
caseTreeMapper.deleteNodeByNodeId(caseTree.getId());
CaseTree parentNode = caseTreeMapper.findByNodeId(caseTree.getParentNodeId());
if(parentNode != null) {
caseTreeMapper.setIsLeaf(caseTree.getParentNodeId(), parentNode.getIsLeaf() - 1);
}
}
@Transactional(rollbackOn = Exception.class)
public void updateNode(CaseTreeVO caseTreeVO) {
caseTreeMapper.updateNode(caseTreeVO.getId(), caseTreeVO.getNodeName(), caseTreeVO.getParentNodeId(), caseTreeVO.getEditor());
caseTreeMapper.set(caseTreeVO.getId(), caseTreeVO.getParentNodeId(), 0);
CaseTree parentNode = caseTreeMapper.findByNodeId(caseTreeVO.getParentNodeId());
caseTreeMapper.setIsLeaf(caseTreeVO.getParentNodeId(), parentNode.getIsLeaf() + 1);
if(parentNode.getIsLeaf() > 0)
caseTreeMapper.setPath(caseTreeVO.getId(), parentNode.getIdPath() + "/" + caseTreeVO.getId());
else
caseTreeMapper.setPath(caseTreeVO.getId(), caseTreeVO.getId() + "");
}
public List<CaseTree> query() {
return caseTreeMapper.findAll();
}
//
// private String generateNewId() {
// // 实现生成唯一ID的逻辑
// return UUID.randomUUID().toString().replace("-", "");
// }
}

8
demo/demo/src/main/java/demo/Service/RoleService.java

@ -19,8 +19,8 @@ public class RoleService {
@Autowired
private RoleMapper roleMapper;
public void insert(String roleName, String roleCode, Date date){
roleMapper.insert(roleName, roleCode, date);
public void insert(String roleName, String roleCode){
roleMapper.insert(roleName, roleCode);
}
public Role findByRoleName(String roleName){
@ -31,8 +31,8 @@ public class RoleService {
roleMapper.deleteByRoleName(roleName);
}
public void update(RoleVO roleVO, Date date){
roleMapper.update(roleVO.getRoleName(), roleVO.getRoleCode(), date);
public void update(RoleVO roleVO){
roleMapper.update(roleVO.getRoleName(), roleVO.getRoleCode());
}
public List<Role> findAll(){

8
demo/demo/src/main/java/demo/Service/UserRoleService.java

@ -25,13 +25,7 @@ public class UserRoleService {
private UserRoleMapper userroleMapper;
public void insert(int userId, int roleId) {
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// 将Instant转换为Date
Date date = Date.from(instant);
userroleMapper.insert(userId, roleId, date);
userroleMapper.insert(userId, roleId);
}
public void delete(int userId) {

12
demo/demo/src/main/java/demo/Service/UserService.java

@ -24,13 +24,7 @@ public class UserService {
}
public void register(String loginName, String UserChnName, String password){
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// 将Instant转换为Date
Date date = Date.from(instant);
userMapper.register(loginName, UserChnName, date, password);
userMapper.register(loginName, UserChnName, password);
}
public User findByUserName(String loginName){
@ -41,8 +35,8 @@ public class UserService {
userMapper.deleteByUserName(loginName);
}
public void updateByUserName(String loginName, String userChnName, Date updateTime, String password){
userMapper.updateByUserName(loginName, userChnName, updateTime, password);
public void updateByUserName(String loginName, String userChnName, String password){
userMapper.updateByUserName(loginName, userChnName, password);
}
public List<UserDTO> query(){

Loading…
Cancel
Save