目录
java代码生成
使用idea的插件codehelper.generator
进行代码生成,可以根据entity,生成对应的
- 建表sql语句
- dao.java文件
- dao.xml文件
- 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")
private 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.ucare.invoice.entity.user;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import com.ucare.invoice.entity.user.UserEntity;
public interface UserEntityDao {
int insert(@Param("pojo") UserEntity pojo);
int insertList(@Param("pojos") List< UserEntity> pojo);
List<UserEntity> select(@Param("pojo") UserEntity pojo);
int update(@Param("pojo") UserEntity pojo);
}
- xml
<?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.ucare.invoice.entity.user.UserEntityDao">
<!--auto generated Code-->
<resultMap id="AllColumnMap" type="com.ucare.invoice.entity.user.UserEntity">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="state" property="state"/>
<result column="remark" property="remark"/>
<result column="addtime" property="addtime"/>
<result column="stoptime" property="stoptime"/>
</resultMap>
<!--auto generated Code-->
<sql id="all_column">
id,
name,
state,
remark,
addtime,
stoptime
</sql>
<!--auto generated Code-->
<insert id="insert">
INSERT INTO user_entity
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="pojo.id != null"> id, </if>
<if test="pojo.name != null"> name, </if>
<if test="pojo.state != null"> state, </if>
<if test="pojo.remark != null"> remark, </if>
<if test="pojo.addtime != null"> addtime, </if>
<if test="pojo.stoptime != null"> stoptime, </if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="pojo.id != null"> #{pojo.id}, </if>
<if test="pojo.name != null"> #{pojo.name}, </if>
<if test="pojo.state != null"> #{pojo.state}, </if>
<if test="pojo.remark != null"> #{pojo.remark}, </if>
<if test="pojo.addtime != null"> #{pojo.addtime}, </if>
<if test="pojo.stoptime != null"> #{pojo.stoptime}, </if>
</trim>
</insert>
<!--auto generated Code-->
<insert id="insertList">
INSERT INTO user_entity(
<include refid="all_column"/>
)VALUES
<foreach collection="pojos" item="pojo" index="index" separator=",">
(
#{pojo.id},
#{pojo.name},
#{pojo.state},
#{pojo.remark},
#{pojo.addtime},
#{pojo.stoptime}
)
</foreach>
</insert>
<!--auto generated Code-->
<update id="update">
UPDATE user_entity
<set>
<if test="pojo.id != null"> id = #{pojo.id}, </if>
<if test="pojo.name != null"> name = #{pojo.name}, </if>
<if test="pojo.state != null"> state = #{pojo.state}, </if>
<if test="pojo.remark != null"> remark = #{pojo.remark}, </if>
<if test="pojo.addtime != null"> addtime = #{pojo.addtime}, </if>
<if test="pojo.stoptime != null"> stoptime = #{pojo.stoptime} </if>
</set>
WHERE id = #{pojo.id}
</update>
<!--auto generated Code-->
<select id="select" resultMap="AllColumnMap">
SELECT <include refid="all_column"/>
FROM user_entity
<where>
<if test="pojo.id != null"> AND id = #{pojo.id} </if>
<if test="pojo.name != null"> AND name = #{pojo.name} </if>
<if test="pojo.state != null"> AND state = #{pojo.state} </if>
<if test="pojo.remark != null"> AND remark = #{pojo.remark} </if>
<if test="pojo.addtime != null"> AND addtime = #{pojo.addtime} </if>
<if test="pojo.stoptime != null"> AND stoptime = #{pojo.stoptime} </if>
</where>
LIMIT 1000
</select>
<!--auto generated Code-->
<delete id="delete">
DELETE FROM user_entity where id = #{id}
</delete>
</mapper>
- service
package com.ucare.invoice.entity.user;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.ucare.invoice.entity.user.UserEntity;
import com.ucare.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< UserEntity> pojos){
return userEntityDao.insertList(pojos);
}
public List<UserEntity> select(UserEntity pojo){
return userEntityDao.select(pojo);
}
public int update(UserEntity pojo){
return userEntityDao.update(pojo);
}
}