CRUD 正确姿势

创建 实体类

Student.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@SuperBuilder
@Table(value = "t_student", comment = "学生表")
public class Student extends BaseEntity {

    String name;
}

DAO

StudentDAO.java

@Repository
public class StudentDAO extends EntityDAOImpl<Student, Long> {

}

Service

StudentService.java

@Service
public class StudentService extends BaseServiceImpl<StudentDAO, Student> {

    public StudentService(StudentDAO baseDAO) {
        super(baseDAO);
    }
}

Controller 类

StudentController.java

@Controller
@RequestMapping("students")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class StudentController extends BaseFormController<Student, StudentService> {

    public StudentController(StudentService studentService) {
        super(studentService, "demos/student/edit");
    }
}

生成表

tableGenerator.createTable(Student.class);

生成 Report

  @Test
  public void testReport() {
      reportService.saveOrUpdate(Report.builder()
              .code("t_student")//  建议和数据库表名保持一致
              .tplName("demos/student/list") // 拷贝模版页面到指定目录
//                .tplName("tpl/list") // 没有特殊要求使用模版页面
//                .tplName("tpl/ajax_list") // 没有特殊要求使用模版页面
              .name("学生信息")
                      .additionalInfo(Params.builder(1).pv("operator-bar", true) // 显示操作按钮
                              .pv("endpoint", "students")
                              .build()) // 显示操作按钮
              .querySql("select id, name, create_time from t_student where name like :name and is_deleted = 0")
              .queryFieldList(Arrays.asList(
                      new QueryField("name", "姓名")
              ))
              .reportColumnList(Arrays.asList(
                      new HiddenReportColumn("id"),
                      new ReportColumn("name", "名称", true),
                      new ReportColumn("create_time", "创建时间", false,null, Arrays.asList("localDateTimeConverter"))
              ))
              .pageable(true)
              .sidx("create_time")
              .sord(SordEnum.DESC)
              .build());
  }

获取 report id = 858412707060166656

配置 index.html 菜单

<li class="nav-item">
    <a class="nav-link" href="javascript:addTab('student', '测试student', '/reports/858412707060166656')">
        <i class="nav-icon icon-home"></i> 测试student
    </a>
</li>

查看list页面

添加Form

拷贝 tpl/edit.html

$.ajax ({
   url: "/endpoint", // 修改一处,改成自己的 endpoint
         type: "post",
         data: JSON.stringify(formData),
         dataType: "json",
         contentType: "application/json; charset=utf-8",
         success: function(data){
               if (!data.success) {
                   toastr.error(data.message)
               } else {
                   idDOM.value = data.id
                   toastr.success("保存成功")
               }
           }
     });

查看edit页面