Ibatisの紹介:
Ibatisは、Apacheのオープンソースプロジェクトであり、O/Rマッピングソリューションです。 Ibatisの最大の特徴は、コンパクトですぐに始めることです。複雑な機能が多すぎない場合、Ibatisは要件を満たし、十分に柔軟に対応できる最も簡単なソリューションです。現在、IbatisはMybatisと改名されています。
公式ウェブサイトはhttp://www.mybatis.org/
1.入力パラメーターは単一の値です
<delete id = "com.fashionfree.stat.accesslog.deletememberccesslogsbefore" parameterclass = "long"> accesstimestamp =#value#</delete> <delete id = "com.fashionfree.stat.ccesslog.deletemembermedlog.detemembermedlog =" ">"> memberAccessLog Where AccessTimestamp =#値#</delete>
2。入力パラメーターはオブジェクトです
<ID = "com.fashionfree.stat.accesslog.memberaccesslog.insert" parashefree.stat.accesslog.memberaccesslog> memberacesslog(accesslogid、membersid、clientip、httpmethod、requictid、requartsid、actionid、required、accessid、required、expecturl #AccessLogid#、#memberID#、#clientIP#、#httpmethod#、#actionid#、#requesturl#、#accesstimestamp#、#endoct1#、#endoct2#、#endoct3#)</insert> <insert> " parameterClass = "com.fashionfree.stat.accesslog.model.memberaccesslog> inserting to memberaccesslog(accesslogid、memberId、clientid、httpmethod、httpmethod、requesturl、accesstimestamp、extend1、extend2、extend3、extend3)値#requesturl#、#AccessTimeStamp#、#extend1#、#extend2#、#extend3#)</insert>
3.入力パラメーターはjava.util.hashmapです
<選択id = "com.fashionfree.stat.accesslog.selectactionidandandactionnumber" parameterclass = "hashmap" resultmap = "getactionidandandactionnumber"> select actionid、count(*)as memberccesslog as count from memberadid =##memberid#and AccessTimestamp> id="com.fashionfree.stat.accesslog.selectActionIdAndActionNumber" parameterClass="hashMap" resultMap="getActionIdAndActionNumber"> select actionId, count(*) as count from MemberAccessLog where memberId = #memberId# and accessTimestamp > #start# and accessTimestamp <= #end# group by actionId </select>
4.入力パラメーターには配列が含まれています
<ID = "updateStatusBatch" parameterClass = "hashmap"> uppdate question set status =#status#<dynamic prepend = "> questionid in"> <isnotnull property = "actionids"> <iterate property = "actionids" open = "(" close = ")" condunction = "、" <ID = "updateStatusBatch" parameterClass = "hashmap"> uppdate question set status =#status#<dynamic prepend = "> questionid in"> <isnotnull property = "actionids"> <iterate property = "actionids" open = "(" close = ")" condunction = "、"
説明:ActionIDSは、渡された配列の名前です。ダイナミックタグを使用して、配列が空のときにSQLステートメントの構文エラーを回避します。 Arrayがnullの場合、ibatisの解析エラーを回避するためにisnotnullタグを使用してください
5。パラメーターの渡しには、1つの配列のみが含まれます
<select id = "com.fashionfree.stat.accesslog.model.statmemberact.selectactionidsofmodule" resultclass = "hashmap"> Select moduleid、select moduleid、select statmebraction <dynamic prepend = " </select> <select id = "com.fashionfree.stat.accesslog.model.statmemberact.selectactionidsofmodule" resultClass = "hashmap"> select satemedic <dynamic prepend = "from moduleid in"> <iterate = "" by moduleid </select>
注:Selectタグにはパラメータークラスアイテムがないことに注意してください
また、ここでアレイをハッシュマップに入れることもできますが、オーバーヘッドが追加され、使用することはお勧めしません。
6. Ibatisをパラメーターを文字列に直接解析させます
<select id = "com.fashionfree.stat.accesslog.selectsumdistctinctcountcessmembernum" parametrasclass = "hashmap" resultclass = "int"> select count> select count> select count(select memberid)from AccessTimeStamp> = =###start id = "com.fashionfree.stat.accesslog.selectsumdistctinctconsmembernum" parameterclass = "hashmap" resultclass = "int"> select count = select count(select count "from memberaccesslog where accasestimestamp> = =##start#and AccessTimestam
注:この方法を使用する場合、SQL注入のリスクがあり、使用することはお勧めしません。
7。ページェリー
<select id = "com.fashionfree.stat.accesslog.selectmemberccesslogby" parameterclass = "hashmap" resultmap = "> <memberaccesslogmap"> <incommedaccesslogmap "> <incell cccesslogmap"> "selectalsql"/> <included refid = "wheresql"/> < id = "com.fashionfree.stat.accesslog.selectmemberaccesslogby.count" parameterclass = "hashmap" resultclass = "int"> <includ refid = "countsql"/> <inclid = "wheresql"/>> </select> <sql = " ActionId、requesturl、AccessTimeStamp、Extend1、Extend2、Extend3 MemberAccessLog </sql> <sql id = "wheresql"> accessTimestamp <=#AccessTimestamp#</sql> <sql id = "countsql"> select count(*) Memberaccesslog </sql> <sql id = "wheresql"> accesstimestamp <=#accesstimestamp#</sql> <sql id = "countsql"> select count(*)プロパティ= "Pagesize">制限#startindex#、#pagesize#</isnotnull> </isnotnull> </dynamic> </sql> <select id = "com.fashionfree.stat.accesslog.selectmemberccesslogby" parameterclass = "hashmap" resultmap = ">>>> <includefid = "wheresql"/> <include refid = "pagesql"/> </select> <select id = "com.fashionfree.stat.accesslogby.count" parameterclass = "hashmap" resultclass = "int"> < <SQL ID = "SelectAllSQL"> Select AccessLogid、MemberID、ClientIP、HttpMethod、actionId、requesturl、accessTimestamp、extend1、extend2、extend3 </sql> count(*)from memberaccesslog </sql> <sql id = "pagesql"> <dynamic> <isnotnull property = "startindex"> <isnotnull property = "pagesize"> limit#startindex#、#pagesize#</isnotnull> </isnotnull>
注:この例では、コードは次のとおりです。
Hashmap Hashmap = new Hashmap(); hashmap.put( "AccessTimestamp"、somevalue); PagedQuery( "com.fashionfree.stat.accesslog.selectmemberccesslogby"、hashmap);
PagedQueryメソッドは、最初にcom.fashionfree.stat.accesslog.selectmemberaccesslogby.countという名前のマップされたステートメントを検索します。 (com.fashionfree.stat.accesslog.selectmemberccesslogby)。特定のプロセスについては、UTILSクラスの関連コードを参照してください。
8。SQLステートメントには、サイン>よりも大きく、記号<1よりも少ない。記号より大きいものを書き込み、サインよりも少ない:> <
<delete id = "com.fashionfree.stat.accesslog.deletememberccesslogsbefore" parameterclass = "long"> accesstimestamp <=#value#</delete> xml code <delete id = "com.fashionfree.stat.acessonolog.deletemecescesslogsecesslogslogsecescesslogs of" parameterClass = "long"> memberaccesslogからdelete accasestimestamp <=#value#</delete>
XMLのCDATAエリアに特殊文字を配置します。
<delete id = "com.fashionfree.stat.accesslog.deletememberccesslogsbefore" parameterclass = "long"> <![cdata [accesstimestamp <=#value#]]> </delete> <delete id = " parameterClass = "long"> <![cdata [accesstimestamp <=#value#]]> </delete> where accessTimestampから削除
最初の方法を使用して<and>として記述することをお勧めします(XMLはCDATAのコンテンツを解析しないため、CDATAに動的タグが含まれている場合、機能しません)
9。SQLタグを含めると共通のSQLステートメントを一緒に編成して、簡単に共有してください。
<sql id = "selectbasicsql"> Select selece selece samplingtimestamp、onlinenum、year、month、day、hour from onlinemembernum </sql> <sql id = "wheresqlbe">ここでid = "com.fashionfree.accesslog.selectonlinemembernumsbeforesAmplingtimeStamp" paramEterClass = "hashmap" resultClass = "onlinemembernum"> <inclid = "selectbasicsql" /> <included refid = "wheresqlbe" /> samplingtimestamp、onlinenum、year、month、week、day from onlinemembernum </sql> <sql id = "wheresqlbefore"> where samplingtimestamp <=#smplingtimestamp parameterClass = "hashmap" resultClass = "onlinemembernum"> <include refid = "selectbasicsql" /> <included refid = "wheresqlbefore" /> < /select>
注:SQLタグは参照にのみ使用でき、マッピングされたステートメントと見なすことはできません。上記の例のように、SelectBasicsQLという名前のSQL要素があり、実行するためのSQLステートメントとして使用しようとするのは間違っています。
sqlmapclient.queryforlist( "selectbasicsql"); ×
10。レコードをランダムに選択します
<sql id =” randomsql”> rand()lime#number#</sql>による注文
データベースからランダムに番号レコードを選択します(mysqlのみ)
11。グループ化によるSQLグループのステッチフィールド
<SQL ID =” SelectGroupby> Select A.AnswererCategoryId、A.Answererid、A.Answerername、A.QuestionCategoryId、A.Score、A.AnsweredNum、A.AnswerSeConds、A.CreatedTimestamp、A.LastquestionAptionApthestamp、A.Lastmodifified(Q.Catifified、Q.Catified、 categoryname from sonswerercategory a、questioncategory q where a.questioncategoryid = q. questioncategoryidグループby a.answerercategoryid </sql> <sql id =” selectgroupby> select a.answerercategoryid、a.answererid、a。answerername、a。 A.correctnum、A.AnswerseConds、A.CreatedTimestamp、A.LastquestionApprovedTimestamp、A.LastModified、Group_Concat(Q.CategoryName)AssenserCategory A、Questioncategory Q Where A.QuestionCategoryId = Q.QuestionCategoryidグループA.Answereridの注文Q. A.Answererid by a.answererid ry </sql>
注:MySQLのGroup_Concat関数はSQLで使用されます
12。INの順序で並べ替えます
①mysql:
<SQL ID =” GroupByInarea”> ModuleID、ModuleName、Status、LastModifierID、lastModifiedNameを選択したStatModuleからlastModified from statmoduleは、3,5,1、 '、'、 '+ltrim(moduleid)+')</sql>モジュールネーム、ステータス、lastModifierid、lastModifiedName、lastModified from statmodule in(3、5、1)order by instr( '、3,5,1、'、 '、'+ltrim(moduleid)+'、')</sql> ')</sql>
②sqlServer:
<SQL ID =” GroupByInarea”> Select ModuleID、ModuleName、Status、LastModifierID、LastModifiedName、lastModified from StatModuleからModuleid in(3、5、1)Order( '、'+ltrim(moduleid)+'、'、 '、3,5,5,1、 "sql>" moduleid、modulename、status、lastmodifierid、lastmodifiedname、lastmodified from statmodule in(3、5、1)order by charindex( '、'+ltrim(moduleid)+'、'、 '、'、3,5,1、 ')</sql>
説明:クエリの結果は、リスト(3、5、1)のModuleIDの順序で返されます。
mysql:str(str、substr)
SQLSERVER:Charindex(Subsstr、Str)String str ltrim(str)のサブストリングの最初の発生位置を返す弦ストリングを返し、そのブート(左)スペース文字が削除されます
13.ResultMap resultMapは、SQLクエリの列値をマッピングしてJava Beansの属性値にマッピングします
<resultmap id = "getActionIdandAndactionNumber"> <result column = "actionid" property = "actionid" jdbctype = "bigint" javatype = "long"/> <result column = "count" count "jdbctype =" int "javatype =" int "/> </> xmlコード<列= "ActionId" Property = "ActionId" jdbctype = "bigint" javatype = "long"/> <result column = "count" count "count" jdbctype = "int" javatype = "int"/> </resultmap> </resultmap>
ResultMapの使用は、resultClass(インライン結果マッピング)に対応する明示的な結果マッピングと呼ばれます。 ResultClassを使用することの最大の利点は、シンプルで便利であることです。指定された結果を表示する必要はありません。 Ibatisは、反射に基づいて決定を決定します。 resultMapは、JDBCTYPEとJAVATYPEを指定することにより、より厳格な構成認証を提供できます。
14.TypeAlias
<typealias alias = "memberonlineduration" type = "com.fashionfree.stat.accesslog.model.memberonlineduration" /> <typealias>
エイリアスを定義して、過度に長い名前の繰り返し入力を避けることができます
15.Remap
<選択id = "testforremap" parameterclass = "hashmap" resultClass = "hashmap" remapresults = "true"> select userid <usequal property = "tag" comparyvalue = "1">、username </sequal> <isequalプロパティ= "tag" comparyvalue = "2" 2 " id = "testforremap" parameterclass = "hashmap" resultclass = "hashmap" remapresults = "true"> select userid <isequal property = "tag" comparyvalue = "1">、username> <isequalプロパティ= "tag" comparyvalue = "2"
この例では、パラメータータグの値に従って異なる結果セットが取得されます。 remapresults = "true"属性がない場合、Ibatisは最初のクエリ中に結果セットをキャッシュします。結果セットマッピングは次回は実行されません(プロセス中である必要があります)が、キャッシュ結果セットが使用されます。
したがって、上記の例でremapresultがデフォルトの偽のプロパティであり、プログラムが次のように書かれている場合:
hashmap <string、integer> hashmap = new hashmap <string、integer>(); Hashmap.put( "tag"、1); sqlclient.queryforlist( "testforremap"、hashmap); hashmap.put( "tag"、2); sqlclient.queryforlist( "testforremap"、hashmap);
Javaコード
hashmap <string、integer> hashmap = new hashmap <string、integer>(); Hashmap.put( "tag"、1); sqlclient.queryforlist( "testforremap"、hashmap); hashmap.put( "tag"、2); sqlclient.queryforlist( "testforremap"、hashmap);
プログラムは、最後のクエリクエリを実行するときにエラーを報告します。その理由は、IBATISが最初のクエリの結果セットを使用し、最初と最後の2回の結果セットが異なるためです。 remapresults = "true"プロパティが使用されている場合、ibatisはクエリが実行されるたびに結果セットマッピングを実行し、エラーを回避します(オーバーヘッドがたくさんあります)。
16.ダイナミックタグのプリプエンド属性が、結果コンテンツのプレフィックスとして追加されます。ラベルの結果コンテンツが空の場合、Prepend属性は機能しません。
Prepend属性がダイナミックタグに存在する場合、ネストされた字幕の最初のプレイエンド属性は無視されます。例えば:
<sql id = "wheresql"> <dynamic prepend = "where"> <isnotnull property = "userid" prepend = "bogus"> userid =#userId#</isnotnull> <isnormame "prepend =" and "> username =##username id = "wheresql"> <dynamic prepend = "where"> <isnotnullプロパティ= "userid" prepend = "bogus"> userid =#userid#</isnotnull> <isnotname "prepend =" and "> username =#username#</isnotempty> </dynomic> </sql>
この例では、動的タグには2つのサブラベル<isnotnull>と<isnotempty>が含まれています。上記の原則によれば、false属性prepend = "bogus"が<isnotnull>タグにない場合、動的削除を削除し、<isnotempty>タグは無視され、SQLの構文エラーが発生します。
注:動的タグにPrepend属性がない場合、子タグの最初のプレイエンド属性は自動的に無視されません。
上記は、Ibatisが紹介するために使用される16のSQLステートメントです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!