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("[email protected]");
userDAO.insert(user);
相当于:insert into user(ID,username,password,email) values(101,‘test‘,‘123‘,‘[email protected]‘);

 ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()

User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("[email protected]");
userDAO.updateByPrimaryKey(user);
相当于:update user set username=‘joe‘,password=‘joe‘,email=‘[email protected]‘ 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;
}
时间: 2024-08-26 19:01:14

iBatis 代码自动生成工具 iBator 及 Example 使用的相关文章

STM32代码自动生成工具使用说明

1.什么是"代码自动生成工具" 为了降低开发者的开发门槛,缩短开发周期,降低开发资源投入,机智云推出了代码自动生成服务.云端会根据产品定义的数据点生成对应产品的设备端代码. 自动生成的代码实现了机智云通信协议的解析与封包.传感器数据与通信数据的转换逻辑,并封装成了简单的API,且提供了多种平台的实例代码.当设备收到云端或APP端的数据后,程序会将数据转换成对应的事件并通知到应用层,开发者只需要在对应的事件处理逻辑中添加传感器的控制函数,就可以完成产品的开发. 使用自动生成的代码开发产品

代码自动生成工具,2小时搞定智能硬件产品Demo

常见的智能硬件设备多是由单片机.微处理器.微控制器等构成的嵌入式系统,通过WIFI.蓝牙.GPRS等无线通信技术连接云服务器,传统的开发方式需要开发人员根据自己的产品功能完成MCU 通过无线通信/芯片模组与云服务器交互的协议,而通过MCU代码自动生成工具,云端会根据产品定义的数据点生成对应产品的设备端代码.使用自动生成的代码开发产品,就不必再处理协议相关的部分,开发者可以将节省出来的精力集中在产品的核心功能开发上,不必重复"造轮子". 使用MCU.手机APP代码自动生成工具,2小时搞定

Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展

Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导> 是生存Web Form的. 这次看到网上有生成MVC Saffolding扩展原作者的代码 https://github.com/robinli/MVC5-

mybaits generator 代码自动生成工具使用

mybaits generator 代码自动生成工具使用MyBatis Generator (MBG) 是一个Mybatis的代码生成器,它可以帮助我们根据数据库中表的设计生成对应的实体类,xml Mapper文件,接口以及帮助类(也就是我们可以借助该类来进行简单的CRUD操作),这样就避免了我们每使用到一张表的数据就需要手动去创建对应的类和xml文件,这就帮我们节约了大量的时间去开发和业务逻辑有关的功能,但是如果对联合查询和存储过程您仍然需要手写SQL和对象. 生成方式主要有 Maven 和

mybatis-generator 代码自动生成工具(maven方式)

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的dao.bean.mapper xml文件. 这里主要通过eclipse工具,来讲解实现; 1.建表语句 CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, PRI

MyBatise代码自动生成时候Oralce的number类型BigDecimal问题

第一次使用MyBatise发现一个问题,使用MyBatise的代码自动生成工具时候,即便在配置文件中定义了 Xml代码   <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaTypeResolver> <property name="forceBigDecimals

mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap

由于mybatis简单易学,比起Hibername来,更容易上手,代码也能自动生成.这几天研究了下代码自动生成的,参考: http://0609xiaohua.iteye.com/blog/1453570 但是把代码复制进来,运行了下,却跑不起来,报以下错误: Exception in thread "main" java.lang.ExceptionInInitializerError at com.test.Test.main(Test.java:12)Caused by: org

如何根据动态SQL代码自动生成DTO

当前的状况 一般做数据库相关开发, 除非学习, 否则很少有人愿意直接使用JDBC.本来Java代码就比较啰嗦了,而直接用JDBC写代码之啰嗦简直有些令人发狂!所以在实际开发过程中,我们通常都会使用一些框架/库来帮助我们操作数据库.而且开源市场上的选择也比较多,就我个人接触到的有:Hibernate,MyBatis,JdbcTemplate,DbUtils,ActiveRecord,JavaLite等等. 这些框架都能大幅的提高开发效率,对于一些基本CRUD操作来说,虽然各有差异,但总的来说基本是

mybatis-generator 代码自动生成插件

Hibernate 可以选择MyEclipse Datebase Explorer 或者是 Hibernate-tools 等工具来自动生成映射文件和实体类. mybatis 当然也要有!下面简单介绍一个代码自动生成插件:mybatis-generator. mybatis-generator有三种用法:命令行.eclipse插件.maven插件. 作为一个使用idea开发的程序猿来说势必选择maven插件了.其他两种方式就不再此介绍了,因为我没有用过啊--- 仅仅需要简单的三步就可以啦,就是这