spring-mybatis代码生成插件,与实例展示
前段时间看了张开涛写的代码生成插件,感觉思路很好,通过连接库然后获取数据库表信息,然后用户在界面中勾选要映射的策略,映射的字段,然后可以自动生成业务代码.
基于开涛的思路,自己写了一个简易插件,去掉了连接库操作,改用注解,使用时需要先自行定义实体类,然后使用注解定义那部分映射,如何映射,然后启动生成,进而生成业务代码
功能描述:
支持
1.注解自定义映射,.
2.扫描实体类生成对应的业务代码(业务层、事务层、持久层[接口+接口对应的映射mapper.xml文件])
3.编码自定义,支持生成注解
4.支持模块扩展,可以自己定义生成哪个模块,也可以扩展生成模块(这块没有设计好,扩展代码比较复杂)
不支持
复杂结构对象映射
目前生成映射xml中的SQL脚本为mysql,只是为了练手而写,所以后期有时间的话,扩展自定义数据库
使用实体:
定义实体如下:
//映射表别名
@GeneratorTable(name = "tb_member")
public class Member implements Serializable{
//映射该字段,并且为主键,自定义字段别名
@GeneratorField(primaryKey = true, name = "m_Id")
private Integer id;
@GeneratorField(name = "member_name")
private String memberName;
//映射时不映射该字段
@AbolishedField
private String address;
//不使用注解,默认为使用属性名进行映射
private String zipCode;
//getter and setter
}
生成配置
public static void main(String [] args){
GeneratorConfig config = new GeneratorConfig();
//代码根路径
config.setSrcRoot("src/main/java");
//扫描的包,默认支持递归扫描该包下的东西
config.setScanBaseModelPackage("com.dobby.code.make.model");
config.setEncoding("UTF-8");
//生成的代码存放的包
config.setCodeGeneratorPackage("com.dobby.autocode");
//如果生成的类文件已经存在,是否覆盖
config.setCodeExistsOverride(false);
//生成注解代码
config.setAnnotationSupport(true);
//使用插件生成
new Generator(config).generatorCode();
}
生成前的代码结构:
生成的代码模块,下图标红区域
生成的业务层代码:
package com.dobby.autocode.service;
import java.util.List;
import com.dobby.common.base.BaseService;
import com.dobby.code.make.model.Member;
/**
*
* User: dobby
* Date: 2014-11-26
* Time: 21:10:06
* Version: 1.0
*
*/
public interface MemberService extends BaseService<Member,java.lang.Integer>{
}
package com.dobby.autocode.service.impl; import java.util.List; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import com.dobby.common.page.Page; import com.dobby.autocode.service.MemberService; import com.dobby.autocode.manager.MemberManager; import com.dobby.code.make.model.Member; /** * * User: dobby * Date: 2014-11-26 * Time: 21:10:06 * Version: 1.0 * */ @Service(value = "memberService") public class MemberServiceImpl implements MemberService{ @Autowired private MemberManager memberManager; /** * save member <br/> * * @param member * member * @return * effect of row number */ @Override public int save(Member member){ return this.memberManager.save(member); } /** * update member <br/> * * @param member * member * @return * effect of row number */ @Override public int update(Member member) { return this.memberManager.update(member); } /** * delete member by primary key <br/> * * @param primaryKey * primaryKey * @return * effect of row number */ @Override public int delete(java.lang.Integer primaryKey){ return this.memberManager.delete(primaryKey); } /** * delete member by condition <br/> * * @param member * condition member * @return * effect of row number */ @Override public int deleteObjects(Member member) { return this.memberManager.deleteObjects(member); } /** * select member by primary key <br/> * * @param primaryKey * primaryKey * @return * with the conditions of the one object */ @Override public Member select(java.lang.Integer primaryKey) { return this.memberManager.select(primaryKey); } /** * select list member by condition <br/> * * @param member * condition member * @return * with the conditions of the result set */ @Override public List<Member> selectObjects(Member member){ return this.memberManager.selectObjects(member); } /** * select list member by condition for page show<br/> * * @param member * query condition * @param pageNow * get data for which page * @param pageSize * pageSize for each page * @return * with the conditions of the result set */ @Override public Page<Member> selectPage(Member member, Integer pageNow, Integer pageSize){ return this.memberManager.selectPage(member , pageNow , pageSize); } public void setMemberManager (MemberManager memberManager) { this.memberManager = memberManager; } }
生成的事物层代码
package com.dobby.autocode.manager;
import com.dobby.common.base.BaseManager;
import com.dobby.code.make.model.Member;
/**
*
* User: dobby
* Date: 2014-11-26
* Time: 21:10:06
* Version: 1.0
*
*/
public interface MemberManager extends BaseManager<Member,java.lang.Integer> {
}
package com.dobby.autocode.manager.impl; import java.util.List; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; import com.dobby.common.page.Page; import com.dobby.autocode.manager.MemberManager; import com.dobby.autocode.dao.MemberDao; import com.dobby.code.make.model.Member; /** * * User: dobby * Date: 2014-11-26 * Time: 21:10:06 * Version: 1.0 * */ @Component(value = "memberManager") public class MemberManagerImpl implements MemberManager{ @Resource( name = "transactionMemberManager") private PlatformTransactionManager transactionMemberManager; @Autowired private MemberDao memberDao; public void setTransactionMemberManager (PlatformTransactionManager transactionMemberManager) { this.transactionMemberManager = transactionMemberManager; } public void setMemberDao (MemberDao memberDao) { this.memberDao = memberDao; } @Override public int save(final Member member){ int result = 0; TransactionTemplate template = new TransactionTemplate(this.transactionMemberManager); result = (Integer)template.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return memberDao.insert(member); } }); return result; } @Override public int update(final Member member) { int result = 0; TransactionTemplate template = new TransactionTemplate(this.transactionMemberManager); result = (Integer)template.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return memberDao.update(member); } }); return result; } @Override public int delete(final java.lang.Integer primaryKey){ int result = 0; TransactionTemplate template = new TransactionTemplate(this.transactionMemberManager); result = (Integer)template.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return memberDao.delete(primaryKey); } }); return result; } @Override public int deleteObjects(final Member member) { int result = 0; TransactionTemplate template = new TransactionTemplate(this.transactionMemberManager); result = (Integer)template.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return memberDao.deleteObjects(member); } }); return result; } @Override public Member select(java.lang.Integer primaryKey) { return this.memberDao.select(primaryKey); } @Override public List<Member> selectObjects(Member member){ return this.memberDao.selectObjects(member); } @Override public Page<Member> selectPage(Member member, Integer pageNow, Integer pageSize){ Page<Member> page = null; try { List<Member> list = this.memberDao.selectPagination(member, (pageNow - 1) * pageSize, pageSize); int recordTotal = this.memberDao.selectPageCount(member); page = new Page<Member>(list,recordTotal,pageNow); } catch (Exception e) { e.printStackTrace(); } return page; } }
生成的持久层代码
package com.dobby.autocode.dao;
import java.util.List;
import com.dobby.common.base.BaseCudDao;
import com.dobby.common.base.BaseReadDao;
import com.dobby.code.make.model.Member;
/**
*
* User: dobby
* Date: 2014-11-26
* Time: 21:10:06
* Version: 1.0
*
*/
public interface MemberDao extends BaseReadDao<Member,java.lang.Integer>,BaseCudDao<Member,java.lang.Integer>{
}
<?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.dobby.autocode.dao.MemberDao"> <!-- all table column--> <sql id="allColumns"> zipCode, memberName </sql> <!-- query entity by id --> <select id="select" parameterType="java.lang.Integer" resultType="member" useCache="true"> SELECT <include refid="allColumns" /> FROM tb_member WHERE m_Id=#{pKey} </select> <!-- query entity by condition for page --> <select id="selectPagination" resultType="member" useCache="true"> SELECT <include refid="allColumns" /> FROM tb_member WHERE 1 = 1 <if test="object.zipCode!= null and ''!=object.zipCode"> <![CDATA[ AND zipCode = #{object.zipCode} ]]> </if> <if test="object.memberName!= null and ''!=object.memberName"> <![CDATA[ AND member_name = #{object.memberName} ]]> </if> LIMIT #{start},#{size} </select> <!-- query entity collection by condition for page total --> <select id="selectPageCount" parameterType="member" resultType="java.lang.Integer" useCache="true"> SELECT count(0) FROM tb_member WHERE 1 = 1 <if test="object.zipCode!= null and ''!=object.zipCode"> <![CDATA[ AND zipCode = #{object.zipCode} ]]> </if> <if test="object.memberName!= null and ''!=object.memberName"> <![CDATA[ AND member_name = #{object.memberName} ]]> </if> </select> <!-- query entity by condition --> <select id="selectObjects" parameterType="member" useCache="true"> SELECT <include refid="allColumns" /> FROM tb_member WHERE 1 = 1 <if test="object.zipCode!= null and ''!=object.zipCode"> <![CDATA[ AND zipCode = #{object.zipCode} ]]> </if> <if test="object.memberName!= null and ''!=object.memberName"> <![CDATA[ AND member_name = #{object.memberName} ]]> </if> </select> <!-- insert entity --> <insert id="insert" parameterType="member" flushCache="true"> INSERT INTO tb_member(<include refid="allColumns"/>) VALUES (#{zipCode}, #{memberName}) </insert> <!-- insert entity by primary key--> <delete id="delete" parameterType="java.lang.Integer" flushCache="true"> DELETE FROM tb_member WHERE m_Id=#{pKey} </delete> <!-- delete entity by object condition--> <delete id="deleteObjects" parameterType="member" flushCache="true"> DELETE FROM tb_member WHERE 1 = 1 <if test="object.zipCode!= null and ''!=object.zipCode"> <![CDATA[ AND zipCode = #{object.zipCode} ]]> </if> <if test="object.memberName!= null and ''!=object.memberName"> <![CDATA[ AND member_name = #{object.memberName} ]]> </if> </delete> <!-- update entity by primary key--> <update id="update" parameterType="member" flushCache="true"> UPDATE tb_member <set> <if test="object.zipCode!= null and ''!=object.zipCode"> <![CDATA[ AND zipCode = #{object.zipCode} ]]> </if> <if test="object.memberName!= null and ''!=object.memberName"> <![CDATA[ AND member_name = #{object.memberName} ]]> </if> </set> WHERE m_Id=#{pKey} </update> </mapper>
说明:生成代码并没有做真正的业务测试,可能某些地方有问题,后续有时间会完善.
附件:
插件包: https://files.cnblogs.com/dennisit/%E7%94%9F%E6%88%90%E6%8F%92%E4%BB%B6%E5%8C%85.zip
注解模块实现说明:http://www.cnblogs.com/dennisit/p/4125103.html
热爱生活,热爱Coding,敢于挑战,用于探索 ...