preface

Velocity provides a template generation tool for dao and Service classes that contain a lot of duplicate code. Today I’m going to teach you how to say goodbye to a lot of duplicate code.

Pay attention to

You can write your own template. For example, we will use cppba-web template directly. For example, you can find velocity syntax in Baidu

Maven configuration

Org. Apache. Velocity velocity of 1.7Copy the code

Creating a template File

Let’s look at the directory structure first:




https://github.com/bigbeef/cppba-codeTemplate

I’ll just post serviceImplTemplate. Java. If you need other template code, you can download it from Github

#set ($domain = $! DomainName. Substring (0, 1). The toLowerCase () + $! domainName.substring(1)) package $! {packageName}.service.impl; import $! {packageName}.core.bean.PageEntity; import $! {packageName}.dao.$! {domainName}Dao; import $! {packageName}.dto.$! {domainName}Dto; import $! {packageName}.dto.BaseDto; import $! {packageName}.entity.$! {domainName}; import $! {packageName}.service.$! {domainName}Service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; / * * * * nickName: developers bumblebee github:https://github.com/bigbeef * * email:[email protected] * generate cppba - codeTemplate velocity templates */ @Service @Transactional public class $! {domainName}ServiceImpl implements $! {domainName}Service{ @Resource private $! {domainName}Dao $! {domain}Dao; @Override public void save($! {domainName} $! {domain}) { $! {domain}Dao.save($! {domain}); } @Override public void delete($! {domainName} $! {domain}) { $! {domain}Dao.delete($! {domain}); } @Override public void update($! {domainName} $! {domain}) { $! {domain}Dao.update($! {domain}); } @Override public $! {domainName} findById(int id) { return ($! {domainName}) $! {domain}Dao.get($! {domainName}.class, id); } @Override public PageEntity<$! {domainName}> query(BaseDto baseDto) { String hql = " select distinct $! {domain} from $! {domainName} $! {domain} where 1=1 "; Map params = new HashMap(); $! {domainName}Dto $! {domain}Dto = ($! {domainName}Dto)baseDto; $! {domainName} $! {domain} = $! {domain}Dto.get$! {domainName}(); int page = $! {domain}Dto.getPage(); int pageSize = $! {domain}Dto.getPageSize(); List list = $! {domain}Dao.query(hql,params,page,pageSize); long count = $! {domain}Dao.count(hql,params); PageEntity<$! {domainName}> pe = new PageEntity<$! {domainName}>(); pe.setCount(count); pe.setList(list); return pe; }}Copy the code

The template to generate

Here is the main function that generates the template:

package com.cppba.core; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Map; import java.util.Properties; / * * * * nickName: developers bumblebee github:https://github.com/bigbeef * email:[email protected] * * / public class Main {static String domainName = "Articles"; Static String packageName = "com.cppba"; Static String templateDir = "\\ SRC \\main\\ webApp \\template\ "; static String sourcePath = System.getProperty("user.dir")+templateDir; static String resultDir = "\\out"; static String targetPath = System.getProperty("user.dir") + resultDir + "\\" + packageName.replace(".", "\\"); public static void main(String []args) throws Exception{ Map map = new HashMap(); map.put("DaoTemplate.java","dao/" + domainName + "Dao.java"); map.put("ServiceTemplate.java","service/" + domainName + "Service.java"); map.put("ServiceImplTemplate.java","service/impl/" + domainName + "ServiceImpl.java"); map.put("DtoTemplate.java","dto/" + domainName + "Dto.java"); for(String templateFile:map.keySet()){ String targetFile = (String) map.get(templateFile); Properties pro = new Properties(); pro.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); pro.setProperty(Velocity.INPUT_ENCODING, "UTF-8"); pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, sourcePath); VelocityEngine ve = new VelocityEngine(pro); VelocityContext context = new VelocityContext(); context.put("domainName",domainName); context.put("packageName",packageName); Template t = ve.getTemplate(templateFile, "UTF-8"); File file = new File(targetPath, targetFile); if (! file.getParentFile().exists()) file.getParentFile().mkdirs(); if (! file.exists()) file.createNewFile(); FileOutputStream outStream = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter(outStream, "UTF-8"); BufferedWriter sw = new BufferedWriter(writer); t.merge(context, sw); sw.flush(); sw.close(); outStream.close(); System.out.println(" Java file generated successfully :" + (targetPath + targetFile).replaceAll("/", "\\\\")); }}}Copy the code

Generating a Java file

We can change domainName and packageName to change our packageName and class name. Let’s run the following:




https://github.com/bigbeef/cppba-codeTemplate

If we see that the build is successful, let’s open articlesServiceImp.java and take a look:

package com.cppba.service.impl; import com.cppba.core.bean.PageEntity; import com.cppba.dao.ArticlesDao; import com.cppba.dto.ArticlesDto; import com.cppba.dto.BaseDto; import com.cppba.entity.Articles; import com.cppba.service.ArticlesService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; / * * * * nickName: developers bumblebee github:https://github.com/bigbeef * * email:[email protected] * generate cppba - codeTemplate velocity templates */ @Service @Transactional public class ArticlesServiceImpl implements ArticlesService{ @Resource private ArticlesDao articlesDao; @Override public void save(Articles articles) { articlesDao.save(articles); } @Override public void delete(Articles articles) { articlesDao.delete(articles); } @Override public void update(Articles articles) { articlesDao.update(articles); } @Override public Articles findById(int id) { return (Articles) articlesDao.get(Articles.class, id); } @Override public PageEntity query(BaseDto baseDto) { String hql = " select distinct articles from Articles articles where 1=1 "; Map params = new HashMap(); ArticlesDto articlesDto = (ArticlesDto)baseDto; Articles articles = articlesDto.getArticles(); int page = articlesDto.getPage(); int pageSize = articlesDto.getPageSize(); List list = articlesDao.query(hql,params,page,pageSize); long count = articlesDao.count(hql,params); PageEntity pe = new PageEntity(); pe.setCount(count); pe.setList(list); return pe; }}Copy the code

It was generated successfully and we copied it to CPPba-web and it worked perfectly!

Reference items: github.com/bigbeef/cpp… Github address: github.com/bigbeef Personal blog: www.cppba.com