您的位置:首页 > 编程语言

iBatis 代码自动生成工具 iBator

2016-12-20 15:09 555 查看
iBator的下载和安装
官方下载地址:http://people.apache.org/builds/ibatis/ibator/

安装:见《Eclipse 插件安装》

安装完成后,“File” —> "New" —> "Other..."



选择项目名  —> "New" —> "Other..." —> “Next” —> 如图



点击“Finish”。就会在IBATORTest/ibatorConfig/目录中生成ibatorConfig.xml文件。



然后再修改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,如图:



将会自动生成映射文件和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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iBator