Accout.xml聽 (sqlmap)
<?xml聽version="1.0"聽encoding="UTF-8"聽standalone="no"?>
<!DOCTYPE聽sqlMap聽PUBLIC聽"-//iBATIS.com//DTD聽SQL聽Map聽2.0//En"聽"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap聽namespace="NS_Account">
聽聽聽聽<typeAlias聽alias="Account"聽type="com.ibatis.domain.Accout"/>
聽聽聽聽<resultMap聽id="AccountResult"聽class="Account">
聽聽聽聽聽聽聽聽<result聽property="accountId"聽column="Account_ID"/>
聽聽聽聽聽聽聽聽<result聽property="name"聽column="Name"/>
聽聽聽聽聽聽聽聽<result聽property="password"聽column="Password"/>
聽聽聽聽聽聽聽聽<result聽property="type"聽column="Type"/>
聽聽聽聽</resultMap>
聽聽聽聽<insert聽id="insert"聽parameterClass="Account">
聽聽聽聽聽聽聽聽<![CDATA[
聽聽聽聽聽聽聽聽insert聽into聽T_Admin_Account(
聽聽聽聽聽聽聽聽聽聽聽聽Name,聽Password,聽Type,聽Account_ID
聽聽聽聽聽聽聽聽)聽values聽(
聽聽聽聽聽聽聽聽聽聽聽聽#name#,聽#password#,聽#type#,聽#accountId#
聽聽聽聽聽聽聽聽)
聽聽聽聽聽聽聽聽]]>
聽聽聽聽</insert>
聽聽聽聽<select聽id="findAll"聽resultMap="AccountResult"聽parameterClass="Account">
聽聽聽聽聽聽聽聽<![CDATA[
聽聽聽聽聽聽聽聽select聽*聽from聽T_Admin_Account
聽聽聽聽聽聽聽聽order聽by聽Account_ID
聽聽聽聽聽聽聽聽]]>
聽聽聽聽</select>
聽聽聽聽<delete聽id="delete"聽parameterClass="Account">
聽聽聽聽聽聽聽聽<![CDATA[
聽聽聽聽聽聽聽聽delete聽聽from聽T_Admin_Account
聽聽聽聽聽聽聽聽where聽Account_ID聽=聽#accountId#
聽聽聽聽聽聽聽聽]]>
聽聽聽聽</delete>
聽聽聽聽<update聽id="update"聽parameterClass="Account">
聽聽聽聽聽聽聽聽<![CDATA[
聽聽聽聽聽聽聽聽update聽T_Admin_Account
聽聽聽聽聽聽聽聽set聽Name聽=聽#name#,聽Password聽=聽#password#,聽Type聽=聽#type#
聽聽聽聽聽聽聽聽where聽Account_ID聽=聽#accountId#
聽聽聽聽聽聽聽聽]]>
聽聽聽聽</update>
</sqlMap>
鍙栧緱sqlmapclient:
private聽static聽SqlMapClient sqlmapclient=聽null;
//
Reader聽reader聽=聽null;
try聽{
聽聽reader聽=聽Resources.getResourceAsReader(
聽聽聽聽"com/ibatis/domain/sql/sql-map-config.xml");
聽 sqlmapclient=聽SqlMapClientBuilder.buildSqlMapClient(reader);
}聽catch聽(Exception聽e)聽{
聽聽e.printStackTrace();
}
鍦ㄨ緗ソ榪欎簺涓滆タ鍚庯紝浣跨敤綾諱技涓婇潰鐨勪唬鐮佸彇寰梥qlmapclient涔嬪悗灝卞彲浠ヤ嬌鐢╯qlmapclient榪涜鏁版嵁搴撴搷浣滀簡銆?br />搴旇娉ㄦ剰鍒板洜涓洪厤緗枃浠朵腑浣跨敤浜?/span>namespace錛堝湪<settings涓緗簡 useStatementNamespaces="true"/>錛夛紝鎵浠ュ湪sqlmapclient榪涜鎿嶄綔鏃惰璁頒綇浣跨敤namespace銆傚錛?br />sqlmapclient.insert("NS_Account.insert",new聽Acount());
榪欐槸涓轟簡閬垮厤sqlmap閰嶇疆鏂囦歡涓嚭鐜扮浉鍚屽悕瀛楃殑鏂規硶鏃朵駭鐢熷啿紿併?br />
濡傛灉浣跨敤spring榪涜浠g爜鐨勭粍緇囷紝閭d箞浜嬫儏灝嗗彉寰楁洿鍔犵畝鍗曪紝浣犲彧瑕佸湪spring鐨勯厤緗枃浠墮噷闈嬌鐢?br />聽聽聽聽<bean聽id="sqlMapClient"聽class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
聽聽聽聽聽聽聽聽<property聽name="configLocation">
聽聽聽聽聽聽聽聽聽聽聽聽<value>com/ibatis/domain/sql/sql-map-config.xml</value>
聽聽聽聽聽聽聽聽</property>
聽聽聽聽聽聽聽聽<property聽name="dataSource">
聽聽聽聽聽聽聽聽聽聽聽聽<ref聽bean="dataSource"/>
聽聽聽聽聽聽聽聽</property>
聽聽聽聽</bean>
瀵?/span>sqlMapClient榪涜娉ㄥ唽錛岀劧鍚庢敞鍏ュ埌鐩稿簲鐨刣ao閲岄潰灝卞彲浠ヤ簡錛宻pring榪樻彁渚涗簡綆渚跨殑SqlMapClientDaoSupport錛岃繖涔熶細瀵逛綘鐨勭▼搴忓緢鏈夊府鍔┿?br />
濡傛灉浣犲彧鏄兂浣跨敤ibatis鑷繁鎻愪緵鐨刣ao妗嗘灦涔熷彲浠ャ備負浜嗕嬌鐢╠ao妗嗘灦錛岃繕闇瑕佸彟澶栫殑閰嶇疆鏂囦歡錛?br />dao.xml:
<?xml聽version="1.0"聽encoding="UTF-8"?>
<!DOCTYPE聽daoConfig聽PUBLIC聽"-//iBATIS.com//DTD聽DAO聽Configuration聽2.0//EN"聽"http://www.ibatis.com/dtd/dao-2.dtd">
<daoConfig>
聽聽聽聽<!--聽Example聽SQL聽Maps聽DAO聽Configuration聽-->
聽聽聽聽<context>
聽聽聽聽聽聽聽聽<transactionManager聽type="SQLMAP">
聽聽聽聽聽聽聽聽聽聽聽聽<property聽name="SqlMapConfigResource"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽value="sql-map-config.xml"聽/>
聽聽聽聽聽聽聽聽</transactionManager>
聽聽聽聽聽聽聽聽<dao聽interface="com.ln.dao.CategoryDao"
聽聽聽聽聽聽聽聽聽聽聽聽implementation="com.ln.daoImpl.ibatis.CategoryDaoImplSqlMap"聽/>
聽聽聽聽聽聽聽聽聽聽聽聽<dao聽interface="com.ln.dao.CalDao"
聽聽聽聽聽聽聽聽聽聽聽聽implementation="com.ln.daoImpl.ibatis.CalDaoImplSqlMap"聽/>
聽聽聽聽</context>
</daoConfig>
鑰屼笖鍦ㄤ綘鐨刣ao interface涓緇ф壙com.ibatis.dao.client.Dao鎺ュ彛錛堣繖鍙槸涓涓爣蹇楁у熷彛錛屾病鏈変換浣曟柟娉曪級
鍦ㄧ▼搴忎腑灝卞彲浠ヤ嬌鐢細
聽聽聽聽聽聽聽聽Reader聽reader聽=聽null;
聽聽聽聽聽聽聽聽DaoManager聽daoManager=null;
聽聽聽聽聽聽聽聽try聽{
聽聽聽聽聽聽聽聽聽聽reader聽=聽Resources.getResourceAsReader(
聽聽聽聽聽聽聽聽聽聽聽聽"com/ibatis/domain/sql/dao.xml");
聽聽聽聽聽聽聽聽聽聽daoManager聽=聽DaoManagerBuilder.buildDaoManager(reader);
聽聽聽聽聽聽聽聽}聽catch聽(Exception聽e)聽{
聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽daoManager.getDao(CalDao.class);
榪欐牱鐨勭▼搴忔潵鍙栧緱dao錛岃繖涔熻兘寰堝ソ鐨勬妸dao鎻愪緵鐨勬柟娉曚笌鍏蜂綋鐨勫疄鐜板垎紱誨紑鏉ャ?br />鍦ㄤ嬌鐢╥batis鐨刣ao妗嗘灦鏃訛紝鏈濂藉湪dao瀹炵幇綾諱腑緇ф壙ibatis鎻愪緵鐨凷qlMapDaoTemplate ,瀹冩槸dao妗嗘灦鎻愪緵鐨勪竴涓ā鐗堢被錛岀敤鏉ョ鐞哾ao mapper妗嗘灦鐨勫悇涓柟闈紝涔熷彲浠ョ畝鍖栦綘鐨勫伐浣?/span>
public聽classCalDaoImpl extends SqlMapDaoTemplate implementsCalDao{
聽聽publicCalDaoImpl (DaoManager聽daoManager)聽{
聽聽聽聽super(daoManager);
聽聽}
}
緇ф壙涔嬪悗鍙敤鍦ㄥ疄鐜頒竴涓甫DaoManager 鍙傛暟鐨勬瀯閫犲嚱鏁板氨鍙互浜嗐傝繖涓瀯閫犲嚱鏁板茍涓嶉渶瑕佹墜鍔ㄨ皟鐢紝浣跨敤daoManager.getDao(CalDao.class);鏉ュ彇寰梔ao瀹炰緥鐨勬椂鍊欙紝妗嗘灦浼氳嚜鍔ㄨ繘琛岃皟鐢ㄣ?/span>
涔嬪悗鍦ㄤ綘鐨刣aoImpl閲岄潰灝卞彲浠ョ洿鎺ヤ嬌鐢╭ueryForList絳塻qlmapclient鎻愪緵鐨勬柟娉曚簡銆?br />
榪欐牱ibatis鐨勪粙緇嶅熀鏈畬鎴愩?br />
閭d負浠涔堣浣跨敤Ibatis鍛紵鍘熷洜寰堢畝鍗曪紝瀹冨緢濂藉湴灞忚斀鎺変簡Connection,statement,resultset絳夌洿鎺ヤ嬌鐢╦dbc鏄緢鐑︿漢鐨勫湴鏂癸紝鍙堝彲浠ョ洿鎺ヤ嬌鐢╯ql璇彞闈炲父鐏墊椿鍦拌繘琛屾暟鎹簱鎿嶄綔錛堢壒鍒槸閰嶇疆鏂囦歡涓殑鍔ㄦ乻ql璇彞鏇存槸瓚沖鐨勭伒媧伙級錛岃屼笖榪樻彁渚涗簡榪炴帴姹狅紝cache錛屼簨鍔$鐞嗙瓑鏀寔銆備嬌鐢ㄨ搗鏉ョ畝鍗曡屽張寮哄ぇ