1.需求业务场景933
有些商业场景也需要SQL语句进入⾏动态拼接,如:
1.1批量删除delete from t_car where id in(1,2,3,4,5,6...这⾥值是动态的,根据⽤家庭选择的id不同,值也不同);
select * from t_car where brand like '丰⽥%' and guide_price > 30 and ...;
打包⽅式:jar
引⼊依赖:mysql驱动依赖,mybatis依赖,junit依赖,logback依赖
pojo:com.powernode.mybatis.pojo.Car
mapper连接⼝:com.powernode.mybatis.mapper.CarMapper
引⼊配置⽂件:mybatis-config.xml、jdbc.properties、logback.xml
mapper配置⽂件:com/powernode/mybatis/mapper/CarMapper.xml
编写测试类:com.powernode.mybatis.test.CarMapperTest
拷⻉⼯具类:SqlSessionUtil
2.if标签94需求:多条件查询。
可能的条件包括:品牌(brand)、指导价格(guide_price)、汽⻋类型(car_type)
2.1细节941.test属性在if标签中是必要的。
2.test属性在if标签中的值是false或true。
3.如果test是true,if标签中的sql语句将拼接。相反,它不会拼接。
5.不能在mybatis的动态SQL中使用&&,只能使用and。
2.1.可用于1test属性:94当使用@Param注释时,@Param注释指定的参数将出现在test中@Param("brand"),这里只能使用brand
当不使用@Param注释时,test将出现:param1param2param3arg0arg1arg2...
使用POJO时,POJO类属性名出现在test中。
//多条件查询 94 //可能的条件包括:品牌(brand)、指导价格(guide_price)、汽⻋类型(car_type) @Test public void testSelectByMultiCondition(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 假设三个条件都不是空的 //List<Car> cars = mapper.selectByMultiCondition(比亚迪”, 2.0, “新能源”); // 假设三个条件都是空的 //select * from t_car where 1 = 1 ////在这个sql的where后面加1 = 1.目的是符合sql语法,同时查询全部 /等于selectt * from t_car //List<Car> cars = mapper.selectByMultiCondition("", null, ""); // 假设后两个条件不是空的,第一个条件是空的 //List<Car> cars = mapper.selectByMultiCondition("", 2.0, “新能源”); // 假设第一个条件不空,最后两个条件是空的 List<Car> cars = mapper.selectByMultiCondition(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); }
<select id="selectByMultiCondition" resultType="Car"> select * from t_car where 1 = 1 <!-- 1. test属性在if标签中是必要的。 2. test属性在if标签中的值是false或true。 3. 如果test是true,if标签中的sql语句就会拼接。相反,它不会拼接。 4. 可用于test属性的是: 当使用@Param注释时,@Param注释指定的参数名应出现在test中。@Param("brand"),这里只能使用brand 当@Param注释没有使用时,test中应该出现的是:param1 param2 param3 arg0 arg1 arg2... 使用POJO时,然后POJO类属性名出现在test中。 5. 不能使用mybatis的动态SQL&&,只能使用and。 5. 不能使用mybatis的动态SQL&&,只能使用and。 --> <if test="brand != null and brand != ''"> and brand like "%"#{brand}"%" </if> <if test="guidePrice != null and guidePrice != ''"> and guide_price > #{guidePrice} </if> <if test="carType != null and carType != ''"> and car_type = #{carType} </if> </select>
实施结果:假设第一个条件不是空的,后两个条件是空的
3.where标签95where标签作品⽤:让where⼦句子更动态、更智能。
当所有条件都是空的时候,where标签保证不会⽣成where⼦句。
⾃移动去除某些条件前⾯多余的and或or。
它可以⾃动去掉前⾯多余的and,那就可以了⾃动去掉后⾯多余的and?
<select id="selectByMultiConditionWithWhere" resultType="car"> select * from t_car <where> <if test="brand != null and brand != ''"> brand like #{brand}"%" and </if> <if test="guidePrice != null and guidePrice != ''"> guide_price >= #{guidePrice} and </if> <if test="carType != null and carType != ''"> car_type = #{carType} </if> </where></select>
List<Car> cars = mapper.selectByMultiConditionWithWhere(比亚迪”, null, "");
很显然,后⾯多余的and不会被删除
继续使⽤if标签中的需求。
///使用where标签,使where子句更加智能化。 95 @Test public void testSelectByMultiConditionWithWhere(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 三个条件都不是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere(比亚迪”, 2.0, “新能源”); // 三个条件都是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere("", null, ""); // 假如第一个条件是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere("", 2.0, “新能源”); // 后两个条件是空的 List<Car> cars = mapper.selectByMultiConditionWithWhere(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); }
<select id="selectByMultiConditionWithWhere" resultType="Car"> select * from t_car <!--where标签专门负责where子句的动态生成。--> <where> <if test="brand != null and brand != ''"> and brand like "%"#{brand}"%" </if> <if test="guidePrice != null and guidePrice != ''"> and guide_price > #{guidePrice} </if> <if test="carType != null and carType != ''"> and car_type = #{carType} </if> </where> </select>
4.96trim标签
trim标签属性:
prefix:在trim标签中的句子之前添加内容
suffix:在trim标签中的句子后添加内容
prefixOverrides:前缀覆盖(去除)
suffixOverrides:后缀覆盖(去除)
<select id="selectByMultiConditionWithTrim" resultType="Car"> select * from t_car <!-- prefix:加前缀 suffix:加后缀 prefixOverrides:删除前缀 suffixOverrides:删除后缀 --> <!--prefix="where" 在trim标签的所有内容之前添加 where--> <!--suffixOverrides="and|or" 删除trim标签中的后缀and或or--> <trim prefix="where" suffixOverrides="and|or"> <if test="brand != null and brand != ''"> brand like "%"#{brand}"%" or </if> <if test="guidePrice != null and guidePrice != ''"> guide_price > #{guidePrice} and </if> <if test="carType != null and carType != ''"> car_type = #{carType} </if> </trim> </select>
///使用trim标签 96 @Test public void testSelectByMultiConditionWithTrim(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); List<Car> cars = mapper.selectByMultiConditionWithTrim(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); }
如果所有条件都是空的,where会被添加吗?
List<Car> cars = mapper.selectByMultiConditionWithTrim("", null, "");
执行结果
5.set标签97主要使⽤在update语句中,⽤来⽣成为set关键词,同时去掉最后多余的“,”
⽐如果我们只更新提交的非空字段,如果提交的数据是空的或“的”,那么我们就不会更新这个字段。
5.1update97正常
<update id="updateById"> update t_car set car_num = #{carNum}, brand = #{brand}, guide_price = #{guidePrice}, produce_time = #{produceTime}, car_type = #{carType} where id = #{id} </update>
@Test public void testUpdateById(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car = new Car(40L, null,“丰田霸道”,null,null,"燃油车"; mapper.updateById(car); sqlSession.commit(); sqlSession.close(); }
" >brand="#{brand}," >guide_price="#{guidePrice}," >produce_time="#{produceTime}," >car_type="#{carType}," <="" set>="" where="" id="#{id}" update>
//使用set标签 97 @Test public void testUpdateBySet(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car = new Car(40L, null,"长安",null,null,null); mapper.updateBySet(car); sqlSession.commit(); sqlSession.close(); }
6.choosewherwise标签98
这三个标签是在⼀起使⽤的:
1.1语法格式98
<choose> <when></when> <when></when> <when></when> <otherwise></otherwise></choose>
等同于
if(){ }else if(){ }else if(){ }else if(){ }else{}
只有⼀个分⽀可以选择!!!!
1.2需求:98根据品牌查询,如果不提供品牌,则根据指导价格查询,如果不提供指导价格,则根据⽣产⽇期查询
<select id="selectByChoose" resultType="Car"> select * from t_car <where> <choose> <when test="brand != null and brand != null and brand != ''"> brand like "%"#{brand}"%" </when> <when test="guidePrice != null and guidePrice != ''"> guide_price > #{guidePrice} </when> <otherwise> car_type = #{carType} </otherwise> </choose> </where> </select>
//使用chosechose when otherwise标签。 98 @Test public void testSelectByChoose(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 三个条件都不是空的 //List<Car> cars = mapper.selectByChoose(“丰田霸道”,1.0,“新能源”); // 第一个条件是空的 //List<Car> cars = mapper.selectByChoose(null,1.0,“新能源”); // 前两个条件都是空的 List<Car> cars = mapper.selectByChoose(null,null,“新能源”); // 全部都是空 //List<Car> cars = mapper.selectByChoose(null,null,null); cars.forEach(car -> System.out.println(car)); sqlSession.close(); }
2.992.1foreach标签属性:99
collection:指定数组或集合
item:代表数组或集合中的元素
separator:循环之间的分离符
open:所有sql语句在foreach循环拼接的前面是什么?
close:foreach循所有SQL语句的最后结束是什么?
collection="ids“当我第一次写这篇文章时,我报错了。错误信息是:[array,arg0]
什么意思?
map.put("array",数组);
map.put(arg0,数组);
循环数组或集合,动态⽣成sql,⽐以下SQL:
delete from t_car where id in(1,2,3);delete from t_car where id = 1 or id = 2 or id = 3;
insert into t_car values (null,1001,凯美瑞,35.0,2010-10-11,燃油⻋'), (null,'1002','⽐亚迪唐,31.0,2020-11-11, (null,'1003','⽐32.0,亚迪宋,"新能源"2020-10-11"
2.2批量删除1002.2.1⽤in来删除100
/** * 批量删除。foreach标签 99 * @param ids * @return */ int deleteByIds(@Param("ids") Long[] ids);
@Test public void testDeleteByIds(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Long[] ids = {34L,35L,36L}; int count = mapper.deleteByIds(ids); System.out.println(count); sqlSession.commit(); sqlSession.close(); }
<!-- foreach标签的属性: collection:指定数组或集合 item:代表数组或集合中的元素 separator:循环之间的分离符 open: 所有sql语句在foreach循环拼接的前面是什么? close: 所有sql语句在foreach循环拼接的结尾是什么? close: 所有sql语句在foreach循环拼接的结尾是什么? collection="ids" 第一次写这个的时候报错了,错误的信息是:[array, arg0] 什么意思? map.put("array", 数组); map.put(arg0”, 数组); --> <delete id="deleteByIds"> <!--delete from t_car where id in( <foreach collection="ids" item="aaaaaaa" separator=","> #{aaaaaaa} </foreach> )--> <!--in后面的()不能写,写成这样--> delete from t_car where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete>
2.2.2使用or101
////根据id批量删除 使用or关键字。 101 @Test public void testdeletebyIds2(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Long[] ids = {8L,10L}; int count = mapper.deletebyIds2(ids); System.out.println(count); sqlSession.commit(); sqlSession.close(); }
<delete id=“deletebyIds2”> delete from t_car where <foreach collection="ids" item="id" separator="or"> id=#{id} </foreach> </delete>
2.3批量添加100
//分批插入,一次插入多个Car信息 100 @Test public void testInsertBatch(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car1 = new Car(null,"1200", “帕萨特1”, 30.0, "2020-11-11", "燃油车"; Car car2 = new Car(null,"1201", “帕萨特2”, 30.0, "2020-11-11", "燃油车"; Car car3 = new Car(null,"1202", "帕萨特3", 30.0, "2020-11-11", "燃油车"; List<Car> cars = new ArrayList<>(); cars.add(car1); cars.add(car2); cars.add(car3); mapper.insertBatch(cars); sqlSession.commit(); sqlSession.close(); }
<insert id="insertBatch"> insert into t_car values <foreach collection="cars" item="car" separator=","> (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType}) </foreach> </insert>
3.sql标签和include标签102
代码写在course18上
sql标签⽤声明sql⽚段
include标签⽤sql将来会发表声明⽚段落包含在sql语句中
作⽤:代码复⽤。易维护。
举例
<!--声明SQL片段--> <sql id="carColumnNameSql"> id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType </sql> <select id="selectByBrandLike" resultType="car"> select <include refid="carColumnNameSql"/> from t_car where brand like "%"#{brand}"%" </select> <select id="selectAll" resultType="car"> select <include refid="carColumnNameSql"/> from t_car </select> <select id="selectById" resultType="car"> select <include refid="carColumnNameSql"/> from t_car where id = #{id} </select>
4.代码汇总main中com.powernode.mybatis.mapperCarMapper接口
package com.powernode.mybatis.mapper;import com.powernode.mybatis.pojo.Car;import org.apache.ibatis.annotations.Param;import java.util.List;///动态sql 93public interface CarMapper { /** * 根据id批量删除 使用or关键字。 101 * @param ids * @return */ int deletebyIds2(@Param("ids") Long[] ids); /** * 批量插入,一次插入多个Car信息 100 * @param cars * @return */ int insertBatch(@Param("cars") List<Car> cars); /** * 批量删除。foreach标签 99 * @param ids * @return */ int deleteByIds(@Param("ids") Long[] ids); /** * 使用choose when otherwise标签。 98 * @param brand * @param guidePrice * @param carType * @return */ List<Car> selectByChoose(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType); /** * 使用set标签 97 * @param car * @return */ int updateBySet(Car car); /** * 更新Car 97 * @param car * @return */ int updateById(Car car); /** * 使用trim标签 96 * @param brand * @param guidePrice * @param carType * @return */ List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType); /** * 使用where标签,使where子句更加智能化。 95 * @param brand * @param guidePrice * @param carType * @return */ List<Car> selectByMultiConditionWithWhere(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType); /** * 多条件查询 94 * 可能的条件包括:品牌(brand)、指导价格(guide_price)、汽⻋类型(car_type) * @param brand 品牌 * @param guidePrice 指导价 * @param carType 汽车类型 * @return */ List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);}
test中com.powernode.mybatis.testCarMapperTest
package com.powernode.mybatis.test;import com.powernode.mybatis.mapper.CarMapper;import com.powernode.mybatis.pojo.Car;import com.powernode.mybatis.utils.SqlSessionUtil;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.ArrayList;import java.util.List;///动态sql 93public class CarMapperTest { ////根据id批量删除 使用or关键字。 101 @Test public void testdeletebyIds2(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Long[] ids = {8L,10L}; int count = mapper.deletebyIds2(ids); System.out.println(count); sqlSession.commit(); sqlSession.close(); } //分批插入,一次插入多个Car信息 100 @Test public void testInsertBatch(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car1 = new Car(null,"1200", “帕萨特1”, 30.0, "2020-11-11", "燃油车"; Car car2 = new Car(null,"1201", “帕萨特2”, 30.0, "2020-11-11", "燃油车"; Car car3 = new Car(null,"1202", "帕萨特3", 30.0, "2020-11-11", "燃油车"; List<Car> cars = new ArrayList<>(); cars.add(car1); cars.add(car2); cars.add(car3); mapper.insertBatch(cars); sqlSession.commit(); sqlSession.close(); } ///批量删除 99 @Test public void testDeleteByIds(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Long[] ids = {34L,35L,36L}; int count = mapper.deleteByIds(ids); System.out.println(count); sqlSession.commit(); sqlSession.close(); } //使用chosechose when otherwise标签。 98 @Test public void testSelectByChoose(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 三个条件都不是空的 //List<Car> cars = mapper.selectByChoose(“丰田霸道”,1.0,“新能源”); // 第一个条件是空的 //List<Car> cars = mapper.selectByChoose(null,1.0,“新能源”); // 前两个条件都是空的 List<Car> cars = mapper.selectByChoose(null,null,“新能源”); // 全部都是空 //List<Car> cars = mapper.selectByChoose(null,null,null); cars.forEach(car -> System.out.println(car)); sqlSession.close(); } //使用set标签 97 @Test public void testUpdateBySet(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car = new Car(40L, null,"长安",null,null,null); mapper.updateBySet(car); sqlSession.commit(); sqlSession.close(); } ///更新Car 97 @Test public void testUpdateById(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); Car car = new Car(40L, null,“丰田霸道”,null,null,"燃油车"; mapper.updateById(car); sqlSession.commit(); sqlSession.close(); } ///使用trim标签 96 @Test public void testSelectByMultiConditionWithTrim(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); List<Car> cars = mapper.selectByMultiConditionWithTrim(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); } ///使用where标签,使where子句更加智能化。 95 @Test public void testSelectByMultiConditionWithWhere(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 三个条件都不是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere(比亚迪”, 2.0, “新能源”); // 三个条件都是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere("", null, ""); // 假如第一个条件是空的 //List<Car> cars = mapper.selectByMultiConditionWithWhere("", 2.0, “新能源”); // 后两个条件是空的 List<Car> cars = mapper.selectByMultiConditionWithWhere(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); } //多条件查询 94 //可能的条件包括:品牌(brand)、指导价格(guide_price)、汽⻋类型(car_type) @Test public void testSelectByMultiCondition(){ SqlSession sqlSession = SqlSessionUtil.openSession(); CarMapper mapper = sqlSession.getMapper(CarMapper.class); // 假设三个条件都不是空的 //List<Car> cars = mapper.selectByMultiCondition(比亚迪”, 2.0, “新能源”); // 假设三个条件都是空的 //select * from t_car where 1 = 1 ////在这个sql的where后面加1 = 1.目的是符合sql语法,同时查询全部 /等于selectt * from t_car //List<Car> cars = mapper.selectByMultiCondition("", null, ""); // 假设后两个条件不是空的,第一个条件是空的 //List<Car> cars = mapper.selectByMultiCondition("", 2.0, “新能源”); // 假设第一个条件不空,最后两个条件是空的 List<Car> cars = mapper.selectByMultiCondition(比亚迪”, null, ""); cars.forEach(car -> System.out.println(car)); sqlSession.close(); }}
CarMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.powernode.mybatis.mapper.CarMapper"> <delete id=“deletebyIds2”> delete from t_car where <foreach collection="ids" item="id" separator="or"> id=#{id} </foreach> </delete> <insert id="insertBatch"> insert into t_car values <foreach collection="cars" item="car" separator=","> (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType}) </foreach> </insert> <!-- foreach标签的属性: collection:指定数组或集合 item:代表数组或集合中的元素 separator:循环之间的分离符 open: 所有sql语句在foreach循环拼接的前面是什么? close: 所有sql语句在foreach循环拼接的结尾是什么? collection="ids" 第一次写这个的时候报错了,错误的信息是:[array, arg0] 什么意思? map.put("array", 数组); map.put(arg0”, 数组); --> <delete id="deleteByIds"> <!--delete from t_car where id in( <foreach collection="ids" item="aaaaaaa" separator=","> #{aaaaaaa} </foreach> )--> <!--in后面的()不能写,写成这样--> delete from t_car where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete> <select id="selectByChoose" resultType="Car"> select * from t_car <where> <choose> <when test="brand != null and brand != null and brand != ''"> brand like "%"#{brand}"%" </when> <when test="guidePrice != null and guidePrice != ''"> guide_price > #{guidePrice} </when> <otherwise> car_type = #{carType} </otherwise> </choose> </where> </select> <update id="updateBySet"> update t_car <set> <if test="carNum != null and carNum != null and carNum != ''">car_num = #{carNum},</if> <if test="brand != null and brand != ''">brand = #{brand},</if> <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if> <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if> <if test="carType != null and carType != ''">car_type = #{carType},</if> </set> where id = #{id} </update> <update id="updateById"> update t_car set car_num = #{carNum}, brand = #{brand}, guide_price = #{guidePrice}, produce_time = #{produceTime}, car_type = #{carType} where id = #{id} </update> <select id="selectByMultiConditionWithTrim" resultType="Car"> select * from t_car <!-- prefix:加前缀 suffix:加后缀 prefixOverrides:删除前缀 suffixOverrides:删除后缀 --> <!--prefix="where" 在trim标签的所有内容之前添加 where--> <!--suffixOverrides="and|or" 删除trim标签中的后缀and或or--> <trim prefix="where" suffixOverrides="and|or"> <if test="brand != null and brand != null and brand != ''"> brand like "%"#{brand}"%" or </if> <if test="guidePrice != null and guidePrice != ''"> guide_price > #{guidePrice} and </if> <if test="carType != null and carType != ''"> car_type = #{carType} </if> </trim> </select> <select id="selectByMultiConditionWithWhere" resultType="Car"> select * from t_car <!--where标签专门负责where子句的动态生成。--where标签专门负责where子句的动态生成。--> <where> <if test="brand != null and brand != ''"> and brand like "%"#{brand}"%" </if> <if test="guidePrice != null and guidePrice != ''"> and guide_price > #{guidePrice} </if> <if test="carType != null and carType != ''"> and car_type = #{carType} </if> </where> </select> <select id="selectByMultiCondition" resultType="Car"> select * from t_car where 1 = 1 <!-- 1. test属性在if标签中是必要的。-- 1. test属性在if标签中是必要的。 2. test属性在if标签中的值是false或true。 3. 如果test是true,if标签中的sql语句就会拼接。相反,它不会拼接。 4. 可用于test属性的是: 当使用@Param注释时,@Param注释指定的参数名应出现在test中。@Param("brand"),这里只能使用brand 当@Param注释没有使用时,test中应该出现的是:param1 param2 param3 arg0 arg1 arg2... 使用POJO时,然后POJO类属性名出现在test中。 5. 不能使用mybatis的动态SQL&&,只能使用and。 5. 不能使用mybatis的动态SQL&&,只能使用and。 --> <if test="brand != null and brand != ''"> and brand like "%"#{brand}"%" </if> <if test="guidePrice != null and guidePrice != ''"> and guide_price > #{guidePrice} </if> <if test="carType != null and carType != ''"> and car_type = #{carType} </if> </select></mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="jdbc.properties"/> <!--mybatis的全局设置--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <package name="com.powernode.mybatis.pojo"/> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.powernode.mybatis.mapper"/> </mappers></configuration>
剩余的
pojo
utils
logback.xml
jdbc.properties
pom.xml
不做赘述