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,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...
随机推荐
- mantis 添加新状态配置方法
在mantis的状态栏中一般只有:新建.反馈.认可.已确认.已分派.已解决.已关闭,七个选项,如果想在其中加入新的状态怎么做? 我要加入的状态为:重新打开 1.添加状态信息 打开config_defa ...
- 字体jquery ---
You don’t need icons! Here are 100+ unicode symbols that you can use Danny Markov December 3rd, 2014 ...
- thinkphp 异常处理
和PHP默认的异常处理不同,ThinkPHP抛出的不是单纯的错误信息,而是一个人性化的错误页面,如下图所示: 只有在调试模式下面才能显示具体的错误信息,如果在部署模式下面,你可能看到的是一个简单的提示 ...
- thinkphp 分布式数据库支持
ThinkPHP内置了分布式数据库的支持,包括主从式数据库的读写分离,但是分布式数据库必须是相同的数据库类型. 配置DB_DEPLOY_TYPE 为1 可以采用分布式数据库支持.如果采用分布式数据库, ...
- Delphi定时模拟键盘按键例程
delphi模拟键盘按键实例delphi模拟键盘按键实例,只是模拟一个按键的例子而已.到一定时间按下模拟按下一个按键,delphi7编译通过. 10秒点击一下H键,其他键你们去找数值替换吧,网上大把的 ...
- IENumerable_Test
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- Python collection模块与深浅拷贝
collection模块是对Python的通用内置容器:字典.列表.元组和集合的扩展,它包含一些专业的容器数据类型: Counter(计数器):dict子类,用于计算可哈希性对象的个数. Ordere ...
- C语言进阶学习第二章
本章重点记录指针的各种概念: 1.地址与内容 2.非法的赋值 3.NULL指针:NULL指针作为一个特殊的指针变量,表示不指向任何东西,在对指针进行解引用操作之前,首先必须 确保它并非NULL指针. ...
- PAT甲级——A1108 Finding Average【20】
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数
1.1 python字符串定义 #!/usr/bin/python # -*- coding: utf8 -*- # 定义一个字符串 s1 = 'this is long String that sp ...