Mybatis逆向工程构建项目实例.
2016/11/06更新:
因为有博友可能需要这份代码, 所以我就直接发到百度云上面和大家共享, 如果链接失效请大家留言提示即可.
下载地址: http://pan.baidu.com/s/1i57E8PR
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、pojo等)
有了sql表的结构后, 我们就可以利用逆向工程直接生成相应的Dao和JavaBean代码, 这样能够大大减少我们平时开发的工作量.
但是我还是觉得使用逆向工程局限性很大, 例如我们的逆向工程main方法只能执行一次, 如果再次执行就会继续生成相应的Dao和JavaBean, 除非我们把之前生成的全都删除. 这样对于代码的扩展性就不是很好, 如果我们需要对表结构进行修改, 那么我们就必须对生成的Dao和JavaBean进行一个个修改.
下面就直接进入开发阶段:
1, 数据库表结构
2,将逆向工程导入到Eclipse中
3,使用逆向工程
逆向工程目录结构:
这里的bean和dao都是使用逆向工程自动生成的两个包, 我们只需要将相应的Dao和Javabean拷贝到相应的project下即可.
看下生成Dao和Bean的代码:
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import org.mybatis.generator.api.MyBatisGenerator;
- import org.mybatis.generator.config.Configuration;
- import org.mybatis.generator.config.xml.ConfigurationParser;
- import org.mybatis.generator.internal.DefaultShellCallback;
- public class GeneratorSqlmap {
- public void generator() throws Exception{
- List<String> warnings = new ArrayList<String>();
- boolean overwrite = true;
- File configFile = new File("generatorConfig.xml");
- ConfigurationParser cp = new ConfigurationParser(warnings);
- Configuration config = cp.parseConfiguration(configFile);
- DefaultShellCallback callback = new DefaultShellCallback(overwrite);
- MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
- callback, warnings);
- myBatisGenerator.generate(null);
- }
- public static void main(String[] args) throws Exception {
- try {
- GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
- generatorSqlmap.generator();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
下面就是看下generatorConfig.xml中的一些配置:
- <?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>
- <context id="testTables" targetRuntime="MyBatis3">
- <!-- JavaBean 实现 序列化 接口 -->
- <plugin type="org.mybatis.generator.plugins.SerializablePlugin">
- </plugin>
- <!-- genenat entity时,生成toString -->
- <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
- <!-- 自定义物理分页 可生成支持Mysql数据的limit 不支持Oracle -->
- <plugin type="org.mybatis.generator.plugins.page.PaginationPlugin" />
- <!-- 自定义查询指定字段 -->
- <plugin type="org.mybatis.generator.plugins.field.FieldsPlugin" />
- <!-- 开启支持内存分页 可生成 支持内存分布的方法及参数
- <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
- -->
- <!-- generate entity时,生成hashcode和equals方法
- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
- -->
- <!-- 此处是将Example改名为Criteria 当然 想改成什么都行~ -->
- <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
- <property name="searchString" value="Example$" />
- <!-- 替换后
- <property name="replaceString" value="Criteria" />
- -->
- <property name="replaceString" value="Query" />
- </plugin>
- <!-- 此处是将UserMapper.xml改名为UserDao.xml 当然 想改成什么都行~ -->
- <plugin type="org.mybatis.generator.plugins.rename.RenameSqlMapperPlugin">
- <property name="searchString" value="Mapper" />
- <property name="replaceString" value="Dao" />
- </plugin>
- <!-- 此处是将UserMapper改名为UserDao 接口 当然 想改成什么都行~ -->
- <plugin type="org.mybatis.generator.plugins.rename.RenameJavaMapperPlugin">
- <property name="searchString" value="Mapper$" />
- <property name="replaceString" value="Dao" />
- </plugin>
- <commentGenerator type="org.mybatis.generator.plugins.comment.MyCommentGenerator">
- <!-- 是否去除自动生成的注释 true:是 : false:否
- <property name="suppressAllComments" value="true" />
- -->
- </commentGenerator>
- <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/babasport" userId="root"
- password="123456">
- </jdbcConnection>
- <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
- connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
- userId="yycg"
- password="yycg">
- </jdbcConnection> -->
- <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
- NUMERIC 类型解析为java.math.BigDecimal -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- targetProject:生成PO类的位置 -->
- <javaModelGenerator targetPackage="cn.itcast.core.bean"
- targetProject=".\src">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- targetProject:mapper映射文件生成的位置 -->
- <sqlMapGenerator targetPackage="cn.itcast.core.dao"
- targetProject=".\src">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
- <!-- targetPackage:mapper接口生成的位置 -->
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="cn.itcast.core.dao"
- targetProject=".\src">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="true" />
- </javaClientGenerator>
- <!-- 指定数据库表 -->
- <!-- 用户模块表 -->
- <table schema="" tableName="bbs_buyer" domainObjectName="user.Buyer"/>
- <!-- 商品模块表 -->
- <table schema="" tableName="bbs_product" domainObjectName="product.Product">
- <!-- 商品介绍 大字段映射 -->
- <columnOverride column="description" javaType="String" jdbcType="VARCHAR" />
- <!-- 包装清单 大字段映射 -->
- <columnOverride column="package_list" javaType="String" jdbcType="VARCHAR" />
- <!-- 商品图片 大字段映射 -->
- <columnOverride column="img_url" javaType="String" jdbcType="VARCHAR" />
- </table>
- <table schema="" tableName="bbs_brand" domainObjectName="product.Brand"/>
- <table schema="" tableName="bbs_Color" domainObjectName="product.Color"/>
- <table schema="" tableName="bbs_sku" domainObjectName="product.Sku"/>
- <!-- 订单模块表 -->
- <table schema="" tableName="bbs_order" domainObjectName="order.Order">
- <!-- 支付方式 0:到付 1:在线 2:邮局 3:公司转帐 -->
- <columnOverride column="payment_way" javaType="Integer"/>
- <!-- 货到付款方式.1现金,2POS刷卡 -->
- <columnOverride column="payment_cash" javaType="Integer" />
- <!-- 送货时间 -->
- <columnOverride column="delivery" javaType="Integer"/>
- <!-- 支付状态 :0到付1待付款,2已付款,3待退款,4退款成功,5退款失败 -->
- <columnOverride column="is_paiy" javaType="Integer"/>
- <!-- 订单状态 0:提交订单 1:仓库配货 2:商品出库 3:等待收货 4:完成 5待退货 6已退货 -->
- <columnOverride column="state" javaType="Integer"/>
- <!-- 订单状态 默认Boolean -->
- <columnOverride column="order_state" javaType="Integer"/>
- </table>
- <table schema="" tableName="bbs_detail" domainObjectName="order.Detail"/>
- <!-- 指定数据库所有表
- <table schema="" tableName="%"/>
- -->
- <!-- 有些表的字段需要指定java类型
- <table schema="" tableName="">
- <columnOverride column="" javaType="" />
- </table> -->
- </context>
- </generatorConfiguration>
主要核心内容就是在这个配置文件中写入相应的配置, 具体的使用方法和配置注释中都有说明.
4, 使用逆向工程进行增删改查操作
- package cn.itcast;
- import java.util.Date;
- import java.util.List;
- import javax.annotation.Resource;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import cn.itcast.core.bean.TestTb;
- import cn.itcast.core.bean.product.Product;
- import cn.itcast.core.bean.product.ProductQuery;
- import cn.itcast.core.dao.TestTbDao;
- import cn.itcast.core.dao.product.ProductDao;
- import cn.itcast.core.service.TestTbService;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:application-context.xml"})
- public class TestProduct {
- @Resource
- private ProductDao productDao;
- @Test
- public void testProduct() throws Exception {
- //Product p = productDao.selectByPrimaryKey(1L);
- //System.out.println(p);
- //查询: 按照条件查询, 支持模糊查询, 分页 排序 指定字段查, 查询总数
- ProductQuery productQuery = new ProductQuery();
- //模糊查询
- //productQuery.createCriteria().andNameLike("%" + "显瘦" + "%");
- //设置条件精准查询
- //productQuery.createCriteria().andNameEqualTo("2016最新款的缔彩枫2015秋冬新款时尚英伦风大衣简约收腰显瘦灰色中长款毛呢外套 灰色 S")
- //.andBrandIdEqualTo(3L);
- //排序 id desc
- productQuery.setOrderByClause("id desc");
- //分页
- productQuery.setPageNo(1);
- productQuery.setPageSize(3);
- //根据指定字段查询
- productQuery.setFields("id, name");
- List<Product> products = productDao.selectByExample(productQuery);
- for (Product product : products) {
- System.out.println(product);
- }
- //查询总条数
- productDao.countByExample(productQuery);
- //保存
- //productDao.insertSelective(product);
- //更新
- //productDao.updateByExampleSelective(record, example);
- //productDao.updateByPrimaryKeySelective(record);
- }
- }
测试类就是如上, 如果对于dao中的方法中的参数不是很详细, 那么就可以直接看dao.xml中的sql语句, 这样就可以一目了然了.
这里只是做个简单的总结, 由于dao和bean全都是自动生成的, 所以里面的代码还有必要再去多看两眼的.
Mybatis逆向工程构建项目实例.的更多相关文章
- mybatis逆向工程(MyBatis Generator)
mybatis逆向工程(MyBatis Generator) 1. 什么是mybatis逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括 :mapper.xml.mappe ...
- 浅析MyBatis(四):全自动写代码的MyBatis逆向工程
在前面几篇文章中,笔者介绍了 MyBatis 的运行流程,在此基础上简单介绍了手写 MyBatis 简易框架与自定义 MyBatis 插件的步骤,相信大家对于 MyBatis 框架的使用流程已经游刃有 ...
- mybatis0212 mybatis逆向工程 (MyBatis Generator)
1mybatis逆向工程 (MyBatis Generator) .1什么是mybatis的逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括生成 :mapper.xml.m ...
- 【JAVA - SSM】之MyBatis逆向工程的使用
MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...
- Mybatis 逆向工程
Mybatis逆向工程: 推荐用Java和XML Configuration的方式生成逆向文件 Java类: package generation; import java.io.File; impo ...
- mybatis逆向工程
一.背景 在实际开发中我们会自己去写mapper映射文件,接口,数据库表对应的实体类,如果需求任务比较少,咱们还可以慢慢的一个一个去写,但是这是不现实的,因为在工作中我们的任务是很多的,这时mybat ...
- JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程
1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...
- mybatis框架(7)---mybatis逆向工程
mybatis逆向工程 逆向工程的目的就是缩减了我们的开发时间.所谓mybatis逆向工程,就是mybatis会根据我们设计好的数据表,自动生成pojo.mapper以及mapper.xml. 接 ...
- 学习笔记01(mybatis逆向工程)
今天来看看一个常用的小功能,就是mybatis的逆向工程.(数据库是mysql) 什么是逆向工程呢?看名字就知道反方向的一个什么工程! 其实啊,如果是平常我们自己学习实践一些小项目的时候,应该是先瞎写 ...
随机推荐
- sql server 分布式查询 和 主从服务器搭建
1. 8K 对应的SQL语句限制 select * from openquery (recei 连接服务器名称 执行的sql 语句放在 SELECT @@SERVERNAME 在本地 ...
- RTABMAP-ROS RGB-D的建图原理
CoreNode.cpp: new CoreWrapper -- CoreWrapper.cpp: process() -- mapsManager_.updateMapCaches MapsMana ...
- 使用doxygen制作C代码文档
使用doxygen制作C代码文档 C 代码注释风格约定 行间注释 /*! * * 这里是注释 * */ 行内注释 <code here> /*! 这里是注释 */ doxygen 风格的宏 ...
- css清除浮动的方法汇总
这是在其他地方看到的一篇文章,汇总的不错,摘过来做个记录. 引用地址 : http://www.cnblogs.com/ForEvErNoME/p/3383539.html ------------- ...
- Web大文件上传控件-jsp-sql示例更新-Xproer.HttpUploader6.2
版权所有 2009-2016荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- Unity IOC容器的简单应用(转)
转自:http://blog.csdn.net/wanzhuan2010/article/details/7763280 Unity是Unity是微软patterns& practices组用 ...
- Java/Android 二进制数据与String互转
将经过加密的二进制数据保存到本地的方法 byte[] src = new byte[] { 122,-69, -17, 92, -76, 52, -21, -87, -10, 105, 76, -75 ...
- 使用TableViewRow完成下拉菜单效果
在TableViewRow标签中增加一个属性opened(自己定义的)=true or false 用于标记当前row的状态 然后用table_view.deleteRow()方法 参数 ...
- linux下subversion server安装手册
linux下subversion server安装手册 安装基于的Linux版本为:Red Hat Enterprise Linux Server release 6.3. 一 准备需要的安装包. ( ...
- [置顶]PADS PCB功能使用技巧系列之NO.001- 如何走蛇形线?
蛇形线是布线过程中常用的一种走线方式,其主要目的是为了调节延时满足系统时序设计要求,但是设计者应该有这样的认识:蛇形线会破坏信号质量,改变传输延时,布线时要尽量避免使用,因此一块PCB上的蛇形线越多并 ...