Commit 12ade8bd by gechengyang

提交模板化代码

parent 1eab7759
......@@ -26,7 +26,7 @@
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" tableName="CPD">
<!-- <table enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" tableName="CPD">
<property name="useActualColumnNames" value="true" />
</table>
<table enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" tableName="CPT">
......@@ -69,8 +69,10 @@
<table enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" tableName="CBE">
<property name="useActualColumnNames" value="true" />
</table>
</table> -->
<table enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" tableName="COU">
<property name="useActualColumnNames" value="true" />
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
......@@ -27,6 +27,14 @@
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- dbcp数据库连接池  -->
<dependency>
<groupId>commons-dbcp</groupId>
......
package com.brilliance.remit.common.util;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
// 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
\ No newline at end of file
package com.brilliance.remit.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.brilliance.remit.common.response.ErrorCodes;
import com.brilliance.remit.common.response.ResultUtil;
import com.brilliance.remit.service.CpdService;
/**
This java was generated by Auto Generator.
created at time 2018-12-17 17:14:05
**/
@Controller
public class CpdController {
@Autowired
private CpdService cpdService;
private static final Log log = LogFactory.getLog(CpdController.class);
@ResponseBody
@RequestMapping(value = "/cpd", method = RequestMethod.POST)
public Object login(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
/**
you can do business hanle here.
**/
return ResultUtil.result(ErrorCodes.SUCCESS, "sucess", null);
}
}
......@@ -28,7 +28,7 @@ public class RemitController {
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public Object login(HttpServletRequest request, HttpSession session) {
remitService.test();
remitService.save();
return ResultUtil.result(ErrorCodes.SUCCESS, "ok", null);
}
......@@ -36,7 +36,7 @@ public class RemitController {
@RequestMapping(value = "/tet1", method = RequestMethod.GET)
public Object login(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
System.out.println(dataMap);
remitService.test();
remitService.save();
return ResultUtil.result(ErrorCodes.SUCCESS, "ok", null);
}
}
package com.brilliance.remit.db.dao;
import com.brilliance.remit.db.model.Cou;
public interface CouMapper {
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
int deleteByPrimaryKey(String COUNAM);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
int insert(Cou record);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
int insertSelective(Cou record);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
Cou selectByPrimaryKey(String COUNAM);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
int updateByPrimaryKeySelective(Cou record);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table COU
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
int updateByPrimaryKey(Cou record);
}
\ No newline at end of file
<?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.brilliance.remit.db.dao.CouMapper">
<resultMap id="BaseResultMap" type="com.brilliance.remit.db.model.Cou">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
<id column="COUNAM" jdbcType="VARCHAR" property="COUNAM" />
<result column="COUSTP" jdbcType="DECIMAL" property="COUSTP" />
<result column="COUVAL" jdbcType="DECIMAL" property="COUVAL" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
COUNAM, COUSTP, COUVAL
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
select
<include refid="Base_Column_List" />
from COU
where COUNAM = #{COUNAM,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
delete from COU
where COUNAM = #{COUNAM,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.brilliance.remit.db.model.Cou">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
insert into COU (COUNAM, COUSTP, COUVAL
)
values (#{COUNAM,jdbcType=VARCHAR}, #{COUSTP,jdbcType=DECIMAL}, #{COUVAL,jdbcType=DECIMAL}
)
</insert>
<insert id="insertSelective" parameterType="com.brilliance.remit.db.model.Cou">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
insert into COU
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="COUNAM != null">
COUNAM,
</if>
<if test="COUSTP != null">
COUSTP,
</if>
<if test="COUVAL != null">
COUVAL,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="COUNAM != null">
#{COUNAM,jdbcType=VARCHAR},
</if>
<if test="COUSTP != null">
#{COUSTP,jdbcType=DECIMAL},
</if>
<if test="COUVAL != null">
#{COUVAL,jdbcType=DECIMAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.brilliance.remit.db.model.Cou">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
update COU
<set>
<if test="COUSTP != null">
COUSTP = #{COUSTP,jdbcType=DECIMAL},
</if>
<if test="COUVAL != null">
COUVAL = #{COUVAL,jdbcType=DECIMAL},
</if>
</set>
where COUNAM = #{COUNAM,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.brilliance.remit.db.model.Cou">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Dec 17 14:04:39 CST 2018.
-->
update COU
set COUSTP = #{COUSTP,jdbcType=DECIMAL},
COUVAL = #{COUVAL,jdbcType=DECIMAL}
where COUNAM = #{COUNAM,jdbcType=VARCHAR}
</update>
</mapper>
\ No newline at end of file
package com.brilliance.remit.db.model;
import java.math.BigDecimal;
public class Cou {
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column COU.COUNAM
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
private String COUNAM;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column COU.COUSTP
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
private BigDecimal COUSTP;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column COU.COUVAL
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
private BigDecimal COUVAL;
/**
* This method was generated by MyBatis Generator. This method returns the value of the database column COU.COUNAM
* @return the value of COU.COUNAM
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public String getCOUNAM() {
return COUNAM;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of the database column COU.COUNAM
* @param COUNAM the value for COU.COUNAM
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public void setCOUNAM(String COUNAM) {
this.COUNAM = COUNAM;
}
/**
* This method was generated by MyBatis Generator. This method returns the value of the database column COU.COUSTP
* @return the value of COU.COUSTP
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public BigDecimal getCOUSTP() {
return COUSTP;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of the database column COU.COUSTP
* @param COUSTP the value for COU.COUSTP
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public void setCOUSTP(BigDecimal COUSTP) {
this.COUSTP = COUSTP;
}
/**
* This method was generated by MyBatis Generator. This method returns the value of the database column COU.COUVAL
* @return the value of COU.COUVAL
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public BigDecimal getCOUVAL() {
return COUVAL;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of the database column COU.COUVAL
* @param COUVAL the value for COU.COUVAL
* @mbggenerated Mon Dec 17 14:04:39 CST 2018
*/
public void setCOUVAL(BigDecimal COUVAL) {
this.COUVAL = COUVAL;
}
}
\ No newline at end of file
package com.brilliance.remit.service;
public interface CntService {
int getCntInr(String nam);
}
package com.brilliance.remit.service;
/**
This java was generated by Auto Generator.
created at time 2018-12-17 17:14:05
**/
public interface CpdService {
public void sample();
}
......@@ -2,6 +2,6 @@ package com.brilliance.remit.service;
public interface RemitService {
public void test();
public boolean save();
}
package com.brilliance.remit.service.imp;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brilliance.remit.db.dao.CouMapper;
import com.brilliance.remit.db.model.Cou;
import com.brilliance.remit.service.CntService;
@Service("cntService")
public class CntServiceImpl implements CntService {
@Autowired
private CouMapper couMapper;
@Override
public synchronized int getCntInr(String nam) {
Cou cnt = couMapper.selectByPrimaryKey(nam);
if (cnt == null) {
cnt = new Cou();
cnt.setCOUNAM(nam);
cnt.setCOUSTP(new BigDecimal(1));
cnt.setCOUVAL(new BigDecimal(1));
couMapper.insert(cnt);
} else {
cnt.setCOUVAL(new BigDecimal(cnt.getCOUVAL().intValue() + 1));
couMapper.updateByPrimaryKey(cnt);
}
return cnt.getCOUVAL().intValue();
}
}
package com.brilliance.remit.service.imp;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import com.brilliance.remit.common.util.DbUtil;
import com.brilliance.remit.common.util.SpringContextUtil;
import com.brilliance.remit.service.CntService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.brilliance.remit.service.CpdService;
import com.brilliance.remit.db.dao.CpdMapper;
/**
This java was generated by Auto Generator.
created at time 2018-12-17 17:14:05
**/
@Service("cpdService")
public class CpdServiceImp implements CpdService {
@Autowired
private CpdMapper cpdMapper;
@Resource(name = "txManager")
private DataSourceTransactionManager transactionManager;
private CntService cntService = (CntService) SpringContextUtil.getApplicationContext().getBean(CntService.class);
private static Log log = LogFactory.getLog(CpdServiceImp.class);
@Override
public void sample() {
// 事务操作
TransactionStatus status = transactionManager.getTransaction(DbUtil.getDbDefinition());
try {
//you can do business hanle herer
transactionManager.commit(status);
// get inr method
cntService.getCntInr("XXX");
} catch (Exception e) {
log.error("error", e);
transactionManager.rollback(status);
}
}
}
......@@ -2,18 +2,22 @@ package com.brilliance.remit.service.imp;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import com.brilliance.remit.common.util.DbUtil;
import com.brilliance.remit.common.util.SpringContextUtil;
import com.brilliance.remit.db.dao.CpdMapper;
import com.brilliance.remit.db.dao.TrnMapper;
import com.brilliance.remit.db.model.Cpd;
import com.brilliance.remit.service.CntService;
import com.brilliance.remit.service.RemitService;
@Service
@Service("remitService")
public class RemitServiceImp implements RemitService {
@Autowired
private CpdMapper cpdMapper;
......@@ -22,9 +26,11 @@ public class RemitServiceImp implements RemitService {
private static int i = 10;
@Resource(name = "txManager")
private DataSourceTransactionManager transactionManager;
private CntService cntService = (CntService) SpringContextUtil.getApplicationContext().getBean(CntService.class);
private static Log log = LogFactory.getLog(RemitServiceImp.class);
@Override
public void test() {
public boolean save() {
// 事务操作
TransactionStatus status = transactionManager.getTransaction(DbUtil.getDbDefinition());
try {
......@@ -32,14 +38,14 @@ public class RemitServiceImp implements RemitService {
cpd.setINR(String.valueOf(i));
i++;
cpdMapper.insert(cpd);
transactionManager.commit(status);
cpdMapper.insert(cpd);
System.out.println("insert cpd success !!!");
cntService.getCntInr("AAA");
transactionManager.commit(status);
} catch (Exception e) {
e.printStackTrace();
log.error("error", e);
transactionManager.rollback(status);
return false;
}
return true;
}
......
package com.brilliance.remit.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.brilliance.remit.common.response.ErrorCodes;
import com.brilliance.remit.common.response.ResultUtil;
import com.brilliance.remit.service.${entity.name}Service;
/**
This java was generated by Auto Generator.
created at time ${entity.date}
**/
@Controller
public class ${entity.name}Controller {
@Autowired
private ${entity.name}Service ${entity.lowerName}Service;
private static final Log log = LogFactory.getLog(${entity.name}Controller.class);
@ResponseBody
@RequestMapping(value = "/${entity.lowerName}", method = RequestMethod.POST)
public Object login(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
/**
you can do business hanle here.
**/
return ResultUtil.result(ErrorCodes.SUCCESS, "sucess", null);
}
}
......@@ -12,4 +12,16 @@ CREATE TABLE TRN (INR CHAR(8), INIDATTIM TIMESTAMP, INIFRM CHAR(6), INIUSR VARCH
CREATE TABLE CBB (INR CHAR(8), OBJTYP CHAR(6), OBJINR CHAR(8), CBC CHAR(6), EXTID CHAR(16), BEGDAT TIMESTAMP, ENDDAT TIMESTAMP, CUR CHAR(3), AMT DECIMAL(18,3), CBEINR CHAR(8), XRFCUR CHAR(3), XRFAMT DECIMAL(18,3), COMCUR CHAR(3), COMAMT DECIMAL(18,3), XCOCUR CHAR(3), XCOAMT DECIMAL(18,3),PRIMARY KEY(inr));
CREATE TABLE CBE (INR CHAR(8), OBJTYP CHAR(6), OBJINR CHAR(8), EXTID CHAR(16), CBT CHAR(6), TRNTYP CHAR(6), TRNINR CHAR(8), DAT TIMESTAMP, CUR CHAR(3), AMT DECIMAL(18,3), RELFLG CHAR(1), CREDAT TIMESTAMP, XRFCUR CHAR(3), XRFAMT DECIMAL(18,3), NAM CHAR(40), ACC CHAR(34), ACC2 CHAR(34), OPTDAT TIMESTAMP, RELCSHPCT DECIMAL(7,4), NOMPCT DECIMAL(7,4), GLEDAT TIMESTAMP,PRIMARY KEY(inr));
create table cou
(
counam varchar2(50),
coustp int,
couval int,
primary key (counam)
);
CREATE TABLE OIT (INR CHARACTER(8), OBJTYP CHARACTER(6), OBJINR CHARACTER(8), SUBTYP CHARACTER(3), SUBBUS CHARACTER(2), INFLEV CHARACTER(1), INFTXT LONG VARCHAR, VER CHARACTER(4), ETGEXTKEY CHARACTER(8)
,primary key(INR));
package com.brilliance.remit.service;
/**
This java was generated by Auto Generator.
created at time ${entity.date}
**/
public interface ${entity.name}Service {
public void sample();
}
package com.brilliance.remit.service.imp;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import com.brilliance.remit.common.util.DbUtil;
import com.brilliance.remit.common.util.SpringContextUtil;
import com.brilliance.remit.service.CntService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.brilliance.remit.service.${entity.name}Service;
import com.brilliance.remit.db.dao.${entity.name}Mapper;
/**
This java was generated by Auto Generator.
created at time ${entity.date}
**/
@Service("${entity.lowerName}Service")
public class ${entity.name}ServiceImp implements ${entity.name}Service {
@Autowired
private ${entity.name}Mapper ${entity.lowerName}Mapper;
@Resource(name = "txManager")
private DataSourceTransactionManager transactionManager;
private CntService cntService = (CntService) SpringContextUtil.getApplicationContext().getBean(CntService.class);
private static Log log = LogFactory.getLog(${entity.name}ServiceImp.class);
@Override
public void sample() {
// 事务操作
TransactionStatus status = transactionManager.getTransaction(DbUtil.getDbDefinition());
try {
//you can do business hanle herer
transactionManager.commit(status);
// get inr method
cntService.getCntInr("XXX");
} catch (Exception e) {
log.error("error", e);
transactionManager.rollback(status);
}
}
}
......@@ -35,6 +35,7 @@
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="SpringContextUtil" class="com.brilliance.remit.common.util.SpringContextUtil" scope="singleton"></bean>
<!-- 配置mapper对应的dao和mybatis工厂  -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
......
package com.brilliance.generate;
public class Entity {
private String name;
private String date;
private String lowerName;
public String getName() {
return name;
}
public String getDate() {
return date;
}
public String getLowerName() {
return lowerName;
}
public void setName(String name) {
this.name = name;
}
public void setDate(String date) {
this.date = date;
}
public void setLowerName(String lowerName) {
this.lowerName = lowerName;
}
}
package com.brilliance.generate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class Genarate {
public static void main(String[] args) throws FileNotFoundException, IOException {
String rootPath = "G:/work/cbpp/remit";
String name = "cpd";
name = name.substring(0, 1).toUpperCase() + name.substring(1);
String servicePath = rootPath + "/src/main/java/com/brilliance/remit/service/" + name + "Service.java";
String serivceImpPath = rootPath + "/src/main/java/com/brilliance/remit/service/imp/" + name + "ServiceImp.java";
String controllerPath = rootPath + "/src/main/java/com/brilliance/remit/controller/" + name + "Controller.java";
IOUtils.write(generateService("cpd", "service.vm").getBytes("utf-8"), new FileOutputStream(new File(servicePath)));
IOUtils.write(generateService("cpd", "serviceImp.vm").getBytes("utf-8"), new FileOutputStream(new File(serivceImpPath)));
IOUtils.write(generateService("cpd", "controller.vm").getBytes("utf-8"), new FileOutputStream(new File(controllerPath)));
}
public static String generateService(String tableName, String vmPath) {
// 初始化参数
Properties properties = new Properties();
// 设置velocity资源加载方式为file
properties.setProperty("resource.loader", "class");
// 设置velocity资源加载方式为file时的处理类
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// 实例化一个VelocityEngine对象
VelocityEngine velocityEngine = new VelocityEngine(properties);
VelocityContext velocityContext = new VelocityContext();
Entity entity = new Entity();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
entity.setDate(simpleDateFormat.format(new Date()));
tableName = tableName.toLowerCase();
entity.setLowerName(tableName);
entity.setName(tableName.substring(0, 1).toUpperCase() + tableName.substring(1));
velocityContext.put("entity", entity);
StringWriter stringWriter = new StringWriter();
// 从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
velocityEngine.mergeTemplate(vmPath, "utf-8", velocityContext, stringWriter);
return stringWriter.toString();
}
}
package org.sss.presentation.noui.api.servlet;
package com.brilliance.http.test;
import java.io.BufferedReader;
import java.io.IOException;
......
package org.sss.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.sss.common.model.IBaseObject;
import org.sss.common.model.IDatafield;
import org.sss.common.model.IModule;
import org.sss.common.model.IModuleList;
import org.sss.common.model.IParent;
public class GsonTest {
public String baseObject2Json(IBaseObject baseObject) {
com.google.gson.Gson gson = new com.google.gson.Gson();
if (baseObject instanceof IModuleList<?>) {
IModuleList<?> moduleList = (IModuleList<?>) baseObject;
List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
for (IModule module : moduleList) {
if (null == module) continue;
Map<String, Object> m = new HashMap<String, Object>();
for (IDatafield<?> datafield : module.getDatafields()) {
Object value = datafield.getValue();
m.put(datafield.getName(), value == null ? "" : value);
}
lst.add(m);
}
return gson.toJson(lst);
}
if (baseObject instanceof IModule) {
Map<String, Object> m = new HashMap<String, Object>();
IModule module = (IModule) baseObject;
for (IDatafield<?> datafield : module.getDatafields()) {
Object value = datafield.getValue();
m.put(datafield.getName(), value == null ? "" : value);
}
return gson.toJson(m);
}
if (baseObject instanceof IDatafield<?>) {
IDatafield<?> datafield = (IDatafield<?>) baseObject;
return "{ " + datafield.getName() + ": " + datafield.getValue() == null
? ""
: datafield.getValue() + " }";
}
return null;
}
public String parent2Json(IParent parent) {
com.google.gson.Gson gson = new com.google.gson.Gson();
return gson.toJson(expandParent(parent));
}
private Object expandParent(IParent parent) {
if (parent instanceof IModuleList<?>) {
IModuleList<?> moduleList = (IModuleList<?>) parent;
List<Object> lst = new ArrayList<Object>();
for (IModule module : moduleList) {
if (null == module) continue;
lst.add(expandParent(module));
}
return lst;
}
if (parent instanceof IModule) {
Map<String, Object> m = new HashMap<String, Object>();
IModule module = (IModule) parent;
for (IDatafield<?> datafield : module.getDatafields()) {
Object value = datafield.getValue();
if (value == null) continue;
m.put(datafield.getUrl().replaceAll("\\\\", "_"), value);
}
for (Iterator<IParent> it = module.getModules().iterator(); it.hasNext();) {
IParent parent2 = it.next();
m.put(parent2.getUrl().replaceAll("\\\\", "_"), expandParent(parent2));
}
return m;
}
return null;
}
}
2018-12-17 16:22:41,448 - ResourceManager : unable to find resource 'service.vm' in any resource loader.
2018-12-17 16:23:42,588 - ResourceManager : unable to find resource '/G:/work/cbpp/remit/target/test-classes/service.vm' in any resource loader.
2018-12-17 16:25:21,768 - ResourceManager : unable to find resource '/G:/work/cbpp/remit/target/test-classes/service.vm' in any resource loader.
2018-12-17 16:29:16,937 - ResourceManager : unable to find resource 'service.vm' in any resource loader.
2018-12-17 16:29:26,286 - ResourceManager : unable to find resource '/service.vm' in any resource loader.
2018-12-17 16:31:38,929 - ResourceManager : unable to find resource 'com/brilliance/velocity/service.vm' in any resource loader.
2018-12-17 16:32:24,956 - ResourceManager : unable to find resource '/G:/work/cbpp/remit/target/classes/com/brilliance/velocity/service.vm' in any resource loader.
2018-12-17 16:37:04,212 - ResourceManager : unable to find resource 'com/brilliance/velocity/service.vm' in any resource loader.
2018-12-17 16:37:14,046 - ResourceManager : unable to find resource 'service.vm' in any resource loader.
2018-12-17 16:37:35,986 - ResourceManager : unable to find resource 'service.vm' in any resource loader.
2018-12-17 16:39:14,589 - ResourceManager : unable to find resource 'service.vm' in any resource loader.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment