当前位置:   article > 正文

springboot+gradle项目_springboot集成gradle

springboot集成gradle

1 首先创建一个数据表实力user

他是一个复合对像(一般直接如下操作不推荐)

  1. //需要配置
  2. /*# mybatis
  3. mybatis.configuration.map-underscore-to-camel-case: true
  4. spring.jackson.date-format: yyyy-MM-dd HH:mm:ss
  5. spring.jackson.time-zone: GMT+8*/
  6. //学生类
  7. @Data
  8. @Builder
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. public class MonitorPanelDO {
  12. private Long id;
  13. @NotBlank(message = "姓名不能为空")
  14. @Length(max = 64, message = "名称太长")
  15. private String name;
  16. @NotNull(message = "性别不能为空")
  17. private Integer age;
  18. private Integer updateSeq; //防止重复覆盖
  19. private Boolean removed; //是否离校
  20. @NotBlank(message = "年级不能为空")
  21. private String class;
  22. @NotNull(message = "学科不能为空")
  23. @Valid
  24. private List<Subject> subjects;
  25. private String description;
  26. private Timestamp creationTime;
  27. private String creationName;
  28. private Timestamp updateTime;
  29. private String updateName;
  30. }
  31. //学科类
  32. @Data
  33. @Builder
  34. @AllArgsConstructor
  35. @NoArgsConstructor
  36. public class MonitorPanelDO {
  37. private Long id;
  38. @NotBlank(message = "学科名不能为空")
  39. @Length(max = 64, message = "名称太长")
  40. private String name;
  41. @NotNull(message = "成绩不能为空")
  42. private Double grade;
  43. @NotBlank(message = "任课老师名不能为空")
  44. private String teacherName;
  45. private Timestamp creationTime;
  46. private String creationName;
  47. }

Dao

  1. //实体类包含List<Class> 需要用BaseTypeHandler进行处理
  2. public class SubjectHandler extends BaseTypeHandler<List<Subject>> {
  3. @Override
  4. public void setNonNullParameter(PreparedStatement ps, int i, List<Subject> parameter, JdbcType jdbcType) throws SQLException {
  5. ps.setString(i, new Gson().toJson(parameter));
  6. }
  7. @Override
  8. public List<Subject> getNullableResult(ResultSet rs, String columnName) throws SQLException {
  9. String json = rs.getString(columnName);
  10. if (!Strings.isNullOrEmpty(json)) {
  11. return new Gson().fromJson(json, new TypeToken<List<Subject>>() {}.getType());
  12. }
  13. return null;
  14. }
  15. @Override
  16. public List<Subject> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  17. String json = rs.getString(columnIndex);
  18. if (!Strings.isNullOrEmpty(json)) {
  19. return new Gson().fromJson(json, new TypeToken<List<Subject>>() {}.getType());
  20. }
  21. return null;
  22. }
  23. @Override
  24. public List<Subject> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  25. String json = cs.getString(columnIndex);
  26. if (!Strings.isNullOrEmpty(json)) {
  27. return new Gson().fromJson(json,new TypeToken<List<Subject>>() {}.getType());
  28. }
  29. return null;
  30. }
  31. }
  32. //
  33. @Mapper
  34. public interface IPanelDao {
  35. @Select("<script> " +
  36. "SELECT * FROM User WHERE id = #{id} " +
  37. "</script>")
  38. @Results({
  39. @Result(property = "allSubject", column = "all_subject",
  40. jdbcType = JdbcType.VARCHAR,
  41. typeHandler = SubjectHandler.class)
  42. })
  43. User getById(Long id);
  44. @Select("<script>" +
  45. "SELECT * FROM User " +
  46. "WHERE 1 = 1 " +
  47. "<if test='ids != null'> " +
  48. " AND id not in " +
  49. " <foreach close=')' collection='ids' index='index' item='id' open='(' separator=','> " +
  50. " #{id}" +
  51. "</foreach> " +
  52. "</if> " +
  53. "<if test='keyword != null'> " +
  54. "AND ( " +
  55. "name LIKE concat('%',#{keyword},'%') " +
  56. "OR id LIKE concat(#{keyword},'%') " +
  57. "OR description LIKE concat('%',#{keyword},'%') " +
  58. ") " +
  59. "</if> " +
  60. "ORDER BY creation_time desc " +
  61. "</script>")
  62. @Results({
  63. @Result(property = "allSubject", column = "all_subject",
  64. jdbcType = JdbcType.VARCHAR,
  65. typeHandler = SubjectHandler.class)
  66. })
  67. List<User> getBasePanelByKeyword(@Param("keyword") String keyword, @Param("ids") List<Long> ids);
  68. @Insert({"INSERT INTO User (name, age,class,allSubject,description, creation_time, creation_name,update_time,update_name) " +
  69. "VALUES (#{name},#{age}, #{class}, '+
  70. "#{allSubject, javaType=com.ht.info.model.dto.Subject, typeHandler= com.ht.info.model.dao.handler.SubjectHandler}, " +
  71. " #{description}, #{creationTime}, #{creationName}, #{updateTime},#{updateName})"})
  72. @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
  73. @Results({
  74. @Result(property = "allSubject", column = "all_subject",
  75. jdbcType = JdbcType.VARCHAR,
  76. typeHandler = SubjectHandler.class)
  77. })
  78. Long addUser(User user);
  79. @Update("UPDATE User SET name = #{name},age= #{age},class = #{class},update_seq = #{updateSeq} + 1, " +
  80. "allSubject = #{allSubject, javaType=com.ht.info.model.dto.Subject, typeHandler= com.ht.info.model.dao.handler.SubjectHandler}, " +
  81. " description = #{description}, update_time = #{updateTime}, update_name = #{updateName} " +
  82. "WHERE id = #{id} AND update_seq = #{updateSeq}")
  83. @Results({
  84. @Result(property = "allSubject", column = "all_subject",
  85. jdbcType = JdbcType.VARCHAR,
  86. typeHandler = SubjectHandler.class)
  87. })
  88. int updateUser(User user);
  89. @Delete("DELETE FROM User WHERE id = #{id} and removed = true")
  90. int deleteById(Long Id);
  91. }

service层

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class PageQuery {
  6. private String keyword;
  7. private Integer pageNo;
  8. private Integer pageSize;
  9. private Long startTime;
  10. private Long endTime;
  11. private Timestamp startTimestamp;
  12. private Timestamp endTimestamp;
  13. public PageQuery(String keyword, Integer pageNo, Integer pageSize) {
  14. this.keyword = keyword;
  15. this.pageNo = pageNo;
  16. this.pageSize = pageSize;
  17. }
  18. public void setStartTime(Long startTime) {
  19. if (startTime != null) {
  20. this.startTimestamp = new Timestamp(startTime);
  21. }
  22. }
  23. public void setEndTime(Long endTime) {
  24. if (endTime != null) {
  25. this.endTimestamp = new Timestamp(endTime);
  26. }
  27. }
  28. }
  29. //需要配置分页插件# 分页配置
  30. /*pagehelper:
  31. helperDialect: mysql
  32. offsetAsPageNum: true
  33. rowBoundsWithCount: true
  34. reasonable: true
  35. returnPageInfo: true
  36. */
  37. @Service
  38. public class UserService implements IUserService {
  39. @Autowired
  40. private UserDao userDao;
  41. @Override
  42. public PageBean<User> findAdminByPage(PageQuery query) {
  43. Integer currentPage = query.getPageNo();
  44. Integer pageSize = query.getPageSize();
  45. Page<User> page = PageHelper.startPage(currentPage, pageSize);
  46. List<User> adminDOList =userDao.searchList(query.getKeyword());
  47. PageBean<User> pageBean = new PageBean<>(currentPage, pageSize, page.getTotal());
  48. pageBean.setItems(adminDOList);
  49. return pageBean;
  50. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/305763
推荐阅读
相关标签
  

闽ICP备14008679号