告别重复造轮子!用Cursor + Java,开发效率飙升200%

告别重复造轮子!用Cursor + Java,开发效率飙升200%

AI这么火,作为一名程序员通过AI辅助编程是必要的,能够减少重复工作、提高编程效率。本文主要介绍如何使用cursor开发java项目。

一、下载、安装

cursor官网下载地址,支持Windows、linux、mac版本的下载.


二、账号登录

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

2.登录成功就是下面这个样子,点击Manage Account后Open可以查看cursor账户和订单信息。然后就可以使用cursor的ai功能,不过新注册Cursor 免费账号有时间要求。新用户可享受 14 天的免费试用期,在此期间能使用所有 Pro 功能,并且有 500 次高级模型的快速请求次数。

试用期结束后,高级 AI 模型的使用次数将受到严格限制,无法继续使用这些高级功能。如果想继续使用高级功能(文末有无限续杯方法),需要升级为付费的 Pro 版本等。


三、主要插件安装

  1. 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(依赖视图)
  1. Project Manager for Java
    帮助管理 Java 项目结构,支持 Maven、Gradle 等构建工具。
  2. 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常用快捷键

核心编辑:

  1. 牛b快捷键:Command+shift+p
  2. 格式化代码:⇧ + ⌥ + F
  3. 移动行:⌥ + ↓ / ⌥ + ↑ (向下/向上移动当前行)
  4. 行注释 / 取消注释:⌘ + /
  5. 块注释 / 取消注释:⌥ + ⇧ + A
  6. 显示方法参数提示:⌘ + ⇧ + Space (在方法调用括号内时显示参数类型和名称)

导航:

  1. 跳转到定义:⌘ + 点击 (将光标放在符号上,跳转到其定义处)
  2. 跳转到类型定义:⌘ + F12 (对于变量,跳转到其类型的定义)
  3. 查找引用:⇧ + F12 (将光标放在符号上,查找项目中所有引用它的地方)
  4. 查找文件:⌘ + P (快速按文件名打开文件)
  5. 查找符号 (类、方法、变量):⌘ + T (在当前工作区中搜索符号名)
  6. 查找所有内容:⌘ + ⇧ + F (全局文本搜索)

Fn 键: 如果你的 Mac 键盘需要 Fn 键来触发 F1-F12 功能键,记得加上 Fn(例如 Fn + F12)。

六、无限续杯方法

注册cursor账号的邮箱使用无限邮的邮箱,无限邮的邮箱注册一个以后后面只要增加后缀就可以生成新的邮箱

注册无限邮:2925邮箱-海量邮

七、效果演示

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信息。


来看看cursor生成的具体代码:

已生成的文件总结

根据您提供的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); /** * 根据ID查询学生 */ StudentDO selectById(@Param("id") Long id); /** * 新增学生 */ int insert(StudentDO studentDO); /** * 更新学生信息 */ int updateById(StudentDO studentDO); /** * 根据ID删除学生(逻辑删除) */ 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) { // 查询总数 Long total = studentDAO.countByPage(studentPageQueryDTO.getName(), studentPageQueryDTO.getSex()); if (total == 0) { PageDTO<StudentPageResultDTO> pageDTO = new PageDTO<>(); pageDTO.setTotal(0L); return pageDTO; } // 查询数据 List<StudentDO> studentDOList = studentDAO.selectByPage( studentPageQueryDTO.getName(), studentPageQueryDTO.getSex(), studentPageQueryDTO.getOffset(), studentPageQueryDTO.getPageSize() ); // 转换为DTO List<StudentPageResultDTO> resultList = studentDOList.stream().map(this::convertToDTO).collect(Collectors.toList()); return new PageDTO<>(resultList, total); } @Override public Boolean create(CreateStudentDTO createStudentDTO) { StudentDO studentDO = new StudentDO(); BeanUtils.copyProperties(createStudentDTO, studentDO); // 初始化创建信息 studentDO.initCreate(); studentDO.setCreateTime(System.currentTimeMillis()); studentDO.setUpdateTime(System.currentTimeMillis()); studentDO.setDeleted(0); return studentDAO.insert(studentDO) > 0; } @Override public Boolean edit(EditStudentDTO editStudentDTO) { // 先查询是否存在 StudentDO existStudent = studentDAO.selectById(editStudentDTO.getId()); if (existStudent == null) { throw new RuntimeException("学生不存在"); } StudentDO studentDO = new StudentDO(); BeanUtils.copyProperties(editStudentDTO, studentDO); // 初始化更新信息 studentDO.initUpdate(); studentDO.setUpdateTime(System.currentTimeMillis()); return studentDAO.updateById(studentDO) > 0; } private StudentPageResultDTO convertToDTO(StudentDO studentDO) { StudentPageResultDTO dto = new StudentPageResultDTO(); BeanUtils.copyProperties(studentDO, dto); // 转换时间戳为LocalDateTime if (studentDO.getCreateTime() != null) { dto.setCreateTime(LocalDateTime.ofEpochSecond(studentDO.getCreateTime() / 1000, 0, ZoneOffset.UTC)); } if (studentDO.getUpdateTime() != null) { dto.setUpdateTime(LocalDateTime.ofEpochSecond(studentDO.getUpdateTime() / 1000, 0, ZoneOffset.UTC)); } return dto; } } 

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)); } } 

主要功能接口

  1. 分页查询学生列表 - POST /student/listByPage
  2. 新增学生 - POST /student/create
  3. 编辑学生信息 - POST /student/edit

特点

  • ✅ 使用Swagger注解进行API文档化
  • ✅ 包含参数验证(@Valid、@NotBlank、@NotNull)
  • ✅ 遵循项目现有的代码风格和架构
  • ✅ 支持软删除(deleted字段)
  • ✅ 包含创建时间、更新时间等审计字段
  • ✅ 支持按姓名模糊查询和性别筛选
  • ✅ 使用MyBatis进行数据库操作
  • ✅ 返回统一的DianResult格式

所有文件都已按照您项目的现有架构和代码风格生成,可以直接使用。您只需要确保数据库中有对应的student表即可。


以上就是cursor生成的代码,看起来还不错,另外cursor还可以直接上传prd原型图生成代码哦,快去试试吧~