標籤:代碼 ibatis 產生器
Ibatis雖然好用但是有的時候也會因為要自己編寫很多sql語句,想到這一點很多公司都放棄使用Ibatis,綜合種種原因我自己花了點時間寫了一個ibatis的隱射檔案產生器,該產生器使用java開發適合java程式員使用,下面來分享下My Code產生器
1 使用該代碼產生器需要使用的jar包
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/3F/9D/wKiom1PLZPLBHU9NAADl90u8HRg228.jpg" title="QQ20140720144305.png" alt="wKiom1PLZPLBHU9NAADl90u8HRg228.jpg" />
其中標紅的開發包是我自己把自訂的java代碼打包形成的
2 設定資料庫屬性檔案,內容如下
JDBC_DRIVER= com.mysql.jdbc.DriverJDBC_URL= jdbc:mysql://127.0.0.1/exerciseJDBC_USER= rootJDBC_PASSWORD= 123456
3 編寫main方法產生對應檔
package org.lxh;import org.cry.Generator;public class RunToGenenrate { public static void main(String[] args) { try { Generator generator = new Generator(); generator.setTable("m_student"); generator.setPrimaryKeys("id"); generator.setSqlmapFile("org/lxh/config/Student_sqlmap.xml"); generator.setSqlNamespace("Student"); generator.generate(); System.out.println("ibatis對應檔產生成功!"); } catch (Exception e) { e.printStackTrace(); } }}
4 重新整理src目錄,修改產生的xml檔案
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN""http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap namespace="Student"><resultMap id="resultMap" class="null"> <result property="FID" column="id"/> <result property="studentNo" column="studentNo"/> <result property="name" column="name"/></resultMap><sql id="sqlwhere"> <dynamic> <isGreaterThan prepend="and" property="id" compareValue="0"> A.id = #id# </isGreaterThan> <isNotEmpty prepend="and" property="id"> A.id = #id# </isNotEmpty> <isNotEmpty prepend="and" property="studentNo"> A.studentNo like #studentNo# </isNotEmpty> <isNotEmpty prepend="and" property="name"> A.name like #name# </isNotEmpty> <isNotEmpty prepend="and" property="containIds"> A.id NOT IN ($containIds$) </isNotEmpty> </dynamic></sql> <!--getInfoByID--><select id="getInfoByID" resultMap="resultMap" parameterClass="int"> select A.* from m_student A where A.id = #id#</select> <!--read--><select id="read" resultMap="resultMap" parameterClass="null"> select A.* from m_student A where 1=1 </select> <!--getlist--> <!-- 分頁方法需要修改 --><select id="getList" resultMap="resultMap" parameterClass="null"> SELECT TOP $pageSize$ A.* from m_student AS A WHERE (id NOT IN (SELECT TOP $startRow$ id FROM m_student A where 1=1 <include refid="sqlwhere"/> ) <include refid="sqlwhere"/> )</select> <!--getAlllist--><select id="getAllList" resultMap="resultMap" parameterClass="null"> select A.* from m_student A where 1=1 <include refid="sqlwhere"/> Order By A.id Desc</select> <!--getBusinessList--><select id="getBusinessList" resultMap="resultMap" parameterClass="null"> select A.* from m_student A where 1=1 <include refid="sqlwhere"/> Order By A.id Desc</select> <!--getCount--><select id="getCount" resultClass="java.lang.Integer" parameterClass="null"> select count(*) from m_student A where 1=1 <include refid="sqlwhere"/></select> <!--create--><insert id="create" parameterClass="null"> insert into m_student(id,studentNo,name) values (#id#,#studentNo:VARCHAR2#,#name:VARCHAR2#)</insert> <!--update --><update id="update" parameterClass="null"> update m_student set studentNo= #studentNo:VARCHAR2#, name = #name:VARCHAR2# where id = #id#</update> <!--deleteByID--><delete id="deleteByID" parameterClass="int"> delete from m_student where id = #id#</delete> <!--deleteByCondition--><delete id="deleteByCondition" parameterClass="null"> delete from m_student A where 1=1 <include refid="sqlwhere"/></delete></sqlMap>
修改的地方主要有2處,第一處是分頁的sql語句,程式預設產生的是sqlserver的分頁語句,可以根據實際情況修改;第二處要修改的地方是parameterClass,預設為空白可以根據實際情況修改;第三處是resultMap,需要修改的是class和第一個屬性FID,也是根據項目的實際情況來修改
註:該代碼產生器適用於 Oracle(推薦)、SQLServer、MySql
代碼產生器如果需要可以到我的資源裡下載
本文出自 “快樂編程” 部落格,謝絕轉載!
自訂Ibatis代碼產生器