一、解决pojo中实体字段和数据表中列名不一致的问题
1)起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样
<select id="selectAll" resultType="pojo.Brand"> select id, brand_name as brandName,company_name companyName,ordered,description,status from tb_brand </select>
2)resultMap:定义<resultMap> 完成不一致的属性名和列名的映射,<select>标签中使用resultMap属性指定<resultMap>标签的id值。
<resultMap id="brandMap" type="pojo.Brand"> <!-- 封装主键的值,如果数据表列名和实体的属性名一样,可以不用手动映射--> <id column="id" property="id"></id> <!--封装的非主键值,如果数据表列名和实体的属性名一样,可以不用手动映射,--> <result column="brand_name" property="brandName"></result> <result column="company_name" property="companyName"></result> </resultMap> <select id="selectAll" resultMap="brandMap"> select * from tb_brand </select>
resultType自动映射,resultMap手动映射,自己定义哪一列的值封装到哪个属性
3)开启驼峰命名自动映射:在mybatis-config.xml核心配置文件中配置使用驼峰命名法【推荐】
接口文件中:
<select id="selectAll" resultType="pojo.Brand"> select * from tb_brand </select>
全局配置文件mybatis-config.xml中:
<!--驼峰命名自动映射--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
二、生成xml查询语句插件
安装插件后
三、参数与占位符 <!--2、根据id查询详情 parameterType="int":参数类型,如果是引用类型,则需要些全类名。 parameterType这个属性可以省略不写。 参数占位符 #{占位符名称}:例如:#{id}。在程序运行的过程中,该占位符会翻译成?,能够解决SQL注入漏洞问题。 如果参数是基本类型,那么占位符的名称可以任意写,一般写参数变量名. ${占位符名称}:例如:${id}。在程序运行的过程中,该占位符不会翻译成?,可能出现SQL注入漏洞问题。 使用场景:#{占位符名称}多用于条件的参数传递。${占位符名称}可能用于动态改变要查询表名或者列名 例如:select * from ${tb_brand} where id=#{id} 特殊字符处理: 小于号 < select * from tb_brand where id < #{id} select * from tb_brand where id <![CDATA[<]]> #{id} --> <select id="selectById" resultType="pojo.Brand"> select * from tb_brand where id=#{id} </select>
四、新增品牌 并获取自动增长的主键id
(1)xml配置文件方式
<!--3、新增品牌 并获取自动增长的主键id useGeneratedKeys="true" :表示需要获取自增的注解值 keyColumn="id" :主键列列名,如果主键列名为id,可以省略不写 keyProperty="id" :将自增的id值赋值给参数对象的哪个属性,此处表示赋值给参数beand对象的id属性 --> <insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id"> insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status}); </insert>
//4 调用方法 Brand brand=new Brand(); //封装要添加的品牌信息 brand.setBrandName("海澜之家3"); brand.setCompanyName("海澜之家3"); brand.setOrdered(1); brand.setDescription("男人的衣柜3"); brand.setStatus(1); int count= brandMapper.add(brand); //5 处理结果 System.out.println("影响的行数:"+count+" 返回的主键id:"+brand.getId());
(2)注解方式
@Insert("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})") @Options(useGeneratedKeys = true,keyProperty = "id") int add(Brand brand);
五、动态sql
1、多参数查询
(1)实体类封装参数【常用 】
* 只需要保证SQL中的参数名 和 实体类属性名对应上,即可设置成功。
List<Brand> selectByCondition(Brand brand);
(2)map集合
* 只需要保证SQL中的参数名 和 map集合的键的名称对应上,即可设置成功。
SQL中的concat函数用来拼接字符串。
List<Brand> selectByCondition(Map map);
(3)散装参数:
* 需要使用@Param(“SQL中的参数名称”),其实就是给xml文件键取名字
List<Brand> selectByCondition(@Param("status")int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
如果参数是多个,mybatis底层会将这些散的参数放到集合中,名字默认是arg0/param1、arg1/param2.... * 弊端:参数的位置固定了,不能更改。
接口中
List<Brand> selectByCondition(int status,String companyName,String brandName);
xml中
或
m
<select id="selectByCondition" resultType="com.itheima.pojo.Brand"> select*from tb_brand where status = #{arg0} and company_name like concat('%',#{arg1},'%') and brand_name like concat('%', #{arg2},'%') </select>
或
<select id="selectByCondition" resultType="com.itheima.pojo.Brand"> select*from tb_brand where status = #{param1} and company_name like concat('%',#{param2},'%') and brand_name like concat('%', #{param3},'%') </select>
解决:mybatis提供了一个@Param注解,专门用来给参数取名字的,将来占位符就是@Param注解的属性值
接口中:
List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName);
配置文件中
<select id="selectByCondition" resultType="com.itheima.pojo.Brand"> select*from tb_brand where status = #{status} and company_name like concat('%',#{companyName},'%') and brand_name like concat('%', #{brandName},'%') </select>
(4)SQL中的concat函数用来拼接字符串
<select id="selectByCondition" resultType="pojo.Brand">
select * from tb_brand where
status = #{status}
and company_name like concat(‘%’,#{companyName},’%’)
and brand_name like concat(‘%’, #{brandName} ,’%’)
</select>
2、单条件查询
•从多个条件中选择一个
choose (when, otherwise):选择,类似于Java 中的 switch 语句
(1)
<select id="selectByConditionSingle" resultType="pojo.Brand"> select * from tb_brand where <choose> <!--类似于switch--> <when test="status != null"> <!--类似于case--> status = #{status} </when> <when test="companyName != null and companyName !=''"> company_name like concat('%',#{companyName},'%') </when> <when test="brandName != null and brandName !='' "> brand_name like concat('%',#{brandName},'%') </when> <otherwise> <!--类似于default--> 1 = 1 </otherwise> </choose> </select>
(2)
<select id="selectByConditionSingle" resultType="com.itheima.pojo.Brand"> select * from tb_brand <where> <choose> <!--类似于switch--> <when test="status != null"> <!--类似于case--> status = #{status} </when> <when test="companyName != null and companyName !=''"> company_name like concat('%',#{companyName},'%') </when> <when test="brandName != null and brandName !='' "> brand_name like concat('%',#{brandName},'%') </when> </choose> </where> </select>
3、修改部分值
之前的修改方式,如果某个字段不传值,那么字段将被设置为null;
<update id="updatePart">
update tb_brand
<set>
<if test="brandName != null and brandName !='' ">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName !=''">
company_name = #{companyName},
</if>
<if test="ordered != null">
ordered = #{ordered},
</if>
<if test="description != null and description !=''">
description = #{description},
</if>
<if test="status != null">
status = #{status},
</if>
</set>
where id = #{id};
</update>
4、批量删除
接口中:
void deleteByIds(@Param("ids") int[] ids);
xml配置文件中:
collection要遍历的数组
item 每次遍历出来的元素
separator分隔符;
open 开始
close 结束;
<delete id="deleteByIds">
delete from tb_brand where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
了解:
对于数组类型默认底层默认存储的是arg0/array,可以用@Param取别名
对于集合类型默认底层默认存储的是arg0/collection/list,可以用@Param取别名