在平常的开发中,我们经常要写Dao,Service,Controller层,而每层都有些共用的代码,其中Dao中的公共代码如下图所示(这里使用了mybatis generator自动生成代码):
那么我们就可以利用idea的class和interface模板来自动生成这些公共代码,省去了复制粘贴的时间,提升效率,模板的语言是velocity。首先进入idea,按下command+,这个快捷键进入到Preferences中,然后在搜索框输入:file and code templates,如下图所示,我们聚焦到class和interface中,这里解释下这两个东西,当你创建class和interface的时候,所生成的代码就是从这里面来的:
首先是class,在代码中把下面的代码覆盖当前的代码,:
//${PACKAGE_NAME}代表包名,如:com.java.test
//${NAME}代表你输入的class名或者interface名,如:UserController
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#set($name = ${NAME})
//如果类是xxxController
#if (${NAME} && $name.endsWith("Controller"))
import com.alibaba.fastjson.JSONObject;
import com.miniprogram.common.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#end
//如果类名是xxxServiceImpl
#if (${NAME} && $name.endsWith("ServiceImpl"))
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#end
//如果类名是xxxDaoImpl
#if (${NAME} && $name.endsWith("DaoImpl"))
import com.google.common.base.Preconditions;
import com.miniprogram.common.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#end
//这里生成作者,日期之类的信息,自己配置,默认空
#parse("File Header.java")
//下面是处理不同类的注解
#if (${NAME} && $name.endsWith("Controller"))
@Api(tags = "")
@RestController
@RequestMapping("")
#end
#if (${NAME} && ($name.endsWith("ServiceImpl") || $name.endsWith("DaoImpl")))
@Service("")
#end
//下面处理类的一些公共代码
public class ${NAME} #if (${NAME} && $name.endsWith("ServiceImpl"))implements $name.replace("Impl","")#end {
//xxxController的公共代码
#if (${NAME} && $name.endsWith("Controller"))
@Autowired
private xxx xxx;
@ApiOperation(value = "xxx")
@RequestMapping(value = "/xxx", method = RequestMethod.GET)
public JSONObject adminLogin(@ApiParam(name = "xxx", value = "xxx") String xxx) {
return Response.succ();
}
#end
//xxxService的公共代码
#if (${NAME} && $name.endsWith("ServiceImpl"))
@Autowired
private $name.replace("ServiceImpl","")Dao $name.replace("ServiceImpl","").toLowerCase()Dao;
@Override
public Long insert($name.replace("ServiceImpl","") $name.replace("ServiceImpl","").toLowerCase()) {
return null;
}
@Override
public List<$name.replace("ServiceImpl","")> find($name.replace("ServiceImpl","")Example example) {
return null;
}
@Override
public Integer update($name.replace("ServiceImpl","") $name.replace("ServiceImpl","").toLowerCase(), $name.replace("ServiceImpl","")Example example) {
return null;
}
@Override
public Integer count($name.replace("ServiceImpl","")Example example) {
return null;
}
#end
//xxxDaoImpl的公共代码
#if (${NAME} && $name.endsWith("DaoImpl"))
@Autowired
private $name.replace("DaoImpl","")Mapper $name.replace("DaoImpl","").toLowerCase()Mapper;
@Override
public Long insert($name.replace("DaoImpl","") $name.replace("DaoImpl","").toLowerCase()) {
Preconditions.checkNotNull($name.replace("DaoImpl","").toLowerCase());
ValidationUtil.validate($name.replace("DaoImpl","").toLowerCase());
${name.replace("DaoImpl","").toLowerCase()}.checkBeforeInsert();
${name.replace("DaoImpl","").toLowerCase()}Mapper.insertSelective($name.replace("DaoImpl","").toLowerCase());
return ${name.replace("DaoImpl","").toLowerCase()}.getId();
}
@Override
public Integer update($name.replace("DaoImpl","") $name.replace("DaoImpl","").toLowerCase(), $name.replace("DaoImpl","")Example example) {
${name.replace("DaoImpl","").toLowerCase()}.checkBeforeUpdate();
return ${name.replace("DaoImpl","").toLowerCase()}Mapper.updateByExampleSelective($name.replace("DaoImpl","").toLowerCase(), example);
}
@Override
public List<$name.replace("DaoImpl","")> find($name.replace("DaoImpl","")Example example) {
return ${name.replace("DaoImpl","").toLowerCase()}Mapper.selectByExample(example);
}
@Override
public Integer count($name.replace("DaoImpl","")Example example) {
return ${name.replace("DaoImpl","").toLowerCase()}Mapper.countByExample(example);
}
#end
}
下面是interface的代码,也是覆盖当前代码:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#set($name = ${NAME})
//下面是根据是xxxService还是xxxDao来导入包
#if (${NAME} && $name.endsWith("Service"))
import org.springframework.stereotype.Service;
import java.util.List;
#end
#if (${NAME} && $name.endsWith("Dao"))
import java.util.List;
#end
#parse("File Header.java")
public interface ${NAME} {
//xxxService的公共代码
#if (${NAME} && $name.endsWith("Service"))
List<$beanName> find(${beanName}Example example);
Integer count(${beanName}Example example);
Integer update($beanName $beanName.toLowerCase(), ${beanName}Example example);
$beanName insert($beanName $beanName.toLowerCase());
#end
//xxxDao的公共代码
#if (${NAME} && $name.endsWith("Dao"))
public Long insert($beanName $beanName.toLowerCase());
public Integer update($beanName $beanName.toLowerCase(), ${beanName}Example example);
public List<$beanName> find(${beanName}Example example);
public Integer count(${beanName}Example example);
#end
}
这两个文件的功能都写在注释之中了,其中velocity的语法我没有写,有兴趣的可以去学习下,下面我们来看看效果图,毕竟好不容易写好了这些代码:
可以看到,这里很方便的就生成了一段公共的代码,各位也可以根据自己公司的逻辑自定义这些templates,只要会一点velocity语法就行。