Mybatis-动态sql

"本文详细介绍了MyBatis在实体类命名、解决列名不一致、参数占位符使用、SQL特殊字段处理及动态SQL方面的配置和应用。通过在mybatis-config.xml中配置实体类包名,可以实现自动映射。对于列名与属性名不一致的情况,可通过sql片段起别名或使用resultMap解决。在SQL语句中,推荐使用#{}
摘要由CSDN通过智能技术生成

目录

实体类别名

解决表的列名和实体类属性名不一致

参数占位符

SQL语句中特殊字段处理

 动态SQL


实体类别名

在核心配置文件mybatis-config.xml中配置

"com.ucloud.brandcrud01.pojo":为实体类包名

  1. <typeAliases>

  2. <package name="com.ucloud.brandcrud01.pojo"/>

  3. </typeAliases>

  1. <select id="selectAll" resultType="dept">

  2. </select>

解决表的列名和实体类属性名不一致

第一种:sql片段起别名

  1. <sql id="brand_column"> id, brand_name as brandName,

  2. company_name as companyName, ordered, description, status</sql>

  3. <select id="selectAll" resultType="Brand">

  4. select <include refid="brand_column"></include> from tb_brand

  5. </select>

第二种:使用resultMap,column:表字段名,property:属性名

  1. <resultMap id="brandResultMap" type="brand">

  2. <result column="brand_name" property="brandName"></result>

  3. <result column="company_name" property="companyName"></result>

  4. </resultMap>

  5. <select id="selectAll" resultType="brand" resultMap="brandResultMap">

  6. select * from tb_brand;

  7. </select>

实体类属性名和数据库表列名不一致时,不能自动封装数

参数占位符

推荐 :#{} :执行SQL时,会将 #{} 占位符替换为  ?,将来自动设置参数值。底层使用的是 PreparedStatement。 

${} :拼接SQL。底层使用的是 Statement ,会存在SQL注入问题。

SQL语句中特殊字段处理

 可以看出报错了,因为映射配置文件是xml类型的问题,而 > < 等这些字符在xml中有特 殊含义,所以此时我们需要将这些符号进行转义。

&lt; 就是 < 的转义字符。

 动态SQL

多条件查询

  1. <select id="selectByCondition" resultMap="brandResultMap">

  2. select * from tb_brand

  3. <where>

  4. <if test="companyName!=null and companyName!=''">

  5. and company_name like #{companyName}

  6. </if>

  7. <if test="brandName!=null and brandName!=''">

  8. and brand_name like #{brandName}

  9. </if>

  10. </where>

  11. </select>

单个条件查询

choose 标签类 似于Java 中的switch语句。

  1. <select id="selectByConditionSingle" resultMap="brandResultMap">

  2. select * from tb_brand

  3. <where>

  4. <choose>

  5. <when test="companyName!=null and companyName!=''">

  6. company_name like #{companyName}

  7. </when>

  8. <when test="brandName!=null and brandName!=''">

  9. brand_name like #{brandName}

  10. </when>

  11. </choose>

  12. </where>

  13. </select>

修改

  1. <update id="update">

  2. update tb_brand

  3. <set>

  4. <if test="brandName != null and brandName != ''">

  5. brand_name = #{brandName},

  6. </if>

  7. <if test="companyName != null and companyName != ''">

  8. company_name = #{companyName},

  9. </if>

  10. <if test="ordered != null">

  11. ordered = #{ordered},

  12. </if>

  13. <if test="description != null and description != ''">

  14. description = #{description},

  15. </if>

  16. <if test="status != null">

  17. status = #{status}

  18. </if>

  19. </set>

  20. where id = #{id};

  21. </update>

批量删除

  1. <delete id="deleteByIds">

  2. delete from tb_brand where id in

  3. <foreach collection="array" item="id" separator=","

  4. open="(" close=")">

  5. #{id}

  6. </foreach>

  7. </delete>

foreach 标签 用来迭代任何可迭代的对象(如数组,集合)。

collection 属性:  mybatis会将数组参数,封装为一个Map集合。

默认:array = 数组 使用@Param注解改变map集合的默认key的名称

item 属性:本次迭代获取到的元素。

separator 属性:集合项迭代之间的分隔符。 foreach 标签不会错误地添加多余的 分隔符。也就是最后一次迭代不会加分隔符。

 open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次 

close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值