• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用idea插件进行java代码生成的操作

java 搞代码 4年前 (2022-01-05) 115次浏览 已收录 0个评论

这篇文章主要介绍了使用idea插件进行java代码生成的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

java代码生成

使用idea的插件codehelper.generator进行代码生成,可以根据entity,生成对应的

1、建表sql语句

2、dao.java文件

3、dao.xml文件

4、service.java文件

同时这个插件还能在new了entity之后生成所有的set方法

多次生成,不会影响自己手动添加的代码

安装

安装插件codehelper.generator

案例

 @Data @AllArgsConstructor @NoArgsConstructor public class UserEntity { @Id private Integer id; private String name; /** * 1启用,0停用 */ private Integer state; private String remark; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") p<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>rivate Date addtime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date stoptime; } 

生成set

UserEntity user=new UserEntity();

//new了之后在下一行:点击tool–codeHelper–GenAllSetter

生成代码

点击tool–codeHelper–tox Boxes–在弹窗中输入entity,多个使用’|’分隔,就会在当前文件夹生成代码

sql

 -- auto Generated on 2020-01-14 12:49:57 -- DROP TABLE IF EXISTS `user_entity`; CREATE TABLE user_entity( `id` INTEGER(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id', `name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'name', `state` INTEGER(12) NOT NULL DEFAULT -1 COMMENT '1启用,0停用', `remark` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'remark', `addtime` DATETIME NOT NULL DEFAULT '1000-01-01 00:00:00' COMMENT 'addtime', `stoptime` DATETIME NOT NULL DEFAULT '1000-01-01 00:00:00' COMMENT 'stoptime', PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'user_entity';

dao

 package com.demo1.invoice.entity.user; import org.apache.ibatis.annotations.Param; import java.util.List; import com.demo1.invoice.entity.user.UserEntity; public interface UserEntityDao { int insert(@Param("pojo") UserEntity pojo); int insertList(@Param("pojos") List pojo); List select(@Param("pojo") UserEntity pojo); int update(@Param("pojo") UserEntity pojo); } 

xml

   <!--auto generated Code--> <!--auto generated Code--> id, name, state, remark, addtime, stoptime <!--auto generated Code--> INSERT INTO user_entity   id,  name,  state,  remark,  addtime,  stoptime,  VALUES   #{pojo.id},  #{pojo.name},  #{pojo.state},  #{pojo.remark},  #{pojo.addtime},  #{pojo.stoptime}, <!--auto generated Code--> INSERT INTO user_entity(  )VALUES  ( #{pojo.id}, #{pojo.name}, #{pojo.state}, #{pojo.remark}, #{pojo.addtime}, #{pojo.stoptime} ) <!--auto generated Code--> UPDATE user_entity   id = #{pojo.id},  name = #{pojo.name},  state = #{pojo.state},  remark = #{pojo.remark},  addtime = #{pojo.addtime},  stoptime = #{pojo.stoptime}  WHERE id = #{pojo.id} <!--auto generated Code--> SELECT  FROM user_entity   AND id = #{pojo.id}  AND name = #{pojo.name}  AND state = #{pojo.state}  AND remark = #{pojo.remark}  AND addtime = #{pojo.addtime}  AND stoptime = #{pojo.stoptime}  LIMIT 1000 <!--auto generated Code--> DELETE FROM user_entity where id = #{id} 

service

 import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import com.demo1.invoice.entity.user.UserEntity; import com.demo1.invoice.entity.user.UserEntityDao; @Service public class UserEntityService { @Resource private UserEntityDao userEntityDao; public int insert(UserEntity pojo){ return userEntityDao.insert(pojo); } public int insertList(List pojos){ return userEntityDao.insertList(pojos); } public List select(UserEntity pojo){ return userEntityDao.select(pojo); } public int update(UserEntity pojo){ return userEntityDao.update(pojo); } } 

补充知识:IDEA 新建junit单元测试

1. 新建test目录

在src同级目录下新建test文件夹,右键test文件夹设置为Test Source Root

2. 创建测试类

选中要创建单元测试的实现类,并将焦点放在编辑器中(鼠标在编辑器中点击一下),菜单栏选择Navigate—-Test(Mac快捷键:Cmd+shift+t):

选择创建新的测试:

选中要测试的方法,以及生成@Before:

这样之后就会在test下新建一个测试类:

3. 测试函数介绍

测试类中包含两个函数:

@Before

public void setUp() throws Exception

这个是测试方法执行前执行的函数,假如在测试方法中需要使用该类中的成员变量,那么可以在该函数中定义该成员变量。

@Test

public void findUserById() throws Exception

这个便是测试函数。点击编辑器左列的小工具即可发起测试。

以上这篇使用idea插件进行java代码生成的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是使用idea插件进行java代码生成的操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用idea插件进行java代码生成的操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址