若依框架--代碼生成器實踐

一、新建表結構

drop table if exists sys_student;
create table sys_student (
  student_id           int(11)         auto_increment    comment '編號',
  student_name         varchar(30)     default ''        comment '學生名稱',
  student_age          int(3)          default null      comment '年齡',
  student_sex          char(1)         default '0'       comment '性別(0男 1女 2未知)',
  student_status       char(1)         default '0'       comment '狀態(0正常 1停用)',
  student_birthday     datetime                          comment '生日',
  remark               varchar(500)    default null      comment '備註',
  primary key (student_id)
) engine=innodb auto_increment=1 comment = '學生信息表';

二、登錄系統(系統工具 -> 代碼生成 -> 導入對應表)

代碼生成1

三、代碼生成列表中找到需要表(可預覽、修改、刪除生成配置)

代碼生成2

四、解壓生成的壓縮包,將代碼文件放到項目對應的位置。執行生成的SQL文件,會在系統工具目錄生成新的菜單。

1.java代碼放置

java代碼的位置
Controller層代碼例外,需單獨放到admin模塊的system下
controller放到如下圖的位置
controller例外,放到這裏

2.前端代碼放置

前端代碼
放置

五、更改前端頁面代碼

1.添加頁面下拉框:

<select name="studentSex" class="form-control m-b">
	<option value="">所有</option>
</select>

改爲:

<select name="studentSex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
	<option value="">所有</option>
	<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>

2.展示頁顯示正確的字典值:

  • <Script>標籤裏添加這句以獲取字典數據:
    var studentSex = [[${@dict.getType('sys_user_sex')}]];
  • JQuery函數裏的colums字段裏的對應位置添加以下這句以格式化數據字典:
formatter: function(value, row, index) {
	return $.table.selectDictLabel(studentSex, value);
}

3.編輯頁面的性別數據回顯:

  • 先在<select>標籤里加:th:with="type=${@dict.getType('sys_user_sex')}"獲取對應的字典值
  • 然後在<option>里加:th:field="*{studentSex}"選中對應的下拉框
<option th:field="*{studentSex}" th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
  • 或者在<option>里加:th:selected="*{studentSex}==${dict.dictValue}"選中對應的下拉框
<option th:selected="*{studentSex}==${dict.dictValue}" th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章