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,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...
随机推荐
- jupyter中使用graphviz
参考: https://www.cnblogs.com/zhanjiahui/p/11335038.html https://blog.csdn.net/linxid/article/details/ ...
- K8S之WebApi部署
转载声明 本文转自:ASP.NET Core on K8S学习初探(3)部署API到K8S 1.下载镜像 docker pull edisonsaonian/k8s-demo 因为是测试流程,直接把文 ...
- node---处理post请求
//nodejs 处理post请求 // 异步 const http =require('http') const server=http.createServer((req,res)=>{ i ...
- hdu6089 Rikka with Terrorist
题意:n*m的平面内有K个不安全点,Q个询问位置在(x,y)的人能走到多少个点?走到:(x,y)和(x',y')之间的矩形中不包含不安全点. 标程: #include<bits/stdc++.h ...
- Unity 手机屏幕适配
////如有侵权 请联系我进行删除 email:YZFHKM@163.com 1.游戏屏幕适配 屏幕适配是为了让我们的项目能够跑在各种电子设备上(手机,平板,电脑) 那么了解是适配之前首先要了解两个知 ...
- Delphi 实现窗体无标题栏有边框
1.在窗体的public区写下这一句: Procedure CreateParams(var Params :TCreateParams);override;2然后把光标停在这一行上,按下Ctrl+S ...
- Python学习详细教程-武沛齐
目录 Python之路[第一篇]:Python简介和入门 Python之路[第二篇]:Python基础(一) Python之路[第三篇]:Python基础(二) Python之路[第四篇]:模块 Py ...
- CCPC 2019 网络赛 1006 Shuffle Card
// 签到题,比赛时候写双向链表debug了半天,发现有更好方法,记录一下. Shuffle Card HDU 6707 题意: 有一 \(n\) 张卡片,编号 \(1~n\) ,给定初始编号 ...
- 1.2_springboot2.x中redis缓存&原理介绍
1.整合redis作为缓存 说明这里springboot版本2.19 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构 ...
- 4_9.springboot2.x之使用外置servlet容器原理解析
问题概述 嵌入式Servlet容器: 应用打成可执行的jar 优点:简单.便携: **缺点:**默认不支持JSP.优化定制比较复杂(使用定制器[ServerProperties.自定义WebServe ...