mybatis註解和xml常用語句

mybatis註解使用

1.簡單crud


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

2.一對一註解

@Select("select * from user")
@Results({
        @Result(id = true,property = "id",column = "id"),//id=true 對應於主鍵
        @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 對應實體類中一對一的實體類名字,uid表示通過uid外鍵查詢User,JavaType表示查詢結果
        //映射成User類型對象,one=表示一對xx fetchType.default默認是立即加載全部查詢,使用lazy懶加載需要才查詢
})
List<User> selectUserList();

3,一對多註解

@Select("select * from user")
@Results({
        @Result(id = true,property = "id",column = "id"),//id=true 對應於主鍵
        @Result(property = "uid",column = "uid"),
        @Result(property = "user",column = "uid",javaType = User.class,
        many = @Many(select = "com.example.dao.UserDao.findUserByid",fetchType = FetchType.DEFAULT))
        //user 對應實體類中一對一的實體類名字,uid表示通過uid外鍵查詢User,JavaType表示查詢結果
        //映射成User類型對象,one=表示一對xx fetchType.default默認是立即加載全部查詢,使用lazy懶加載需要才查詢
})
List<User> selectUserList();

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短語

  <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.需要驗證的插入

 <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.需要驗證的更新

<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>

使case when求和算數sql

SELECT
    sum(
        CASE
        WHEN `REMOVED` = 0 THEN
            1
        ELSE
            0
        END
    )
FROM
    xxx;


mybatis可以使用string給數據庫int類型賦值
springboot中開啓日誌

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

1.ORDER BY ${columnName}

這裏 MyBatis 不會修改或轉義字符串。NOTE 用這種方式接受用戶的輸入,並將其用於語句中的參數是不安全的,會導致潛在的 SQL 注入攻擊,因此要麼不允許用戶輸入這些字段,要麼自行轉義並檢驗。

2.如何使用連接池。

首先實例化連接池數據源對象,讓他實現DataSourceFactory這個接口。然後實現方法。在mybatis。conf文件中設置數據連接池這個類,將數據庫連接信息放在config.properties文件中。

3.mybatis.config文件中setting和數據源的設置參數區別

會被覆蓋。

4.連接參數查詢順序

首先查詢properties文件,然後查詢resource文件,最後查詢方法參數。重複的話會被覆蓋。

5.druid連接池配置方式:

詳見官網
DruidDataSourceFactory首先實行setproperties方法,然後返回設置數據源方法。drui數據源也需要在DataSource中設置properties文件

8.實體類的方法不定義也可以進行映射

9.mybatis默認是事務不提交

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章