目录
- 1. AI工具介绍
- 2. 写代码的正确顺序
- 2.1 编写 Entity 类:
- 2.2 编写 Mapper 接口:
- 2.3 编写 Mapper XML 文件(如果使用 MyBatis):
- 2.4 编写 Service 接口:
- 2.5 编写 Service 实现类(ServiceImpl):
- 2.6 编写 Controller 类:
- 3. 总结
1. AI工具介绍
在idea的插件市场里:
2. 写代码的正确顺序
2.1 编写 Entity 类:
定义与数据库表对应的领域模型类,包括字段、getter 和 setter 方法。
注意:主要确定各个字段的合理性,sql对于的idea上的类型,例如日期Date类,在Java上需要加注解@DateTimeFormat(value = “yyyy-MM-dd”)来固定格式。
package com.goblin.BIbackend.model.entity;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName install_requests
*/
@TableName(value ="install_requests")
@Data
public class InstallRequests implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
*
*/
private Long userId;
/**
*
*/
@DateTimeFormat(value = "yyyy-MM-dd")
private Date requestDate;
/**
*
*/
private String status;
/**
*
*/
private String description;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
2.2 编写 Mapper 接口:
创建与数据库操作相关的接口,使用 MyBatis 或 JPA 等 ORM 框架注解来标识数据库操作。
package com.goblin.BIbackend.mapper;
import com.goblin.BIbackend.model.entity.InstallationProgress;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author **
* @description 针对表【installation_progress】的数据库操作Mapper
* @createDate 2024-07-07 00:36:01
* @Entity com.goblin.BIbackend.model.entity.InstallationProgress
*/
public interface InstallationProgressMapper extends BaseMapper<InstallationProgress> {
public InstallationProgress select1(Long id);
public void insert1(InstallationProgress installationProgress);
public void delete1(Long id);
public void update1(InstallationProgress installationProgress);
}
2.3 编写 Mapper XML 文件(如果使用 MyBatis):
在 XML 文件中编写具体的 SQL 语句和结果映射,与 Mapper 接口中的方法相对应。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.goblin.BIbackend.mapper.InstallRequestsMapper">
<resultMap id="BaseResultMap" type="com.goblin.BIbackend.model.entity.InstallRequests">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="userId" column="user_id" jdbcType="BIGINT"/>
<result property="requestDate" column="request_date" jdbcType="DATE"/>
<result property="status" column="status" jdbcType="VARCHAR"/>
<result property="description" column="description" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,user_id,request_date,
status,description
</sql>
<select id="select1" resultMap="BaseResultMap">
SELECT * FROM install_requests where id = #{id}
</select>
<insert id="insert1" parameterType="com.goblin.BIbackend.model.entity.InstallRequests">
INSERT INTO install_requests (user_id,request_date,status,description)
VALUES (#{userId},#{requestDate},#{status},#{description})
</insert>
<delete id="delete1" parameterType="com.goblin.BIbackend.model.entity.InstallRequests">
DELETE FROM install_requests where id = #{id}
</delete>
<update id="update1" parameterType="com.goblin.BIbackend.model.entity.InstallRequests">
UPDATE install_requests
set user_id = #{userId},request_date = #{requestDate},status = #{status},description = #{description}
WHERE id = #{id}
</update>
</mapper>
2.4 编写 Service 接口:
定义业务逻辑的接口,声明服务层的方法。
package com.goblin.BIbackend.service;
import com.goblin.BIbackend.model.entity.InstallRequests;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author **
* @description 针对表【install_requests】的数据库操作Service
* @createDate 2024-07-07 00:04:57
*/
public interface InstallRequestsService extends IService<InstallRequests> {
InstallRequests select2(Long id);
InstallRequests insert2(InstallRequests installRequests);
void delete2(Long id);
InstallRequests update2(InstallRequests installRequests);
}
2.5 编写 Service 实现类(ServiceImpl):
实现 Service 接口中定义的方法,调用 Mapper 接口进行数据访问。
package com.goblin.BIbackend.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.goblin.BIbackend.model.entity.InstallRequests;
import com.goblin.BIbackend.service.InstallRequestsService;
import com.goblin.BIbackend.mapper.InstallRequestsMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author **
* @description 针对表【install_requests】的数据库操作Service实现
* @createDate 2024-07-07 00:04:57
*/
@Service
public class InstallRequestsServiceImpl extends ServiceImpl<InstallRequestsMapper, InstallRequests>
implements InstallRequestsService{
@Resource
private InstallRequestsMapper mapper;
@Override
public InstallRequests select2(Long id) {
return mapper.select1(id);
}
@Override
public InstallRequests insert2(InstallRequests installRequests) {
mapper.insert1(installRequests);
return installRequests;
}
@Override
public void delete2(Long id) {
mapper.delete1(id);
}
@Override
public InstallRequests update2(InstallRequests installRequests) {
mapper.update1(installRequests);
return installRequests;
}
}
2.6 编写 Controller 类:
创建 RESTful API 或 MVC 控制器,处理 HTTP 请求并调用 Service 层。
package com.goblin.BIbackend.controller;
import com.goblin.BIbackend.common.BaseResponse;
import com.goblin.BIbackend.common.ResultUtils;
import com.goblin.BIbackend.model.entity.InstallRequests;
import com.goblin.BIbackend.service.InstallRequestsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/installRequests")
@Slf4j
public class InstallRequestsController {
@Resource
private InstallRequestsService iRS;
@GetMapping("/select2")
public BaseResponse<InstallRequests> select2(Long id) {
log.info("查询电表安装请求: {}", id);
InstallRequests installRequests = iRS.select2(id);
return ResultUtils.success(installRequests);
}
@PostMapping("/insert2")
public BaseResponse<InstallRequests> insert2(@RequestBody InstallRequests installRequests) {
log.info("新增电表安装请求: {}", installRequests);
InstallRequests insert = iRS.insert2(installRequests);
return ResultUtils.success(insert);
}
@DeleteMapping("/delete2")
public BaseResponse<Boolean> delete2(Long id) {
log.info("删除电表安装请求: {}", id);
iRS.delete2(id);
return ResultUtils.success(true);
}
@PutMapping("/update2")
public BaseResponse<InstallRequests> update2(@RequestBody InstallRequests installRequests) {
log.info("更新电表安装请求: {}", installRequests);
iRS.update2(installRequests);
return ResultUtils.success(installRequests);
}
}
3. 总结
这个顺序并不是固定不变的,实际开发过程中可能会根据项目需求和团队习惯进行调整。例如,可以先编写 Controller 层来定义 API 接口,再逆向工程到 Service 层和 Entity 层。over