spring-boot-data-jpa操作mysql数据库的增删改查

直接上代码
表映射实体:

@Entity
@Table(name = "manager_labeling_tasks")
public class LabelingTasks {
    /**
     * 在数据库生成一张表来管理主键生成策略
     */
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE,generator = "id_generator")
    @TableGenerator(name = "id_generator",
            table = "id_generator",//表名
            pkColumnName = "pk_name",//生成存储ID的键
            valueColumnName = "pk_value",//存储ID值
            pkColumnValue = "extract_task_id",//列1的键值
            initialValue = 10000,//初始值
            allocationSize = 1)//增长值
    private Integer id;
    //任务id
    @Column(nullable = false)
    private String taskId;
    //任务名称
    @Column(nullable = false)
    private String taskName;
    //任务类型
    @Column(nullable = false)
    private String taskType;
    //标注数据
    @Column(nullable = false,columnDefinition = "TEXT")
    private String labelingData;
    //数据工程
    @Column(nullable = false)
    private String dataEngineering;
    //标注小组
    @Column(nullable = false)
    private String labelingGroup;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public String getLabelingData() {
        return labelingData;
    }

    public void setLabelingData(String labelingData) {
        this.labelingData = labelingData;
    }

    public String getDataEngineering() {
        return dataEngineering;
    }

    public void setDataEngineering(String dataEngineering) {
        this.dataEngineering = dataEngineering;
    }

    public String getLabelingGroup() {
        return labelingGroup;
    }

    public void setLabelingGroup(String labelingGroup) {
        this.labelingGroup = labelingGroup;
    }

    public LabelingTasks() {
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
}

Contrller类:

@Controller
@RequestMapping("/admin/labeling")
public class ManagerLabelingTaskController {

    @Autowired
    private ManagerLabelingTaskService managerLabelingTaskService;

    @RequestMapping("/save")
    @ResponseBody
    public LabelingTasks saveLabelingTasks(@RequestParam("taskName") String taskName,
                                           @RequestParam("taskType") String taskType,
                                           @RequestParam("labelingData") String labelingData,
                                           @RequestParam("dataEngineering") String dataEngineering,
                                           @RequestParam("labelingGroup") String labelingGroup){
        LabelingTasks labelingTasks = new LabelingTasks();

        final String taskId = UUID.randomUUID().toString();
        labelingTasks.setTaskId(taskId);
        labelingTasks.setTaskName(taskName);
        labelingTasks.setTaskType(taskType);
        labelingTasks.setLabelingData(labelingData);
        labelingTasks.setDataEngineering(dataEngineering);
        labelingTasks.setLabelingGroup(labelingGroup);

        return managerLabelingTaskService.saveAndFlush(labelingTasks);
    }

    /**
     * 查看
     */
    @RequestMapping("/check")
    @ResponseBody
    public LabelingTasks CheckLabelingTasks(@RequestParam("taskId") String taskId){
        LabelingTasks labelingTasks = managerLabelingTaskService.findByTaskId(taskId);
        return labelingTasks;
    }

    /**
     * 编辑
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Integer EditLabelingTasks(@Param("id") Integer id,
                                 @Param("taskName") String taskName,
                                 @Param("taskType") String taskType,
                                 @Param("labelingData") String labelingData,
                                 @Param("dataEngineering") String dataEngineering,
                                 @Param("labelingGroup") String labelingGroup,
                                 @Param("taskId") String taskId){

        int row = managerLabelingTaskService.updateTask(id, taskName, taskType, labelingData,
                                                        dataEngineering, labelingGroup, taskId);
        return row;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public void DeleteLabelingTasks(@RequestParam("id") Integer id){
        managerLabelingTaskService.deleteById(id);
    }
}

service类:

public interface ManagerLabelingTaskService extends JpaRepository {
    //根据任务id查询任务信息
    LabelingTasks findByTaskId(String taskId);
    //修改已经存在的任务信息
    @Modifying
    @Transactional
    @Query(value = "update manager_labeling_tasks set id=:id,task_name=:taskName,task_name=:taskType,labeling_data=:labelingData,data_engineering=:dataEngineering,labeling_group=:labelingGroup where task_id=:taskId",nativeQuery = true)
    int updateTask(@Param("id") Integer id,
                   @Param("taskName") String taskName,
                   @Param("taskType") String taskType,
                   @Param("labelingData") String labelingData,
                   @Param("dataEngineering") String dataEngineering,
                   @Param("labelingGroup") String labelingGroup,
                   @Param("taskId") String taskId);
}   

配置文件application.yaml:

spring:
  freemarker:
    template-loader-path: classpath:/templates/
    prefer-file-system-access: true
    suffix: .ftl
    request-context-attribute: ctx
    charset: UTF-8
    check-template-location: true
  aop:
      auto: true
      proxy-target-class: false
  datasource:
#    url: jdbc:mysql://localhost:3306/dataexa-labeling?useUnicode=true&characterEncoding=UTF-8
#    username: root
#    password: 123456
    url: jdbc:mysql://localhost:3306/dataexa-labeling?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
      database-platform: org.hibernate.dialect.MySQL5Dialect
      hibernate:
        ddl-auto: update
        use-new-id-generator-mappings: true
      show-sql: false

server:
  port: 8080

python:
  #Python脚本的存放目录
  #extractDataWorkHome: /home/yuanting/桌面/TextRank4ZH-master
  #服务器192.168.1.61的Python脚本的存放目录
  extractDataWorkHome: /usr/qa/python/TextRank4ZH-master

  #python数据提取服务的端口号
  extractDataPort: 5052

你可能感兴趣的:(spring-boot-data-jpa操作mysql数据库的增删改查)