数据库逆向框架代码生成工具:MyBatis Generator的使用
2016-09-08 17:18
573 查看
数据库逆向框架代码生成工具:MyBatis Generator的使用
转自 http://my.oschina.net/lujianing/blog/200135
摘要
通过MyBatis Generator可逆向生成持久层的代码,与Spring实现无缝融合,本文是基于Intellij+Maven实现。
MyBatis Generator的详细介绍http://mybatis.github.io/generator/index.html
MyBatis Generator With Maven http://mybatis.github.io/generator/running/runningWithMaven.html
2.在pom文件中,添加MBG插件,IDE会自动帮我们下载插件
(如果没反应,可以点开右侧Maven Project选项卡刷新以下)
(插件1.3.0有点小bug,不能去掉生成的注释)
3.在src/main/resource目录下创建generatorConfig.xml文件
(官方配置以及说明:http://mybatis.github.io/generator/configreference/xmlconfig.html)
4.点击Maven Project——项目——Plugins——mybatis generator——Run Maven build
5.可以根据自己项目的配置,把生成的代码拷贝到自己的项目中去
3.(1)模糊查询并且排序 (2)大于等于某个分数 并且小于某个分数的查询
Example中提供了Critertia,一种面向对象的查询方式,并且根据实体类中字段的属性,生成不同的操作。
当然你也可以根据实际需要直接使用实体类进行增删改查。
转自 http://my.oschina.net/lujianing/blog/200135
摘要
通过MyBatis Generator可逆向生成持久层的代码,与Spring实现无缝融合,本文是基于Intellij+Maven实现。
MyBatis Generator的详细介绍http://mybatis.github.io/generator/index.html
MyBatis Generator With Maven http://mybatis.github.io/generator/running/runningWithMaven.html
1前言
前段时间根据工作需要,使用Spring+Mybatis完成了一个功能模块,领导推荐了MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比较好,可以自由组合完成比较复杂的查询,当然更复杂的就需要手动写了,下面整理下基本使用2搭建逆向工程
1.创建一个Maven项目:File——New Project——Maven2.在pom文件中,添加MBG插件,IDE会自动帮我们下载插件
(如果没反应,可以点开右侧Maven Project选项卡刷新以下)
(插件1.3.0有点小bug,不能去掉生成的注释)
<build> <finalName>mybatis_generator</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </plugin> </plugins> </build>
3.在src/main/resource目录下创建generatorConfig.xml文件
(官方配置以及说明:http://mybatis.github.io/generator/configreference/xmlconfig.html)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--数据库驱动jar --> <classPathEntry location="E:\mysql-connector-java-5.1.7-bin.jar" /> <context id="DB2Tables" targetRuntime="Ibatis2Java5"> <!--去除注释 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/news" userId="root" 13ca1 password=""> </jdbcConnection> <!--默认false Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC. --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) --> <javaModelGenerator targetPackage="com.qianyan.model" targetProject="MAVEN"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成SQLMAP文件 --> <sqlMapGenerator targetPackage="com.qianyan.persistence.ibatis" targetProject="MAVEN"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" --> <javaClientGenerator type="SPRING" targetPackage="com.qianyan.persistence.dao" targetProject="MAVEN"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等--> <table tableName="USER" domainObjectName="User" > </table> </context> </generatorConfiguration>
4.点击Maven Project——项目——Plugins——mybatis generator——Run Maven build
5.可以根据自己项目的配置,把生成的代码拷贝到自己的项目中去
public int updateStudyTeamInfo(StudyTeamInfo st){ StudyTeamInfoExample ste = new StudyTeamInfoExample(); ste.createCriteria().andTeamIdEqualTo(st.getTeamId()); return studyTeamInfoDAO.updateByExampleSelective(st,ste); }
3.(1)模糊查询并且排序 (2)大于等于某个分数 并且小于某个分数的查询
public List<StudyTeamInfo> getStudyTeamInfoByName(String team_name){ StudyTeamInfoExample se = new StudyTeamInfoExample(); se.createCriteria().andTeamNameLike("%"+team_name+"%").andEnableEqualTo((short)1); se.setOrderByClause("team_score desc"); List<StudyTeamInfo> ls = studyTeamInfoDAO.selectByExample(se); if(ls!=null&&ls.size()>0){ return ls; } return null; }
public StudyTeamLevel getStudyTeamLevel(long score){ StudyTeamLevelExample le = new StudyTeamLevelExample(); le.createCriteria().andNeedScoreLessThanOrEqualTo(score).andUpScoreGreaterThan(score); List<StudyTeamLevel> ls = studyTeamLevelDAO.selectByExample(le); if(ls!=null&&ls.size()>0){ return ls.get(0); } return null; }
Example中提供了Critertia,一种面向对象的查询方式,并且根据实体类中字段的属性,生成不同的操作。
当然你也可以根据实际需要直接使用实体类进行增删改查。
相关文章推荐
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- 数据库逆向代码生成工具:MyBatis Generator的使用
- MybatisGenerator 数据库逆向生成代码工具
- 基于数据库的自动化生成工具,自动生成JavaBean、数据库文档、框架代码等(v5.8.8版)
- WCF开发框架形成之旅---结合代码生成工具实现快速开发
- WCF开发框架形成之旅---结合代码生成工具实现快速开发
- CSharp数据库代码生成工具
- eclipse使用hibernate工具数据库逆向生成实体类
- 黄聪:C#代码生成工具:文本模板初体验 使用T4批量修改实体框架(Entity Framework)的类名
- [c#]一步一步开发自己的自动代码生成工具之一:获取sql2005的数据库表结构
- 按Sybase的PowerDesigner工具设计的数据库模型 ---> 解析生成能兼容多种数据库的相应的C#底层代码
- 从零开始学YII2框架(五)快速生成代码工具 Gii 的使用
- 按Sybase的PowerDesigner工具设计的数据库模型 ---> 解析生成能兼容多种数据库的相应的C#底层代码