zoukankan      html  css  js  c++  java
  • 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:javaEEIBATORTestlibsqlserver.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;
    }
  • 相关阅读:
    ffmpeg显示视频
    眼见为实(1):C++基本概念在编译器中的实现
    在Windows系统上实现轻量级的线程间及进程间消息队列
    Intellij IDEA 2017 debug断点调试技巧与总结详解篇
    redis 全局命令 查看所有的键,删除键,检查键是否存在,获取过期时间,键的数据结构类型
    java.security.InvalidKeyException: IOException : Short read of DER length
    RSA解密报错java.security.spec.InvalidKeySpecException的解决办法
    IntelliJ IDEA全局内容搜索和替换
    RSA加密/解密 Decryption error异常解决
    java rsa 解密报:javax.crypto.BadPaddingException: Decryption error
  • 原文地址:https://www.cnblogs.com/mengyuxin/p/5661143.html
Copyright © 2011-2022 走看看