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>
说明:生成代码并没有做真正的业务测试,可能某些地方有问题,后续有时间会完善.
附件:
插件包: http://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
转载请注明出处:[http://www.cnblogs.com/dennisit/p/4125000.html]
spring-mybatis代码生成插件,与实例展示的更多相关文章
- Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...
- Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...
- Spring+Mybatis+SpringMVC后台与前台分页展示实例
摘要:本文实现了一个后台由spring+Mybatis+SpringMVC组成,分页采用PageHelper,前台展示使用bootstrap-paginator来显示效果的分页实例.整个项目由mave ...
- Spring+Mybatis+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...
- SpringMVC笔记——Spring+MyBatis组合开发简单实例
简介 SSH框架很强大,适合大型项目开发.但学无止境,多学会一门框架组合开发会让自己增值许多. SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的.在SSM框架搭建之前,我们先学习 ...
- spring+mybatis的插件【shardbatis2.0】+mysql+java自定义注解实现分表
一.业务场景分析 只有大表才需要分表,而且这个大表还会有经常需要读的需要,即使经过sql服务器优化和sql调优,查询也会非常慢.例如共享汽车的定位数据表等. 二.实现步骤 1.准备pom依赖 < ...
- Mybatis Generator插件升级版
一.目的: 1. *mapper.java 文件名称 改为*DAO.java2. mapper以及mapper.xml 重复执行,只会覆盖原模板方法,不会覆盖自定义方法3. 实体类添加中文注释 二.步 ...
- 通过eclipse mybatis generater代码生成插件自动生成代码
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件.通过在Ecl ...
- (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示
http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...
随机推荐
- struts2之OGNL和struts2标签库和ValueStack对象
OGNL简介: (1)OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. struts2框架默认就支持Ognl表达式语言(所以 ...
- (canvas)两小球碰撞后的速度问题研究
这两天在研究canvas碰撞 先把小球开始运动的图拿出来 参考了一下别的的代码,在两个小球碰撞处理上,我觉得不完善 怎么样处理才算完善呢,当然是要用高中物理学的动量守恒了和机械能守恒了 机械能守恒我其 ...
- 【AtCoder】ARC083
C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...
- 098实战 Job的调度
一:介绍 1.job调度 容量调度:Apache Hadoop的默认方式 公平调度:CDH版本的Hadoop的默认方式 2.公平调度 是一种资源分配方式,在yarn的整个生命周期中,所有的applic ...
- header 和http状态码
select qg.*,gg.finalgrade,gi.itemname,gi.courseid,gi.itemmodule,gi.itemtype from mymdl_quiz_grades a ...
- How to uninstall Maya on a Mac
How to uninstall Maya on a Mac Products and versions covered By: Support Jun 15 2018 SHARE ADD TO ...
- js异步刷新局部页面
真不想说博客园的Markdown编辑器,我发表到我的个人博客上多好看的一篇文章,发到博客园上格式就成这个鸟样了,哎,不发现到博客首页了,就个人存个档吧 最近在做一个异步刷新页面中的局部,这样做可以防出 ...
- java 同步 synchronized
http://www.cnblogs.com/Qian123/p/5691705.html http://www.cnblogs.com/GnagWang/archive/2011/02/27/196 ...
- Wamp修改端口
WAMP装好之后默认的端口是80,但是这个80端口呢,可以热门端口啊,迅雷,IIS都挺喜欢,在WIN7下本来还有1个进程也点80,让人伤脑筋啊,所以把它改掉,方法如下: 一.修改APACHE的监听端口 ...
- 安装Appium-desktop,并连接模拟器!Appium-Python-Client
1.下载appium-desktop https://github.com/appium/appium-desktop/releases 2.双击 exe 文件,然后,等待安装完就好了 3.启动app ...