easycode反向工程生成DAO service层:
通过创建好的数据库使用的技术是jpa
安装easycode:
我使用的是idea进行生成代码,在plugins安装easycode
设置easycode,在模板配置配置jpa:
首先添加分组,我命名为jpa,然后再依次添加生成dao层service层的jpa代码模板。
service:
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
##拿到主键#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#end#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()*/
public interface $!{tableName} {
}
serviceImpl:
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
##拿到主键#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#end#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.List;/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
*
* @author $!author
* @since $!time.currTime()*/@Service
@Transactionalpublic class $!{tableName} implements $!{tableInfo.name}Service {
@Resourceprivate $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
}
dao:
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
##拿到主键#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#set($pkType = $tableInfo.pkColumn.get(0).shortType)
#end#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;import $!{tableInfo.savePackageName}.entity.Base$!{tableInfo.name};
import com.acmtc.common.dao.IRepository;/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()*/
public interface $!{tableName} extends IRepository{
}
entity:
##引入宏定义
$!define
##定义初始变量
#set($entityName = $tool.append("Base", $tableInfo.name))
##设置回调
$!callback.setFileName($tool.append($entityName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/entity"))
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import org.hibernate.annotations.GenericGenerator;
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Entity
@Table(name="$!{tableInfo.obj.name}")
public class $!{entityName} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})/**
* ${column.comment}
*/#end
#if(${velocityCount} == 1)
@Id
@Column(name = "$column.obj.getName()")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@GeneratedValue(generator = "system-uuid")
#else
@Column(name = "$column.obj.getName()")
#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
}
配置完成:
就可以连接数据库,右击选中的表,选中easycode,使用自己配置好的模板即可