2020-04-20 Mybatis相關(guān)知識

Mybatis相關(guān)知識

Mybatis之多參傳遞

注解方式傳遞參數(shù)

1.在mappper.xml中編寫相關(guān)sql語句

<select id="findUserByNameAndSex" resultType="User">
    select * from user where username = #{name} , sex = #{sex}
</select>

2.在接口中用注解傳遞參數(shù)

//注解方式傳多個參數(shù),xml里不用寫parameterType
    public Users findUserByNameAndSex(@Param("name")String name,@Param("sex")String sex);   

3.也可以使用注解方式編寫sql語句

@Select("select * from user where username = #{name} , sex = #{sex}")
    public Users findUserByNameAndSex2(@Param("name")String name,@Param("sex")String sex);

包裝類傳遞參數(shù)

將所需參數(shù)封裝到一個包裝類里
特別地:如果需要將一個類作為參數(shù)封裝到一個包裝類里,調(diào)這個類里的屬性需要用 "."

<!-- 包裝類中參數(shù)是一個封裝類,采用. 調(diào)用封裝類里的屬性   名字要一樣 -->
    <select id="findUserByUserVo" parameterType="usersQueryVo" resultType="users">
        select * from users where username like "%"#{users.username}"%"
    </select>

Map方式

一般用于只傳遞兩個參數(shù)進入查詢語句,將這兩個參數(shù)封裝到Map中。
在mapper中,Map默認(rèn)別名為map

<!-- Map默認(rèn)別名 map -->
<select id="" parameterType="map"></select>

別名

別名:實體類中的名字與數(shù)據(jù)庫中的不一致。使用resultMap高級映射
resultMap用于高級映射 實體類與表不一致,自定義結(jié)果映射集 type:指向映射的對象類
<id/>:匹配主鍵 column:表里的列名
property:實體類的屬性名

在xml文件里設(shè)置別名

<resultMap type="Orders" id="orders">
    <id column="id" property="id"/>
    <result column="user_id" property="userId"/>
</result>
//column 是數(shù)據(jù)庫中的名字   property 是類中的屬性
<select id="selectOrderList" resultMap=" orders">
    select id,user_id,number FROM orders
</select>   
//resultType改為resultMap  resultMap中的內(nèi)容與id一致

sql語句拼接

利用動態(tài)sql實現(xiàn)根據(jù)用戶傳入?yún)?shù)不同,自動完成查詢條件的變化

查詢中常用的sql語句拼接

where: 相當(dāng)于sql中的where 不同是where標(biāo)簽不會編輯到sql中 where會去前and
if:判斷 test:判斷條件
Integer默認(rèn)值是"" , 但int默認(rèn)值為0 建議使用integer

實例:

<!-- 根據(jù)性別和名字查詢用戶 where 可以在sex為null或空值時 去掉username前的 and 但不能去掉后 and -->
<select id="selectUserBySexAndUsername" parameterType="User" resultType="User">
    select * from user
    <where>
        <if text="sex != null and sex != ' '">
            sex = #{sex}
         </if>
         <if test="username !=null and username != ' '">
             and username = #{username}
          </if>
    </where>
 </select>  

增加中常用的sql語句

<insert id="insertUserByExample" parameterType="users">
        insert into users 
        <trim prefix="(" suffix=")"  suffixOverrides=",">
            <if test="id != null and '' != id">
                id,
            </if>
            <if test="username != null and '' != username">
                username,
            </if>
        </trim>
        <trim   prefix="values(" suffix=")" suffixOverrides=",">
            <if test="id != null and '' != id">
                #{id},
            </if>
            <if test="username != null and '' != username">
                #{username},
            </if>
        </trim>
    </insert>

sql語句封裝

定義sql片段

可以將常用的sql語句單獨定義出來,需要的時候調(diào)用

<sql id="findsql">
     select * form users
</sql>
<!-- include:應(yīng)用已定義的sql語句 -->
<select id="findUser" resultType="users">
    <include refid="findsql" ></include>
</select>

封裝到類

將sql語句封裝到類里,在接口中調(diào)用封裝類

@SelectProvider(type = UserSqlProvider.class,method="queryUserById")
    public Users findUserById(Integer id);

封裝類

    public String queryUserById(int id) {
        return "select * from user where id = #{id}";
    }
    //注意and 前面的空格
    public String queryUserByExample(Users user) {
        String sql = "select * from user where 0=0";
        if(null != user.getId()) {
            sql+=" and where id = #{id}";
        }
        if(user.getName() != null) {
            sql+=" and name  = #{name}";
        }
        return sql;
    }
    

結(jié)構(gòu)化sql語句

//結(jié)構(gòu)化sql語句
    public String queryUserByName() {
        //String sql = "select * from user where name like '%'#{name}'%'";
        SQL sql = new SQL();
        String str=sql.SELECT("*").FROM("user").WHERE("name like concat('%',#{name},'%')").toString();
        return str;
    }

sql語句中的遍歷

在sql語句中傳入一個集合,然后遍歷集合中的數(shù)據(jù),用于執(zhí)行相關(guān)操作
foreach:遍歷 如果是集合,直接傳遞過來List Map 和Array 有默認(rèn)別名 首字母小寫
item:遍歷時的元素
open:開始遍歷時添加
close:遍歷結(jié)束時添加
separator:不同元素之間的分隔符
如果是在傳進來的封裝類中有一個list屬性,collection里直接寫數(shù)組的屬性名

<select id="findUserBatchById" resultType="user" parameterType="int">
        select * from user where id IN
            <foreach collection="list" item="i" index="" open="(" close=")" separator=",">
                #{i}
            </foreach>
    </select>

注解式遍歷

@Select({"<script>",
            "select * from user",
            "<where>" , 
            "<if test='id!=null '>id=#{id}</if>", 
            "</where>",
            "</script>"})
    public List<Users> findUserByExample(Users user);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容