1.分析目錄結構“tools”(或者說模板)

2.首先分析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:\sqljdbc.jar" /> <ibatorContext id="context1" targetRuntime="Ibatis2Java5"> <!-- classPathEntry 指定數據庫jdbc驅動jar包的絕對路徑。 --> <!-- id 這個id可以在使用命令行運行Abator時指定,以單獨處理某一個ibatorContext targetRuntime Ibatis2Java5 生成適合JDK5.0的類,另一個選項是 Ibatis2Java2,生成適合Java2的類。 --> <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Example" /> </ibatorPlugin> <!-- ibatorPlugin 繼承自IbatorPluginAdapter,包名必須是 org.apache.ibatis.ibator.plugins,具體實現可以參考官方文檔 必須有替換和被替換字符屬性。 --> <!-- <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://127.0.0.1:5432/hibernateTest" userId="postgres" password="postgres" /> --> <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=PanaDatabase" userId="sa" password="sasa" /> <!-- driverClass 數據庫驅動類 connectionURL 數據庫連接地址 userId 用戶 password 密碼 還可以使用以下格式添加數據庫的其他連接屬性 <property name="" value=""/> --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer true,把JDBC DECIMAL 和 NUMERIC 類型解析為java.math.BigDecimal --> </javaTypeResolver> <javaModelGenerator targetPackage="abator.model" targetProject="comboFramework/tools" /> <!-- targetProject 生成的Java Bean放置在哪個項目的哪個目錄下 targetPackage 生成的Java Bean的包名 一個有用的屬性 <property name="trimStrings" value="true" /> 從數據庫返回的值被清理前后的空格 <property name="enableSubPackages" value="false" /> 是否在包名后加上scheme名稱 --> <sqlMapGenerator targetPackage="abator.sqlmap" targetProject="comboFramework/tools" /> <!-- targetProject 生成的 SqlMap.xml 文件放置在哪個項目的哪個目錄下 targetPackage 生成的 SqlMap.xml 文件的包名 <property name="enableSubPackages" value="false" /> 是否在包名后加上scheme名稱 --> <daoGenerator targetPackage="abator.dao" targetProject="comboFramework/tools" type="GENERIC-CI" /> <table tableName="InfoVoteItm"></table> </ibatorContext> </ibatorConfiguration>
需要注意的幾點是
1.指向的jdbc包是否正確或存在
<classPathEntry location="F:\sqljdbc.jar" /> <ibatorContext id="context1" targetRuntime="Ibatis2Java5">
2.數據庫連接是否配置正確
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=PanaDatabase" userId="sa" password="sasa" />
3.指向的模板工具是否正確
<!-- targetProject 生成的Java Bean放置在哪個項目的哪個目錄下 targetPackage 生成的Java Bean的包名 一個有用的屬性 <property name="trimStrings" value="true" /> 從數據庫返回的值被清理前后的空格 <property name="enableSubPackages" value="false" /> 是否在包名后加上scheme名稱 --> <sqlMapGenerator targetPackage="abator.sqlmap" targetProject="comboFramework/tools" /> <!-- targetProject 生成的 SqlMap.xml 文件放置在哪個項目的哪個目錄下 targetPackage 生成的 SqlMap.xml 文件的包名 <property name="enableSubPackages" value="false" /> 是否在包名后加上scheme名稱 --> <daoGenerator targetPackage="abator.dao" targetProject="comboFramework/tools" type="GENERIC-CI" />
4.指向需要生成的表
<table tableName="InfoVoteItm"></table>
這里是單表(還沒試過多個表一起生成呢,可以試試)
最終生成的結果如圖

可以看出對iBATIS對表InfoVoteItm生成了三個模塊即"sqlmap","model","dao"。
現在我們一個一個的來分析這三個部分。
在分析之前了解下InfoVoteItm表的結構
USE [PanaDatabase] GO /****** 對象: Table [dbo].[InfoVoteItm] 腳本日期: 06/04/2012 16:22:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[InfoVoteItm]( [EmpID] [nvarchar](250) COLLATE Chinese_PRC_CI_AS NULL, [InfoDetail_ID] [int] NULL, [VoteItmDate] [datetime] NULL CONSTRAINT [DF_InfoVoteItm_VoteItmDate] DEFAULT (getdate()) ) ON [PRIMARY]
A.首先我們來分析"sqlmap"這一部分對InfoVoteItm表生成的文件InfoVoteItm_SqlMap.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap namespace="InfoVoteItm" > <resultMap id="ibatorgenerated_BaseResultMap" class="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> <result column="EmpID" property="empid" jdbcType="VARCHAR" /> <result column="InfoDetail_ID" property="infodetailId" jdbcType="INTEGER" /> <result column="VoteItmDate" property="voteitmdate" jdbcType="TIMESTAMP" /> </resultMap> <sql id="ibatorgenerated_Example_Where_Clause" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> <iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" > <isEqual property="oredCriteria[].valid" compareValue="true" > ( <iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" > $oredCriteria[].criteriaWithoutValue[]$ </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" > $oredCriteria[].criteriaWithSingleValue[].condition$ #oredCriteria[].criteriaWithSingleValue[].value# </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" > $oredCriteria[].criteriaWithListValue[].condition$ <iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," > #oredCriteria[].criteriaWithListValue[].values[]# </iterate> </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" > $oredCriteria[].criteriaWithBetweenValue[].condition$ #oredCriteria[].criteriaWithBetweenValue[].values[0]# and #oredCriteria[].criteriaWithBetweenValue[].values[1]# </iterate> ) </isEqual> </iterate> </sql> <select id="ibatorgenerated_selectByExample" resultMap="ibatorgenerated_BaseResultMap" parameterClass="abator.model.InfovoteitmExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> select EmpID, InfoDetail_ID, VoteItmDate from InfoVoteItm <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> <isNotNull property="orderByClause" > order by $orderByClause$ </isNotNull> </isParameterPresent> </select> <delete id="ibatorgenerated_deleteByExample" parameterClass="abator.model.InfovoteitmExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> delete from InfoVoteItm <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </delete> <insert id="ibatorgenerated_insert" parameterClass="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> insert into InfoVoteItm (EmpID, InfoDetail_ID, VoteItmDate) values (#empid:VARCHAR#, #infodetailId:INTEGER#, #voteitmdate:TIMESTAMP#) </insert> <insert id="ibatorgenerated_insertSelective" parameterClass="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> insert into InfoVoteItm <dynamic prepend="(" > <isNotNull prepend="," property="empid" > EmpID </isNotNull> <isNotNull prepend="," property="infodetailId" > InfoDetail_ID </isNotNull> <isNotNull prepend="," property="voteitmdate" > VoteItmDate </isNotNull> ) </dynamic> values <dynamic prepend="(" > <isNotNull prepend="," property="empid" > #empid:VARCHAR# </isNotNull> <isNotNull prepend="," property="infodetailId" > #infodetailId:INTEGER# </isNotNull> <isNotNull prepend="," property="voteitmdate" > #voteitmdate:TIMESTAMP# </isNotNull> ) </dynamic> </insert> <select id="ibatorgenerated_countByExample" parameterClass="abator.model.InfovoteitmExample" resultClass="java.lang.Integer" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> select count(*) from InfoVoteItm <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </select> <update id="ibatorgenerated_updateByExampleSelective" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> update InfoVoteItm <dynamic prepend="set" > <isNotNull prepend="," property="record.empid" > EmpID = #record.empid:VARCHAR# </isNotNull> <isNotNull prepend="," property="record.infodetailId" > InfoDetail_ID = #record.infodetailId:INTEGER# </isNotNull> <isNotNull prepend="," property="record.voteitmdate" > VoteItmDate = #record.voteitmdate:TIMESTAMP# </isNotNull> </dynamic> <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </isParameterPresent> </update> <update id="ibatorgenerated_updateByExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> update InfoVoteItm set EmpID = #record.empid:VARCHAR#, InfoDetail_ID = #record.infodetailId:INTEGER#, VoteItmDate = #record.voteitmdate:TIMESTAMP# <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </isParameterPresent> </update> </sqlMap>
可以看出對表InfoVoteItm做了實體映射(實現增加add,刪除delete,修改update,查詢select)
首先分析下映射實體類
<resultMap id="ibatorgenerated_BaseResultMap" class="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> <result column="EmpID" property="empid" jdbcType="VARCHAR" /> <result column="InfoDetail_ID" property="infodetailId" jdbcType="INTEGER" /> <result column="VoteItmDate" property="voteitmdate" jdbcType="TIMESTAMP" /> </resultMap>
其中"id"起標識的作用,"class"映射由iBATIS生成的實體類(路徑指向,若要移動該文件此處要及得修改)
"column"可以隨意定義但前提是實體類中有相匹配的字段即"property","jdbcType"設置字段屬性(屬性與數據庫字段類型相同)
查詢(可以看出resultMap是對實體類的一種映射,如果查詢select中的字段在實體類中未定義則會出現異常,parameterClass指向的是參數需要注意與class的區別)
<select id="ibatorgenerated_selectByExample" resultMap="ibatorgenerated_BaseResultMap" parameterClass="abator.model.InfovoteitmExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> select EmpID, InfoDetail_ID, VoteItmDate from InfoVoteItm <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> <isNotNull property="orderByClause" > order by $orderByClause$ </isNotNull> </isParameterPresent> </select>
查詢返回結果值
<select id="ibatorgenerated_countByExample" parameterClass="abator.model.InfovoteitmExample" resultClass="java.lang.Integer" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> select count(*) from InfoVoteItm <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </select>
刪除
<delete id="ibatorgenerated_deleteByExample" parameterClass="abator.model.InfovoteitmExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> delete from InfoVoteItm <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </delete>
添加
1.添加所有項
<insert id="ibatorgenerated_insert" parameterClass="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> insert into InfoVoteItm (EmpID, InfoDetail_ID, VoteItmDate) values (#empid:VARCHAR#, #infodetailId:INTEGER#, #voteitmdate:TIMESTAMP#) </insert>
2.選擇性添加
<insert id="ibatorgenerated_insertSelective" parameterClass="abator.model.Infovoteitm" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> insert into InfoVoteItm <dynamic prepend="(" > <isNotNull prepend="," property="empid" > EmpID </isNotNull> <isNotNull prepend="," property="infodetailId" > InfoDetail_ID </isNotNull> <isNotNull prepend="," property="voteitmdate" > VoteItmDate </isNotNull> ) </dynamic> values <dynamic prepend="(" > <isNotNull prepend="," property="empid" > #empid:VARCHAR# </isNotNull> <isNotNull prepend="," property="infodetailId" > #infodetailId:INTEGER# </isNotNull> <isNotNull prepend="," property="voteitmdate" > #voteitmdate:TIMESTAMP# </isNotNull> ) </dynamic> </insert>
變更
1.選擇性變更(條件可選)
<update id="ibatorgenerated_updateByExampleSelective" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> update InfoVoteItm <dynamic prepend="set" > <isNotNull prepend="," property="record.empid" > EmpID = #record.empid:VARCHAR# </isNotNull> <isNotNull prepend="," property="record.infodetailId" > InfoDetail_ID = #record.infodetailId:INTEGER# </isNotNull> <isNotNull prepend="," property="record.voteitmdate" > VoteItmDate = #record.voteitmdate:TIMESTAMP# </isNotNull> </dynamic> <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </isParameterPresent> </update>
2.條件必須
<update id="ibatorgenerated_updateByExample" > <!-- WARNING - This element is automatically generated by Apache iBATIS ibator, do not modify. This element was generated on Wed Mar 09 16:57:25 CST 2011. --> update InfoVoteItm set EmpID = #record.empid:VARCHAR#, InfoDetail_ID = #record.infodetailId:INTEGER#, VoteItmDate = #record.voteitmdate:TIMESTAMP# <isParameterPresent > <include refid="InfoVoteItm.ibatorgenerated_Example_Where_Clause" /> </isParameterPresent> </update>
"model"是對實體類的封裝就不說了,看下"dao"層。
"dao"層生成了兩個文件InfovoteitmDAO是接口類對方法的聲明(包括增,刪,改,查)而InfovoteitmDAOImpl是類是對InfovoteitmDAO接口方法的
實現。
InfovoteitmDAO 接口
package abator.dao; import abator.model.Infovoteitm; import abator.model.InfovoteitmExample; import java.sql.SQLException; import java.util.List; public interface InfovoteitmDAO { /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ int countByExample(InfovoteitmExample example) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ int deleteByExample(InfovoteitmExample example) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ void insert(Infovoteitm record) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ void insertSelective(Infovoteitm record) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ List<Infovoteitm> selectByExample(InfovoteitmExample example) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ int updateByExampleSelective(Infovoteitm record, InfovoteitmExample example) throws SQLException; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ int updateByExample(Infovoteitm record, InfovoteitmExample example) throws SQLException; }
InfovoteitmDAOImpl類
package abator.dao; import abator.model.Infovoteitm; import abator.model.InfovoteitmExample; import com.ibatis.sqlmap.client.SqlMapClient; import java.sql.SQLException; import java.util.List; public class InfovoteitmDAOImpl implements InfovoteitmDAO { /** * This field was generated by Apache iBATIS ibator. * This field corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ private SqlMapClient sqlMapClient; /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public InfovoteitmDAOImpl(SqlMapClient sqlMapClient) { super(); this.sqlMapClient = sqlMapClient; } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public int countByExample(InfovoteitmExample example) throws SQLException { Integer count = (Integer) sqlMapClient.queryForObject("InfoVoteItm.ibatorgenerated_countByExample", example); return count; } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public int deleteByExample(InfovoteitmExample example) throws SQLException { int rows = sqlMapClient.delete("InfoVoteItm.ibatorgenerated_deleteByExample", example); return rows; } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public void insert(Infovoteitm record) throws SQLException { sqlMapClient.insert("InfoVoteItm.ibatorgenerated_insert", record); } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public void insertSelective(Infovoteitm record) throws SQLException { sqlMapClient.insert("InfoVoteItm.ibatorgenerated_insertSelective", record); } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ @SuppressWarnings("unchecked") public List<Infovoteitm> selectByExample(InfovoteitmExample example) throws SQLException { List<Infovoteitm> list = sqlMapClient.queryForList("InfoVoteItm.ibatorgenerated_selectByExample", example); return list; } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public int updateByExampleSelective(Infovoteitm record, InfovoteitmExample example) throws SQLException { UpdateByExampleParms parms = new UpdateByExampleParms(record, example); int rows = sqlMapClient.update("InfoVoteItm.ibatorgenerated_updateByExampleSelective", parms); return rows; } /** * This method was generated by Apache iBATIS ibator. * This method corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ public int updateByExample(Infovoteitm record, InfovoteitmExample example) throws SQLException { UpdateByExampleParms parms = new UpdateByExampleParms(record, example); int rows = sqlMapClient.update("InfoVoteItm.ibatorgenerated_updateByExample", parms); return rows; } /** * This class was generated by Apache iBATIS ibator. * This class corresponds to the database table InfoVoteItm * * @ibatorgenerated Wed Mar 09 16:57:25 CST 2011 */ private static class UpdateByExampleParms extends InfovoteitmExample { private Object record; public UpdateByExampleParms(Object record, InfovoteitmExample example) { super(example); this.record = record; } public Object getRecord() { return record; } } }
