跳到主要内容Cursor AI 辅助 Java 项目开发与配置指南 | 极客日志JavaAIjava
Cursor AI 辅助 Java 项目开发与配置指南
使用 Cursor AI 工具进行 Java 项目开发的全流程。涵盖软件安装、账号登录、必要插件配置及快捷键设置。通过 SQL 表结构生成实体类、DTO、DAO、Service 及 Controller 层代码,展示 AI 在减少重复编码工作方面的实际应用效果。适合希望提升开发效率的 Java 开发者参考。
MqEngine4 浏览 一、下载与安装
支持 Windows、Linux、mac 版本的下载。


二、账号登录
- 安装好 cursor 后,打开 cursor 点击右上角的设置,进行 cursor 账号登录,如果没有账号可以用邮箱先注册一个。

- 登录成功就是下面这个样子,点击 Manage Account 后 Open 可以查看 cursor 账户和订单信息。然后就可以使用 cursor 的 ai 功能,不过新注册 Cursor 免费账号有时间要求。新用户可享受 14 天的免费试用期,在此期间能使用所有 Pro 功能,并且有 500 次高级模型的快速请求次数。
试用期结束后,高级 AI 模型的使用次数将受到严格限制,无法继续使用这些高级功能。如果想继续使用高级功能,需要升级为付费的 Pro 版本等。

三、主要插件安装
- Extension Pack for Java
微软官方的 Java 扩展包,集成了 VS Code 的 Java 开发工具链,包含:
- Language Support for Java(语言支持)
- Debugger for Java(调试器)
- Test Runner for Java(测试运行器)
- Maven for Java(Maven 支持)
- Java Dependency Viewer(依赖视图)
- Project Manager for Java
帮助管理 Java 项目结构,支持 Maven、Gradle 等构建工具。
- Lombok Annotations Support for VS Code
如果你使用 Lombok 简化 Java 代码,此插件可提供编译时注解支持。
四、相关配置
快捷键 Command+shift+p 打开 Open user settings.json 添加
{ "java.configuration.maven.userSettings": "你的 maven setting 路径",
"java.home"
:
"你的 jdk 路径"
}
五、mac 版本 cursor 常用快捷键
- **核心快捷键:**Command+shift+p
- 格式化代码:
⇧ + ⌥ + F
- 移动行:
⌥ + ↓ / ⌥ + ↑ (向下/向上移动当前行)
- 行注释 / 取消注释:
⌘ + /
- 块注释 / 取消注释:
⌥ + ⇧ + A
- 显示方法参数提示:
⌘ + ⇧ + Space (在方法调用括号内时显示参数类型和名称)
- 跳转到定义:
⌘ + 点击 (将光标放在符号上,跳转到其定义处)
- 跳转到类型定义:
⌘ + F12 (对于变量,跳转到其类型的定义)
- 查找引用:
⇧ + F12 (将光标放在符号上,查找项目中所有引用它的地方)
- 查找文件:
⌘ + P (快速按文件名打开文件)
- 查找符号 (类、方法、变量):
⌘ + T (在当前工作区中搜索符号名)
- 查找所有内容:
⌘ + ⇧ + F (全局文本搜索)
Fn 键: 如果你的 Mac 键盘需要 Fn 键来触发 F1-F12 功能键,记得加上 Fn(例如 Fn + F12)。
六、效果演示
CREATE TABLE `student` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名字', `sex` tinyint NOT NULL COMMENT '性别', `birthday` datetime NOT NULL COMMENT '出生日期', `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '简介', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新者', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', `tenant_id` bigint NOT NULL DEFAULT '0' COMMENT '租户编号', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='学生表';
**提示词:**根据以上 sql,帮我生成相应的接口 controller、service、dao 和 mybatis 的 xml 文件。接口及参数定义使用 swagger 注解。主要接口为以下三个:1.分页查询 student 列表;2.新增 student;3.编辑 student 信息。
已生成的文件总结
根据您提供的 SQL 表结构,我已经为您生成了完整的学生管理模块,包含以下文件:
1. 实体类 (Model)
src/main/java/com/zl/model/StudentDO.java - 学生实体类,继承 BaseEntityDO
package com.zl.model; import lombok.Data; import lombok.EqualsAndHashCode; import java.time.LocalDateTime; @Data @EqualsAndHashCode(callSuper = true) public class StudentDO extends BaseEntityDO { private Long id; private String name; private Integer sex; private LocalDateTime birthday; private String description; private String creator; private String updater; private Long tenantId; }
2. DTO 类
src/main/java/com/zl/dto/StudentPageQueryDTO.java - 分页查询参数 DTO
package com.zl.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel(value = "学生分页查询") public class StudentPageQueryDTO { @ApiModelProperty(value = "学生姓名", dataType = "String") private String name; @ApiModelProperty(value = "性别", dataType = "Integer") private Integer sex; @ApiModelProperty(value = "偏移量", dataType = "Integer") private Integer offset; @ApiModelProperty(value = "每页数量", dataType = "Integer") private Integer pageSize; }
src/main/java/com/zl/dto/CreateStudentDTO.java - 新增学生参数 DTO
package com.zl.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.time.LocalDateTime; @Data @ApiModel(value = "新增学生") public class CreateStudentDTO { @ApiModelProperty(value = "学生姓名", required = true, dataType = "String") @NotBlank(message = "学生姓名不能为空") private String name; @ApiModelProperty(value = "性别", required = true, dataType = "Integer") @NotNull(message = "性别不能为空") private Integer sex; @ApiModelProperty(value = "出生日期", required = true, dataType = "LocalDateTime") @NotNull(message = "出生日期不能为空") private LocalDateTime birthday; @ApiModelProperty(value = "简介", required = true, dataType = "String") @NotBlank(message = "简介不能为空") private String description; }
src/main/java/com/zl/dto/EditStudentDTO.java - 编辑学生参数 DTO
package com.zl.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.time.LocalDateTime; @Data @ApiModel(value = "编辑学生") public class EditStudentDTO { @ApiModelProperty(value = "学生 ID", required = true, dataType = "Long") @NotNull(message = "学生 ID 不能为空") private Long id; @ApiModelProperty(value = "学生姓名", required = true, dataType = "String") @NotBlank(message = "学生姓名不能为空") private String name; @ApiModelProperty(value = "性别", required = true, dataType = "Integer") @NotNull(message = "性别不能为空") private Integer sex; @ApiModelProperty(value = "出生日期", required = true, dataType = "LocalDateTime") @NotNull(message = "出生日期不能为空") private LocalDateTime birthday; @ApiModelProperty(value = "简介", required = true, dataType = "String") @NotBlank(message = "简介不能为空") private String description; }
src/main/java/com/zl/dto/StudentPageResultDTO.java - 分页查询结果 DTO
package com.zl.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.time.LocalDateTime; @Data @ApiModel(value = "学生分页结果") public class StudentPageResultDTO { @ApiModelProperty(value = "学生 ID", dataType = "Long") private Long id; @ApiModelProperty(value = "学生姓名", dataType = "String") private String name; @ApiModelProperty(value = "性别", dataType = "Integer") private Integer sex; @ApiModelProperty(value = "出生日期", dataType = "LocalDateTime") private LocalDateTime birthday; @ApiModelProperty(value = "简介", dataType = "String") private String description; @ApiModelProperty(value = "创建者", dataType = "String") private String creator; @ApiModelProperty(value = "创建时间", dataType = "LocalDateTime") private LocalDateTime createTime; @ApiModelProperty(value = "更新者", dataType = "String") private String updater; @ApiModelProperty(value = "更新时间", dataType = "LocalDateTime") private LocalDateTime updateTime; }
3. DAO 层
src/main/java/com/zl/dao/StudentDAO.java - 数据访问接口
package com.zl.dao; import com.zl.model.StudentDO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper public interface StudentDAO { List<StudentDO> selectByPage(@Param("name") String name, @Param("sex") Integer sex, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize); Long countByPage(@Param("name") String name, @Param("sex") Integer sex); StudentDO selectById(@Param("id") Long id); int insert(StudentDO studentDO); int updateById(StudentDO studentDO); int deleteById(@Param("id") Long id); }
src/main/resources/mapper/StudentDAO.xml - MyBatis 映射文件
<?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.zl.dao.StudentDAO"> <resultMap type="com.zl.model.StudentDO"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="sex" property="sex" jdbcType="TINYINT"/> <result column="birthday" property="birthday" jdbcType="TIMESTAMP"/> <result column="description" property="description" jdbcType="VARCHAR"/> <result column="creator" property="creator" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="updater" property="updater" jdbcType="VARCHAR"/> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/> <result column="deleted" property="deleted" jdbcType="BIT"/> <result column="tenant_id" property="tenantId" jdbcType="BIGINT"/> </resultMap> <sql> id, name, sex, birthday, description, creator, create_time, updater, update_time, deleted, tenant_id </sql> <sql> WHERE deleted = 0 <if test="name != null and name != ''"> AND name LIKE CONCAT('%', #{name}, '%') </if> <if test="sex != null"> AND sex = #{sex} </if> </sql> <select resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM student <include refid="Base_Where_Clause"/> ORDER BY create_time DESC <if test="offset != null and pageSize != null"> LIMIT #{offset}, #{pageSize} </if> </select> <select resultType="java.lang.Long"> SELECT COUNT(*) FROM student <include refid="Base_Where_Clause"/> </select> <select resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM student WHERE id = #{id} AND deleted = 0 </select> <insert parameterType="com.zl.model.StudentDO" useGeneratedKeys="true" keyProperty="id"> INSERT INTO student ( name, sex, birthday, description, creator, create_time, updater, update_time, deleted, tenant_id ) VALUES ( #{name}, #{sex}, #{birthday}, #{description}, #{creator}, FROM_UNIXTIME(#{createTime}/1000), #{updater}, FROM_UNIXTIME(#{updateTime}/1000), #{deleted}, #{tenantId} ) </insert> <update parameterType="com.zl.model.StudentDO"> UPDATE student <set> <if test="name != null">name = #{name},</if> <if test="sex != null">sex = #{sex},</if> <if test="birthday != null">birthday = #{birthday},</if> <if test="description != null">description = #{description},</if> <if test="updater != null">updater = #{updater},</if> <if test="updateTime != null">update_time = FROM_UNIXTIME(#{updateTime}/1000),</if> <if test="deleted != null">deleted = #{deleted},</if> <if test="tenantId != null">tenant_id = #{tenantId},</if> </set> WHERE id = #{id} AND deleted = 0 </update> <update> UPDATE student SET deleted = 1 WHERE id = #{id} </update> </mapper>
4. Service 层
src/main/java/com/zl/service/StudentService.java - 业务逻辑接口
package com.zl.service; import com.zl.common.PageDTO; import com.zl.dto.CreateStudentDTO; import com.zl.dto.EditStudentDTO; import com.zl.dto.StudentPageQueryDTO; import com.zl.dto.StudentPageResultDTO; public interface StudentService { PageDTO<StudentPageResultDTO> listByPage(StudentPageQueryDTO studentPageQueryDTO); Boolean create(CreateStudentDTO createStudentDTO); Boolean edit(EditStudentDTO editStudentDTO); }
src/main/java/com/zl/service/impl/StudentServiceImpl.java - 业务逻辑实现
package com.zl.service.impl; import com.zl.common.PageDTO; import com.zl.dao.StudentDAO; import com.zl.dto.CreateStudentDTO; import com.zl.dto.EditStudentDTO; import com.zl.dto.StudentPageQueryDTO; import com.zl.dto.StudentPageResultDTO; import com.zl.model.StudentDO; import com.zl.service.StudentService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.List; import java.util.stream.Collectors; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDAO studentDAO; @Override public PageDTO<StudentPageResultDTO> listByPage(StudentPageQueryDTO studentPageQueryDTO) {
5. Controller 层
src/main/java/com/zl/controller/StudentController.java - REST API 控制器
package com.zl.controller; import com.zl.common.DianResult; import com.zl.common.PageDTO; import com.zl.dto.CreateStudentDTO; import com.zl.dto.EditStudentDTO; import com.zl.dto.StudentPageQueryDTO; import com.zl.dto.StudentPageResultDTO; import com.zl.service.StudentService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @RestController @RequestMapping("/student") @Api(tags = "学生管理") public class StudentController { @Autowired private StudentService studentService; @ApiOperation(value = "分页查询学生列表") @PostMapping("/listByPage") public DianResult<PageDTO<StudentPageResultDTO>> listByPage(@RequestBody StudentPageQueryDTO studentPageQueryDTO) { return DianResult.success(studentService.listByPage(studentPageQueryDTO)); } @ApiOperation(value = "新增学生") @PostMapping("/create") public DianResult<Boolean> create(@Valid @RequestBody CreateStudentDTO createStudentDTO) { return DianResult.success(studentService.create(createStudentDTO)); } @ApiOperation(value = "编辑学生信息") @PostMapping("/edit") public DianResult<Boolean> edit(@Valid @RequestBody EditStudentDTO editStudentDTO) { return DianResult.success(studentService.edit(editStudentDTO)); } }
主要功能接口
- 分页查询学生列表 -
POST /student/listByPage
- 新增学生 -
POST /student/create
- 编辑学生信息 -
POST /student/edit
特点
- ✅ 使用 Swagger 注解进行 API 文档化
- ✅ 包含参数验证(@Valid、@NotBlank、@NotNull)
- ✅ 遵循项目现有的代码风格和架构
- ✅ 支持软删除(deleted 字段)
- ✅ 包含创建时间、更新时间等审计字段
- ✅ 支持按姓名模糊查询和性别筛选
- ✅ 使用 MyBatis 进行数据库操作
- ✅ 返回统一的 DianResult 格式
所有文件都已按照您项目的现有架构和代码风格生成,可以直接使用。您只需要确保数据库中有对应的 student 表即可。
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- RSA密钥对生成器
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
- Mermaid 预览与可视化编辑
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online