自訂Ibatis代碼產生器

來源:互聯網
上載者:User

標籤:代碼   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代碼產生器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.