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 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...
随机推荐
- 003 关于shell基础,大数据的前期准备
一:正则表达式 1.通配符与正则表达式的区别 通配符有 *,?,[]. 区别: 通配符用来匹配文件名 正则表达式是为了匹配字符串 2.“.*”的意思 .:任意字符 *:匹配前一个字符任意次 3.过滤出 ...
- UVa 11059 - Maximum Product 最大乘积【暴力】
题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...
- Python常用模块--base64
作用:对一些保密性不强的信息进行加密,变为人类不能直接理解的字符串,但是可以反向解密,是一种‘防君子,不防小人’的措施. 例如:在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动 ...
- python3之Django表单(一)
1.HTML中的表单 在HTML种,表单是在<form>...</form>种的元素,它允许用户输入文本,选择选项,操作对象等,然后发送这些数据到服务器 表单元素允许用户在表单 ...
- Spring框架学习04——复杂类型的属性注入
代码示例如下: 创建BeanClass实体类 public class BeanClass { private String[] arrs;//数组类型 private List<String& ...
- Unterminated <c:forEach tag
c:forEach tag意思是这一块有语法错误
- window10总提示幸福倒计时,解决方法
删除window用于标识计算机更新的临时文件,需要先停止功能Windows Update. 在开始菜单的“搜索程序和文件”框输入“Services.msc”命令打开服务窗口 打开Windows Upd ...
- 读写锁--DEMO
package com.demo.read.write.lock; import java.util.HashMap; import java.util.Map; import java.util.c ...
- 可以替代putty的ssh客户端
1. Bitvise SSH Client http://www.putty.org/ Bitvise SSH Client is an SSH and SFTP client for Windows ...
- Android Studio下添加assets目录
Android Studio下添加assets目录 分类: Android Studio2013-11-06 18:09 10872人阅读 评论(2) 收藏 举报 android studioasse ...