iBator的下載和安裝
官方下載地址:http://people.apache.org/builds/ibatis/ibator/
選擇項目名 —> "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("[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;
}