ダイナミックな開発機関のダオ
動的モデルの開発機関
使用Mapperプロキシインタフェースの開発アプローチ(主流)
インタフェース用には実装クラスがありません
:手順
インターフェースを作成します。1.:インタフェース名およびマッピングファイルの名前がまったく同じでなければなりません!!!
2。インターフェイスと抽象メソッドの名前は正確に同じid属性マッピングファイルの名前でなければなりません!
3。あなたは、動的エージェントの開発名前空間を使用する場合:完全なパスは正確に同じインタフェースを維持する必要があります。
JavaソースファイルのMavenプロジェクトパッケージをしてみましょう。4.次のXMLファイルを読むことができるようにします
<!--让maven工程的java源文件包下面能读取到xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
5.コア設定ファイルにマッピング設定を行います
<!--扫描包: 扫描mapper包下面所有的接口文件-->
<package name="cn.kgc.mapper"></package>
詳細なコア構成
注:タグの設定順序を記述してください!!!
1. 配置文件中的标签和顺序如下:
properties?, 配置属性(学习)
settings?, 全局配置:缓存,延迟加载
typeAliases?, 类型别名(学习)
typeHandlers?, 类型转换(操作)(了解)
objectFactory?, 对象工厂
plugins?, 插件:分页插件
environments?, 环境配置(数据源)
environment (环境子属性对象)
transactionManager (事务管理)
dataSource (数据源)
mappers? 引入映射配置文件(学习)
? : 一个或者零个
| : 任选其一
+ : 最少一个
* : 零个或多个
, : 必须按照此顺序编写
ジャーナル
図1は、MyBatisの独自のログを使用することができる
sqlMapConfig.xmlに配置しました
<!--mybatis自带日志功能-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
2.サードパーティのロギング:log4jの
最初のステップ:座標の導入
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
ステップ2:インポート・ログをファイル
sqlMapConfig.xmlに配置されます。ステップを
<!--日志配置:log4j-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
動的SQLステートメントの
1.問題の
クエリは、動的SQLクエリのエラーかもしれないが使用していないそうだとすれば、任意である
だけで、その上にSQL文に関する:MyBatisのを
ステートメント2.Ifは
長いあなたが条件を満たしている限り、コンテンツ条件、実行することができます
<!--
where标签:
1.当有查询条件的时候,自动添加where条件
2.当没有查询条件的时候,自动去除where条件
-->
<select id="queryActiveByCondition" parameterType="Condition"
resultType="Computers">
select * from computers
<where>
<if test="cBrand!=null and cBrand!='' ">
and brand = #{cBrand}
</if>
<if test="minPrice!=null and minPrice>=0 ">
and price >= #{minPrice}
</if>
<if test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</if>
<if test="cRunMem!=null and cRunMem>0 ">
and runMem = #{cRunMem}
</if>
<if test="cpuM!=null and cpuM!='' ">
and cpu = #{cpuM}
</if>
<if test="cVideoCard!=null and cVideoCard>0 ">
and videoCard = #{cVideoCard}
</if>
<if test="cStatus!=null and cStatus>0 ">
and `status` = #{cStatus};
</if>
</where>
order by id
3.where文の
ラベル
1.条件が自動的に追加されたクエリがあり、
条件はどこクエリは自動的に削除されていない場合2.
そうでないときの文4.choose
アクション場合=決定:switch文のJavaに似ては、条件は、コードのブロックが場合に実行されることが、満たされる、破断端、スイッチうち1つの条件のみを行うだけで、結果を返します。
<!--条件查询 = choose-->
<select id="queryActiveByChoose" parameterType="Condition"
resultType="Computers">
select * from computers
<where>
<choose>
<when test="cBrand!=null and cBrand!=''">
and brand = #{cBrand}
</when>
<when test="minPrice!=null and minPrice>=0">
and price >= #{minPrice}
</when>
<when test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</when>
<otherwise>
</otherwise>
</choose>
</where>
order by id
</select>
5.update声明
<!--动态更新-->
<!--
set标签:如果有条件,就去除最后后面的,号
-->
<update id="updateActiveBySet" parameterType="Computers">
update computers
<set>
<if test="brand!=null and brand!=''">
brand = #{brand},
</if>
<if test="price!=null and price>=0">
price = #{price},
</if>
<if test="runMem!=null and runMem>0">
runMem = #{runMem},
</if>
<if test="cpu!=null and cpu!=''">
cpu = #{cpu}
</if>
</set>
where id = #{id}
</update>
6.foreach声明
<!--
查询编号是1,2的电脑信息
foreach标签:循环遍历作用
collection: 集合:list / 数组: 类型[] = Integer[]
item: 集合/数组的数据值
item="接口传递的参数的名字"
面向接口 = 面向百度
-->
<select id="queryBatch" parameterType="list"
resultType="Computers">
select * from computers where id in
<foreach collection="list" open="(" separator=","
close=")" item="cids">
#{cids}
</foreach>
</select>
使用して、トリムタブ7.mybatis
トリムタグのMyBatisのが一般的に過剰のSQL文とキーワードを、コンマ、またはSQL言語を除去するために使用される
「セット」と「値(」などの接頭辞、またはアドオン、「」ステッチ前の文「)」サフィックスは、選択的に使用することができ
、挿入、更新、または削除情報、動作条件。
プロパティ | 説明 |
接頭辞 | プレフィックスステッチするには、SQL文の |
サフィックス | モザイクのサフィックスSQL文 |
prefixOverriders | SQL文のキーワードや文字の前を削除し、文字持ってprefixOverridesキーワードまたは属性として指定され、そのプロパティを想定して、指定されている「と」で始まるSQL文は「と」、トリムタブが削除されたときに「と」 |
suffixOverrides | suffixOverridesで指定したキーワードや文字属性次のSQL文にキーワードや文字を削除します |
<!--条件查询 = trim-->
<!--
prefix属性:表示拼接SQL语句的前缀
=== 如果有查询条件,prefix会自动的给语句添加where
prefixOverrides属性:去除SQL语句的前缀
-->
<select id="queryActiveByTrim" parameterType="Condition"
resultType="Computers">
select * from computers
<trim prefix="where" prefixOverrides="and">
<if test="cBrand!=null and cBrand!='' ">
and brand = #{cBrand}
</if>
<if test="minPrice!=null and minPrice>=0 ">
and price >= #{minPrice}
</if>
<if test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</if>
<if test="cRunMem!=null and cRunMem>0 ">
and runMem = #{cRunMem}
</if>
<if test="cpuM!=null and cpuM!='' ">
and cpu = #{cpuM}
</if>
<if test="cVideoCard!=null and cVideoCard>0 ">
and videoCard = #{cVideoCard}
</if>
<if test="cStatus!=null and cStatus>0 ">
and `status` = #{cStatus};
</if>
</trim>
order by id
</select>
<!--动态更新 : if+trim-->
<!--
suffixOverrides属性: 表示去除掉后缀
=== 表示如果有后缀的,号 去掉。
-->
<update id="updateActiveByTrim" parameterType="Computers">
update computers
<trim prefix="set" suffixOverrides=",">
<if test="brand!=null and brand!=''">
brand = #{brand},
</if>
<if test="price!=null and price>=0">
price = #{price},
</if>
<if test="runMem!=null and runMem>0">
runMem = #{runMem},
</if>
<if test="cpu!=null and cpu!=''">
cpu = #{cpu}
</if>
</trim>
where id = #{id}
</update>
間違ったが、私を修正してください場合は、あなたに感謝!!