iBatis 代码自动生成工具 iBator 及 Example 使用
iBator的下载和安装 官方下载地址:http://people.apache.org/builds/ibatis/ibator/ 安装:见《Eclipse 插件安装》
安装完成后,“File” —> "New" —> "Other..."
iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间 选择项目名 —> "New" —> "Other..." —> “Next” —> 如图 iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间
点击“Finish”。就会在IBATORTest/ibatorConfig/目录中生成ibatorConfig.xml文件。 iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间
然后再修改ibatorConfig.xml文件,修改后的文件如下 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"
"http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" >
<ibatorConfiguration >
<classPathEntry location="F:\javaEE\IBATORTest\lib\sqlserver.jar" /> /*SQL Server 数据库驱动路径*/
<ibatorContext id="context1" >
<jdbcConnection driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver"
connectionURL="jdbc:Microsoft:sqlserver://localhost:1433;DatabaseName=demo" userId="sa" password="joe" />
<javaModelGenerator targetPackage="com.demo.ibatis.beans" targetProject="IBATORTest" />
<sqlMapGenerator targetPackage="com.demo.ibatis.beans.mapFiles" targetProject="IBATORTest" />
<daoGenerator targetPackage="com.demo.ibatis.dao" targetProject="IBATORTest" type="GENERIC-CI" />
<table schema="dbo" tableName="user" catalog="demo" domainObjectName="User">
<generatedKey column="ID" sqlStatement="SQLSERVER" identity="true" type="post" />
</table>
</ibatorContext>
</ibatorConfiguration> 鼠标右键ibatorConfig.xml,如图: iBatis 代码自动生成工具 iBator - 低调的华丽 - 辉色空间
将会自动生成映射文件和Domain层,还同时生成DAO层,用户只需编写Service层即可。 iBator 数据库操作 通过iBator导出的文件包括映射文件、Domain类、DAO类。它导出的Domain类和DAO类是依据iBator设计的框架生成的,其中包括了各种函数。我们要基于这些类来开发Service层代码。 新生成的DAO层的接口提供了以下操作函数: int countByExample(UserExample example) thorws SQLException:按条件计数。
int deleteByPrimaryKey(Integer id) thorws SQLException:按主键删除。
int deleteByExample(UserExample example) thorws SQLException:按条件删除。
String/Integer insert(User record) thorws SQLException:插入 (返回值为id值)
User selectByPrimaryKey(Integer id) thorws SQLException:按主键查询。
List<?>selectByExample(UserExample example) thorws SQLException:按条件查询
List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException:按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
int updateByPrimaryKey(User record) thorws SQLException:按主键更新
int updateByPrimaryKeySelective(User record) thorws SQLException:按主键更新值不为null的字段
int updateByExample(User record, UserExample example) thorws SQLException:按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException:按条件更新值不为null的字段 详解: UserDAOImpl userDAO = new UserDAOImpl(SqlMapClientFactory.getSqlMapClient());
注:SqlMapClientFactory.getSqlMapClient():是自定义的类和方法,目的是获取SqlMapClient. ① selectByPrimaryKey() User user = userDAO.selectByPrimaryKey(100); 相当于select * from user where id = 100 ② selectByExample() 和 selectByExampleWithBLOGs() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = userDAO.selectByExample(example);
相当于:select * from user where username = 'joe' and username is null order by username asc,email desc 注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。 ③ insert() User user = new User();
user.setId(101);
user.setUsername("test");
user.setPassword("123")
user.setEmail("joe@163.com");
userDAO.insert(user);
相当于:insert into user(ID,username,password,email) values(101,'test','123','joe@163.com'); ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective() User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("joe@163.com");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='joe',password='joe',email='joe@163.com' where id=101 User user = new User();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
相当于:update user set password='joe' where id=101 ⑤ updateByExample() 和 updateByExampleSelective() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
User user = new User();
user.setPassword("123");
userDAO.updateByPrimaryKeySelective(user,example);
相当于:update user set password='123' where username='joe' ⑥ deleteByPrimaryKey() userDAO.deleteByPrimaryKey(101); 相当于:delete from user where id=101 ⑦ deleteByExample() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相当于:delete from user where username='joe' ⑧ countByExample() UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
int count = userDAO.countByExample(example);
相当于:select count(*) from user where username='joe' 扩展DAO类实现更复杂的SQL iBator插件只是给我们产生了一个满足基本功能的代码框架,比如:增、删、改、查、条件、排序的使用,这些都是iBator工具导出的函数实现的。但iBator不能给我们提供所有的函数,但由于iBatis框架是基于原生SQL的。因此,我们只需要在iBator代码插件产生的代码基础上进行扩展即可。扩招的方法当然是基于iBatis的映射文件,只需要添加更多的<statement>、<select>等SQL声明元素即可。 例: <select id="getMaxUserid" resultClass="java.lang.Integer">
<![CDATA[select max(id) from user]]>
</select>
<select id="getUsernameList" resultClass="java.lang.String">
<![CDATA[select distinct username from user]]>
</select> 然后在UserDAOImpl.java中实现如下的两个函数:
public Integer getMaxUserid() throws SQLException{
return (Integer)sqlMapClient.queryForObject("users.getMaxUserid");
}
public List<?>getUsernameList() throws SQLException{
List<?>list = sqlMapClient.queryForList(“users.getUsernameList”);
return list;
}
iBatis 代码自动生成工具 iBator 及 Example 使用的更多相关文章
- Ibatis代码自动生成工具——Abator安装与应用实例(图解)
Abator 能自动生成DAO,DTO和sqlMap,大大提高开发效率.Abator 的官方网站:http://ibatis.apache.org/ibator.html 使用也比较简单,以下做个实例 ...
- 代码自动生成工具MyGeneration之一(程序员必备工具)
代码自动生成工具MyGeneration之一(程序员必备工具) 转 分类: C#2008-08-06 18:12 16064人阅读 评论(12) 收藏 举报 工具数据库相关数据库stringbrows ...
- Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展
Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffoldi ...
- 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(v6.0.0版)
TableGo v6.0.0 版震撼发布,此次版本更新如下: 1.UI界面大改版,组件大调整,提升界面功能的可扩展性. 2.新增BeautyEye主题,界面更加清新美观,也可以通过配置切换到原生Jav ...
- C# 代码自动生成工具
开源:C# 代码自动生成工具,支持站点前后台 前言 写这个项目有很长一段时间了,期间也修修改改,写到最后,自己也没咋用(研究方向变化了). 正文 具体项目开源了:https://github.co ...
- 代码自动生成工具_java版
项目结构: 这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后, 我想用代码自动生成我想要的代码,最后生成的效果: 也就是说为每一个bean都生成相应的Dao, ...
- mybatis-generator 代码自动生成工具
今天来介绍下怎么用mybatis-gennerator插件自动生成mybatis所需要的dao.bean.mapper xml文件,这样我们可以节省一部分精力,把精力放在业务逻辑上. 之前看过很多文章 ...
- mybatis-generator 代码自动生成工具(maven方式)
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的 ...
- Py福利,基于uiautomatorviewer 的Python 自动化代码自动生成工具分享(jar已发布GitHub,欢迎Star)
前言做UI自动化无论你用SDK自带的uiautomatorviewer还是Macaca还是Appium自动的inspector,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...
随机推荐
- vue cli3使用webpack4打包优化
去掉console.log,以及开启gzip const CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件 ...
- a^a^a^a^a^a^a^a^a
a^a^a^a是从前向后算,也就是a^(a^3)
- day21 生成器,列表解析,三元表达式
Python之路,Day9 = Python基础9 判断可迭代对象和迭代器 from collections import Iterable, Iterator # 导入模块功能,用来判断对象是否为I ...
- Git中.gitignore忽略规则
# 此为注释 – 将被 Git 忽略 *.a # 忽略所有 .a 结尾的文件 !lib.a # 但 lib.a 除外 /TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TO ...
- 云-腾讯云-实时音视频:实时音视频(TRTC)
ylbtech-云-腾讯云-实时音视频:实时音视频(TRTC) 支持跨终端.全平台之间互通,从零开始快速搭建实时音视频通信平台 1.返回顶部 1. 腾讯实时音视频(Tencent Real-Time ...
- quartz的job中注入spring对象!
一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: public ...
- System Verilog的概念以及与verilog的对比
以下内容源自:http://blog.csdn.net/gtatcs/article/details/8970489 SystemVerilog语言简介 SystemVerilog是一种硬件描述和验证 ...
- DXP 笔记
1. 从原理图上添加 net class,快捷键 : P -> V -> C
- Spring+SpringMVC+Mybatis搭建的网站的处理流程总结
最近学习了如何使用SSM框架搭建网站,以前没用过框架,第一次使用,总结一下自己对框架处理流程的理解
- Python接口测试框架实战与自动化进阶✍✍✍
Python接口测试框架实战与自动化进阶 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看 ...