Next development project management, is a very common function: list page, query, add, edit, delete temporarily do not do.
First to do the function of the new project, first to implement the back-end interface.
First, write the entity class Project
Defines attributes of the entity class that correspond to fields in the corresponding tables in the database.
package com.pingguo.bloomtest.pojo; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import java.util.Date; @Data public class Project { @TableId(type = IdType.AUTO) private Long id; private String projectName; private String description; @tableField (fill = FieldFill.INSERT) private Date createTime; @tableField (fill = FieldFill.INSERT_UPDATE) // Fill data when adding or modifying data private Date updateTime; private String createUser; }Copy the code
Create a table
CREATE TABLE 'project' (' id 'bigint NOT NULL AUTO_INCREMENT COMMENT '主键 iD ', 'projectName' varchar(30) DEFAULT NULL COMMENT 'project ',' description 'varchar(255) DEFAULT NULL COMMENT' description ', 'createTime' datetime NOT NULL DEFAULT '1900-01-01 00:00:00' COMMENT 'createTime ', 'updateTime' datetime NOT NULL DEFAULT '1900-01-01 00:00:00' COMMENT 'updateTime ', 'createUser' varchar(30) DEFAULT NULL COMMENT 'createUser ', PRIMARY KEY (' id ') ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT=' table ';Copy the code
Modify the configuration in application.properties
# mybatis-plus, turn off automatic transformation of the camel and underscore mybatis-plus.configuration. Map -underscore-to-camel-case=falseCopy the code
Here add a mybatis-plus configuration, default true, set to false.
- True: For example, the attribute projectName in the Project class, and the field name mapped to the table is project_name.
- False: Turn off autoconversion of hump and underline. ProjectName is mapped to the table as projectName.
Write the ProjectDAO interface
package com.pingguo.bloomtest.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pingguo.bloomtest.pojo.Project;
import org.springframework.stereotype.Repository;
@Repository
public interface ProjectDAO extends BaseMapper<Project> {
}
Copy the code
Write the ProjectService class
Here, you implement methods to add items.
package com.pingguo.bloomtest.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.pingguo.bloomtest.common.BtException; import com.pingguo.bloomtest.dao.ProjectDAO; import com.pingguo.bloomtest.pojo.Project; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; @Service public class ProjectService { @Autowired ProjectDAO projectDAO; public void addProject(Project project) { if (StringUtils.isBlank(project.getProjectName())) { BtException. ThrowException (" the project name is empty "); } QueryWrapper<Project> wrapperProject = new QueryWrapper<>(); wrapperProject.eq("projectName", project.getProjectName()); if (projectDAO.selectCount(wrapperProject) ! = 0) {BtException. ThrowException (" the project name already exists "); } project.setCreateTime(new Date()); project.setUpdateTime(new Date()); projectDAO.insert(project); }}Copy the code
Note that here, joined the new code, such as BtException. ThrowException (” the project name is empty “); Throw my custom exception when the project name is empty.
Custom exception classes
Here we write a custom exception class BtException to throw custom exceptions.
package com.pingguo.bloomtest.common; public class BtException extends RuntimeException{ private BtException(String message) { super(message); } public static void throwException(String message) { throw new BtException(message); }}Copy the code
Write the ProjectController class
Write controller classes to receive requests from the front end.
package com.pingguo.bloomtest.controller; import com.pingguo.bloomtest.common.Result; import com.pingguo.bloomtest.pojo.Project; import com.pingguo.bloomtest.service.ProjectService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("project") public class ProjectController { @Autowired ProjectService projectService; @PostMapping("/add") public Result addProject(@RequestBody Project project) { try { projectService.addProject(project); return Result.success(); } catch (Exception e) { return Result.fail(e.toString()); }}}Copy the code
Here call projectService. AddProject () method, with try catch, to catch exceptions thrown by the service layer, unity in order to return the result.
When not captured, an error is reported like this:
After capture, this is it:
Test new interface /project/add
A normal new scenario.
Repeat the new scenario.
The entry project name is empty.
Refresh the data table.
Data is successfully added.