最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis注解與xml常用語(yǔ)句匯總

 更新時(shí)間:2018年09月10日 11:18:03   作者:wotrd  
最近一直在用mybatis,由于需要使用到了動(dòng)態(tài)sql,遇到了一些問(wèn)題,現(xiàn)在來(lái)總結(jié)一下,經(jīng)驗(yàn)教訓(xùn)。下面這篇文章主要給大家總結(jié)介紹了mybatis注解與xml常用語(yǔ)句的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

MyBatis是一個(gè)支持普通SQL查詢(xún),存儲(chǔ)過(guò)程和高級(jí)映射的優(yōu)秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及對(duì)結(jié)果集的檢索封裝。MyBatis可以使用簡(jiǎn)單的XML或注解用于配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對(duì)象)映射成數(shù)據(jù)庫(kù)中的記錄。

本文將給大家詳細(xì)介紹關(guān)于mybatis注解與xml常用語(yǔ)句的相關(guān)內(nèi)容,下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

mybatis注解使用

1.簡(jiǎn)單crud

public interface UserMapper {
 //查詢(xún)
 @Select("select * from user where id=#{id}")
 User selectUser(int id);
 //查詢(xún)?nèi)?
 @Select("select * from user")
 List<User> selectUserList();
 //增加數(shù)據(jù)
 @Insert("insert into user (name) values(#{name})")
 boolean insertUser(String name);
 //修改用戶(hù)
 @Update("update user set name=#{name} where id=#{id}")
 boolean updateUser(@Param("name") String name,@Param("id") int id);
 //刪除用戶(hù)
 @Delete("delete from user where id=#{id}")
 boolean deleteUser(int id);
}

2.一對(duì)一注解

@Select("select * from user")
@Results({
 @Result(id = true,property = "id",column = "id"),//id=true 對(duì)應(yīng)于主鍵
 @Result(property = "uid",column = "uid"),
 @Result(property = "user",column = "uid",javaType = User.class,
 one = @One(select = "com.example.dao.UserDao.findUserByid",fetchType = FetchType.DEFAULT))
 //user 對(duì)應(yīng)實(shí)體類(lèi)中一對(duì)一的實(shí)體類(lèi)名字,uid表示通過(guò)uid外鍵查詢(xún)User,JavaType表示查詢(xún)結(jié)果
 //映射成User類(lèi)型對(duì)象,one=表示一對(duì)xx fetchType.default默認(rèn)是立即加載全部查詢(xún),使用lazy懶加載需要才查詢(xún)
})
List<User> selectUserList();

3,一對(duì)多注解

mybatis的xml配置

1.配置resultMap

<resultMap id="BaseResultMap" type="xx" >
 <id column="id" property="ID" jdbcType="BIGINT" />
 <result column="aa" property="aa" jdbcType="VARCHAR" />
 <result column="bb" property="bb" jdbcType="INTEGER" />
 <result column="cc" property="cc" jdbcType="DECIMAL" javaType="java.math.BigDecimal" />
 <result column="dd" property="dd" jdbcType="DATE" />
</resultMap>

2.通用sql短語(yǔ)

 <sql id="Base_Column_List" >
 aa, bb
 </sql>

 <sql id="where">
 <trim prefix="WHERE" prefixOverrides="AND|OR">
 <if test="id != null and id != ''">
  AND t.ID = #{id}
 </if>
 <if test="content != null and content != ''">
  AND t.CONTENT LIKE concat('%', #{content},'%')
 </if>
 AND t.APP_CODE IN
 <foreach item="item" index="index" collection="appcodes"
  open="(" separator="," close=")">
  #{item}
 </foreach>
 and t.USER_ID=u.id and t.REMOVED=0
 </trim>
</sql>

3.需要驗(yàn)證的插入

 <insert id="insert" parameterType="xxx"
 useGeneratedKeys="true" keyColumn="id" keyProperty="id">
 insert into xxx (
 <trim suffixOverrides=",">
  <if test="title != null and title != '' ">
   TITLE ,
  </if>
 </trim>
 ) VALUES (
 <trim suffixOverrides=",">
  <if test="title != null and title != '' ">
   #{title} ,
  </if>
 </trim>
 )
</insert>

4.需要驗(yàn)證的更新

<update id="update" parameterType="xxx">
 UPDATE xxx
 <set>
  <if test="title != null and title != '' ">
   TITLE = #{title} ,
  </if>
 </set>
 WHERE
 ID = #{id}
</update>

5.<!--批量更新ticketid和SeatNo-->

<update id="xxxUpdate" parameterType="java.util.List">
 update xxx
 <trim prefix="set" suffixOverrides=",">
  <trim prefix="AA =case" suffix="end,">
   <foreach collection="orders" item="item" index="index">
    <if test="item.aa !=null">
     when ID=#{item.id} then #{item.aa}
    </if>
   </foreach>
  </trim>
  <trim prefix="BB =case" suffix="end,">
   <foreach collection="orders" item="item" index="index">
    <if test="item.bb !=null">
     when ID=#{item.id} then #{item.bb}
    </if>
   </foreach>
  </trim>
 </trim>
 where ID in
 <foreach collection="orders" index="index" item="item" separator="," open="(" close=")">
  #{item.id,jdbcType=BIGINT}
 </foreach>
</update>

mybatis可以使用string給數(shù)據(jù)庫(kù)int類(lèi)型賦值

springboot中開(kāi)啟日志

#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

1.ORDER BY ${columnName}

這里 MyBatis 不會(huì)修改或轉(zhuǎn)義字符串。NOTE 用這種方式接受用戶(hù)的輸入,并將其用于語(yǔ)句中的參數(shù)是不安全的,會(huì)導(dǎo)致潛在的 SQL 注入攻擊,因此要么不允許用戶(hù)輸入這些字段,要么自行轉(zhuǎn)義并檢驗(yàn)。

2.如何使用連接池。

首先實(shí)例化連接池?cái)?shù)據(jù)源對(duì)象,讓他實(shí)現(xiàn)DataSourceFactory這個(gè)接口。然后實(shí)現(xiàn)方法。在mybatis。conf文件中設(shè)置數(shù)據(jù)連接池這個(gè)類(lèi),將數(shù)據(jù)庫(kù)連接信息放在config.properties文件中。

3.mybatis.config文件中setting和數(shù)據(jù)源的設(shè)置參數(shù)區(qū)別

會(huì)被覆蓋。

4.連接參數(shù)查詢(xún)順序

首先查詢(xún)properties文件,然后查詢(xún)r(jià)esource文件,最后查詢(xún)方法參數(shù)。重復(fù)的話(huà)會(huì)被覆蓋。

5.druid連接池配置方式:

詳見(jiàn)官網(wǎng)

DruidDataSourceFactory首先實(shí)行setproperties方法,然后返回設(shè)置數(shù)據(jù)源方法。drui數(shù)據(jù)源也需要在DataSource中設(shè)置properties文件

6.實(shí)體類(lèi)的方法不定義也可以進(jìn)行映射

7.mybatis默認(rèn)是事務(wù)不提交

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

新源县| 安龙县| 临海市| 莲花县| 叶城县| 城固县| 水富县| 贵阳市| 蒲江县| 中牟县| 巴彦县| 灵寿县| 临夏市| 蕲春县| 紫金县| 宿松县| 松阳县| 乐东| 贵定县| 永福县| 九江县| 沁水县| 长沙市| 池州市| 宾川县| 宁武县| 无棣县| 呼伦贝尔市| 南木林县| 马鞍山市| 赤水市| 盘山县| 佛坪县| 沙雅县| 高阳县| 临沭县| 嘉禾县| 罗江县| 碌曲县| 泾川县| 吉安市|