Java示例(spring)

java动态生成初始化数据(spring框架)

示例提供来源:香喷喷的如歌

对应控制器 controllers/IndexController.java

  1. @RestController
  2. @RequestMapping("login")
  3. public class LoginController {
  4. @Resource
  5. private SysMenuService sysMenuService;
  6. @GetMapping("/menu")
  7. public Map<String, Object> menu() {
  8. return sysMenuService.menu();
  9. }
  10. }

对应Service逻辑层 service/SysLoginServiceImpl.java

  1. @Service
  2. public class SysMenuServiceImpl implements SysMenuService {
  3. @Resource
  4. private SysMenuRepository sysMenuRepository;
  5. @Override
  6. public Map<String, Object> menu() {
  7. Map<String, Object> map = new HashMap<>(16);
  8. Map<String,Object> home = new HashMap<>(16);
  9. Map<String,Object> logo = new HashMap<>(16);
  10. List<SysMenu> menuList = sysMenuRepository.findAllByStatusOrderBySort(true);
  11. List<MenuVo> menuInfo = new ArrayList<>();
  12. for (SysMenu e : menuList) {
  13. MenuVo menuVO = new MenuVo();
  14. menuVO.setId(e.getKey().getId());
  15. menuVO.setPid(e.getPid());
  16. menuVO.setHref(e.getKey().getHref());
  17. menuVO.setTitle(e.getKey().getTitle());
  18. menuVO.setIcon(e.getIcon());
  19. menuVO.setTarget(e.getTarget());
  20. menuInfo.add(menuVO);
  21. }
  22. map.put("menuInfo", TreeUtil.toTree(menuInfo, 0L));
  23. home.put("title","首页");
  24. home.put("href","/page/welcome-1");//控制器路由,自行定义
  25. logo.put("title","后台管理系统");
  26. logo.put("image","/static/images/back.jpg");//静态资源文件路径,可使用默认的logo.png
  27. map.put("homeInfo", "{title: '首页',href: '/ruge-web-admin/page/welcome.html'}}");
  28. map.put("logoInfo", "{title: 'RUGE ADMIN',image: 'images/logo.png'}");
  29. return map;
  30. }
  31. }

TreeUtilutil/TreeUtil.java

  1. public class TreeUtil {
  2. public static List<MenuVo> toTree(List<MenuVo> treeList, Long pid) {
  3. List<MenuVo> retList = new ArrayList<MenuVo>();
  4. for (MenuVo parent : treeList) {
  5. if (pid.equals(parent.getPid())) {
  6. retList.add(findChildren(parent, treeList));
  7. }
  8. }
  9. return retList;
  10. }
  11. private static MenuVo findChildren(MenuVo parent, List<MenuVo> treeList) {
  12. for (MenuVo child : treeList) {
  13. if (parent.getId().equals(child.getPid())) {
  14. if (parent.getChild() == null) {
  15. parent.setChild(new ArrayList<>());
  16. }
  17. parent.getChild().add(findChildren(child, treeList));
  18. }
  19. }
  20. return parent;
  21. }
  22. }

repository层 reposiroty/SysMenuRepository.java

  1. public interface SysMenuRepository extends JpaRepository<SysMenu, Long> {
  2. //这里我只查询页面转态为启用,可自行定义和写
  3. @Query(value = "select * from permission_security.system_menu where STATUS = 1 ORDER BY sort ",nativeQuery = true)
  4. public List<SystemMenu> getSystemMenuByStatusAndSort(Long status,Integer sort);
  5. List<SysMenu> findAllByStatusOrderBySort(Boolean status);
  6. }

entity层 entity/MenuEntity.java

  1. @Getter
  2. @Setter
  3. @Embeddable
  4. public class MenuKey implements Serializable {
  5. private Long id;
  6. private String title;
  7. private String href;
  8. }
  9. @Getter
  10. @Setter
  11. @Entity
  12. @Table(name = "system_menu")
  13. public class SysMenu implements Serializable {
  14. // 复合主键要用这个注解
  15. @EmbeddedId
  16. private MenuKey key;
  17. private Long pid;
  18. private String icon;
  19. private String target;
  20. private Integer sort;
  21. private Boolean status;
  22. private String remark;
  23. @CreatedDate
  24. private Date create_at;
  25. @CreatedDate
  26. private Date update_at;
  27. private Date delete_at;
  28. }
  29. @Data
  30. @JsonInclude(JsonInclude.Include.NON_NULL)
  31. public class MenuVo {
  32. private Long id;
  33. private Long pid;
  34. private String title;
  35. private String icon;
  36. private String href;
  37. private String target;
  38. private List<MenuVo> child;
  39. }
  40. //这里是常规写法不使用lombok
  41. /**
  42. * insert和update注解,解决新增和更新的时候没有使用默认值
  43. * @Author wupeng
  44. * @Date 2020-04-25
  45. * @Description name ="表" 删掉permission_security. schema=""可删掉不必跟我,前提是你的表对应你的表空间就是数据库
  46. */
  47. @Entity
  48. @Table ( name ="permission_security.system_menu" , schema = "root")
  49. @DynamicInsert
  50. @DynamicUpdate
  51. public class SystemMenu implements Serializable {
  52. private static final long serialVersionUID = 5421757630121636006L;
  53. /**复合主键要用这个注解*/
  54. @EmbeddedId
  55. private MenuKey key;
  56. /**
  57. * 父ID
  58. */
  59. @Column(name = "pid" )
  60. private Long pid;
  61. /**
  62. * 菜单图标
  63. */
  64. @Column(name = "icon")
  65. private String icon;
  66. /**
  67. * 链接打开方式
  68. */
  69. @Column(name = "target",columnDefinition = "_self")
  70. private String target;
  71. /**
  72. * 菜单排序
  73. */
  74. @Column(name = "sort" )
  75. private Long sort;
  76. /**
  77. * 状态(0:禁用,1:启用)
  78. */
  79. @Column(name = "status",columnDefinition = "tinyint DEFAULT 1")
  80. private Integer status;
  81. /**
  82. * 备注信息
  83. */
  84. @Column(name = "remark" )
  85. private String remark;
  86. /**
  87. * 创建时间
  88. */
  89. @Column(name = "create_at" )
  90. private Date createAt;
  91. /**
  92. * 更新时间
  93. */
  94. @Column(name = "update_at" )
  95. private Date updateAt;
  96. /**
  97. * 删除时间
  98. */
  99. @Column(name = "delete_at" )
  100. private Date deleteAt;
  101. /* public Long getId() {
  102. return this.id;
  103. }
  104. public void setId(Long id) {
  105. this.id = id;
  106. }*/
  107. public Long getPid() {
  108. return this.pid;
  109. }
  110. public void setPid(Long pid) {
  111. this.pid = pid;
  112. }
  113. public String getIcon() {
  114. return this.icon;
  115. }
  116. public void setIcon(String icon) {
  117. this.icon = icon;
  118. }
  119. public String getTarget() {
  120. return this.target;
  121. }
  122. public void setTarget(String target) {
  123. this.target = target;
  124. }
  125. public Long getSort() {
  126. return this.sort;
  127. }
  128. public void setSort(Long sort) {
  129. this.sort = sort;
  130. }
  131. public Integer getStatus() {
  132. return this.status;
  133. }
  134. public void setStatus(Integer status) {
  135. this.status = status;
  136. }
  137. public String getRemark() {
  138. return this.remark;
  139. }
  140. public void setRemark(String remark) {
  141. this.remark = remark;
  142. }
  143. public Date getCreateAt() {
  144. return this.createAt;
  145. }
  146. public void setCreateAt(Date createAt) {
  147. this.createAt = createAt;
  148. }
  149. public Date getUpdateAt() {
  150. return this.updateAt;
  151. }
  152. public void setUpdateAt(Date updateAt) {
  153. this.updateAt = updateAt;
  154. }
  155. public Date getDeleteAt() {
  156. return this.deleteAt;
  157. }
  158. public void setDeleteAt(Date deleteAt) {
  159. this.deleteAt = deleteAt;
  160. }
  161. public MenuKey getKey() {
  162. return key;
  163. }
  164. public void setKey(MenuKey key) {
  165. this.key = key;
  166. }
  167. }
  168. @Embeddable
  169. public class MenuKey implements Serializable {
  170. @GeneratedValue(strategy = GenerationType.IDENTITY)
  171. @Column(name = "id")
  172. private Long id;
  173. /**
  174. * 名称
  175. */
  176. @Column(name = "title")
  177. private String title;
  178. /**
  179. * 链接
  180. */
  181. @Column(name = "href")
  182. private String href;
  183. public Long getId() {
  184. return id;
  185. }
  186. public void setId(Long id) {
  187. this.id = id;
  188. }
  189. public String getTitle() {
  190. return title;
  191. }
  192. public void setTitle(String title) {
  193. this.title = title;
  194. }
  195. public String getHref() {
  196. return href;
  197. }
  198. public void setHref(String href) {
  199. this.href = href;
  200. }
  201. }
  202. @JsonInclude(JsonInclude.Include.NON_NULL)
  203. public class MenuVo {
  204. private Long id;
  205. private Long pid;
  206. private String title;
  207. private String icon;
  208. private String href;
  209. private String target;
  210. public Long getId() {
  211. return id;
  212. }
  213. public void setId(Long id) {
  214. this.id = id;
  215. }
  216. public Long getPid() {
  217. return pid;
  218. }
  219. public void setPid(Long pid) {
  220. this.pid = pid;
  221. }
  222. public String getTitle() {
  223. return title;
  224. }
  225. public void setTitle(String title) {
  226. this.title = title;
  227. }
  228. public String getIcon() {
  229. return icon;
  230. }
  231. public void setIcon(String icon) {
  232. this.icon = icon;
  233. }
  234. public String getHref() {
  235. return href;
  236. }
  237. public void setHref(String href) {
  238. this.href = href;
  239. }
  240. public String getTarget() {
  241. return target;
  242. }
  243. public void setTarget(String target) {
  244. this.target = target;
  245. }
  246. public List<MenuVo> getChild() {
  247. return child;
  248. }
  249. public void setChild(List<MenuVo> child) {
  250. this.child = child;
  251. }
  252. private List<MenuVo> child;
  253. }

yml配置文件 resource/application.yml

  1. #本地开发环境配置中心
  2. spring:
  3. application:
  4. name: springboot-webAdmin
  5. jpa:
  6. show-sql: true
  7. database: mysql
  8. #generate-ddl: true
  9. database-platform: org.hibernate.dialect.MySQL5Dialect
  10. hibernate:
  11. naming:
  12. #解决使用其他库的表时候,把小数点变成下划线,导致sql无法成功执行。
  13. #这是由于物理命名策略引起的,大写字母变小写,加_下划线(hibernate5以上高版本)
  14. physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  15. #ddl-auto: update
  16. #ddl-auto: update
  17. datasource:
  18. url: jdbc:mysql://127.0.0.1:3306/数据库名称?characterEncoding=utf-8&serverTimezone=GMT%2B8
  19. username: root
  20. password: 123456
  21. driver-class-name: com.mysql.jdbc.Driver
  22. type: com.zaxxer.hikari.HikariDataSource
  23. hikari:
  24. auto-commit: true
  25. minimum-idle: 2
  26. idle-timeout: 60000
  27. connection-timeout: 30000
  28. max-lifetime: 1800000
  29. pool-name: DatebookHikariCP
  30. #thymeleaf模板配置
  31. thymeleaf:
  32. cache: false
  33. enabled: true
  34. prefix: classpath:/templates/
  35. suffix: .html
  36. #严格执行H5标准
  37. mode: LEGACYHTML5
  38. encoding: UTF-8
  39. servlet:
  40. content-type: text/html
  41. #content-type: text/html
  42. resources:
  43. chain:
  44. strategy:
  45. content:
  46. enabled: true
  47. paths: /**
  48. #静态资源路径
  49. mvc:
  50. static-path-pattern: /static/**
  51. #关掉原生icon图标
  52. favicon:
  53. enabled: false
  54. #项目端口
  55. server:
  56. port: 8080
  57. #连接超时,单位为毫秒,-1永不超时
  58. connection-timeout: 60000
  59. #设置tomcat参数
  60. tomcat:
  61. uri-encoding: utf-8
  62. max-connections: 10000
  63. min-spare-threads: 10
  64. #最大220个并发,可以达到不丢包(可以自己实测),默认为200。
  65. max-threads: 220
  66. #配置访问路径,默认为/
  67. #servlet:
  68. #context-path: /index/main
  69. #配置日志文件参数
  70. logging:
  71. file:
  72. path: F:/myLog/adminLog.log
  73. level:
  74. org:
  75. springframework: debug
  76. hibernate: debug

© zhongshaofa all right reserved,powered by Gitbook文件修订时间: 2021-04-06 22:10:57

results matching ""

No results matching ""

results matching ""

No results matching ""