01为什么使用sql语句的动态拼接
目前学习的内容sql语句都是直接写死的,但是在实际的开发过程中很多的sql语句都是根据不同的业务情况进行不同的改变的
02 解决的方案
sql语句的动态拼接
03学习的标签
if where when set trim foreach bind include sql
接口代码1
public interface FlowerMapper {//按照条件查询List<Flower> selectMore(String name,String production);//按照条件查询List<Flower> selectMore2(String name,String production);List<Flower> selectMore3(String name);//按照条件查询List<Flower> selectMore4(String name,String production);//修改操作int update(Flower flower);//修改操作int update2(Flower flower);
}
XML文件1
<mapper namespace="com.bjsxt.mapper.FlowerMapper"><!--if标签if(test){..}if(test){..}--><select id="selectMore" resultType="flower">SELECT * from flower where 1=1<!--OGNL表达式--><if test="param1!=null and param1!=''">and name=#{param1}</if><if test="param2!=null and param2!=''">and production=#{param2}</if></select><!--Where标签的作用:会自动的增加where关键字,并且会把多余的第一个and去掉--><select id="selectMore2" resultType="flower">SELECT * from flower<!--OGNL表达式--><where><if test="param1!=null and param1!=''">name=#{param1}</if><if test="param2!=null and param2!=''">and production=#{param2}</if></where></select><select id="selectMore3" resultType="flower">SELECT * from flower<where><if test="param1!=null and param1!=''">name=#{param1}</if></where></select><!--if(){..}else if(){..}else if(){..}else {}--><select id="selectMore4" resultType="flower">SELECT * from flower<where><choose><when test="param1!=null and param1!=''">name=#{param1}</when><when test="param2!=null and param2!=''">and production=#{param2}</when><otherwise>1=1</otherwise></choose></where></select><!--Set 会自动增加set关键字,并且去除最后一个逗号--><update id="update">UPDATE flower<set><if test="name!=null and name!=''">name=#{name},</if><if test="production!=null and production!=''">production=#{production},</if></set>where id=#{id}</update><!--trim:prefix:添加前缀prefixOverrides:去除前缀suffix:添加后缀suffixOverrides:去除后缀--><update id="update2">UPDATE flower<trim prefix="set" suffixOverrides=","><if test="name!=null and name!=''">name=#{name},</if><if test="production!=null and production!=''">production=#{production}</if></trim>where id=#{id}</update></mapper>
测试代码1
// 动态SQL语句
public static void main(String[] args) throws IOException {//[1]解析myBatis.xml文件InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");//[2]获得sqlsession工厂SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);//[3]获得session对象SqlSession sqlSession = factory.openSession(true);//[4]执行方法FlowerMapper mapper = sqlSession.getMapper(FlowerMapper.class);//List<Flower> list = mapper.selectMore4("玫瑰花","");Flower f=new Flower();f.setId(9);mapper.update(f);//[5]关闭资源sqlSession.close();
}
接口2
public interface FlowerMapper2 {List<Flower> selectMore(List<Integer> li);List<Flower> selectMore2(String name, String production);List<Flower> selectMore3();}
XML2
<mapper namespace="com.bjsxt.mapper.FlowerMapper2"><!--foreach select * from flower where id in (1,2,3)--><select id="selectMore" resultType="flower">select * from flower where id in<foreach collection="list" open="(" separator="," close=")" item="it">#{it}</foreach></select><!--bind 模糊查询--><select id="selectMore2" resultType="flower">select * from flower<where><if test="param1 != null and param1 != ''"><bind name="pa" value=" '%' + param1 + '%' "></bind>name like #{pa} <!--占位(推荐)--></if><if test="param2 != null and param2 != ''">and production like '%${param2}%' <!--拼接--></if></where></select><select id="selectMore3" resultType="flower">select <include refid="sq1"></include> from flower</select><!--定义(公共的)sql代码片段--><sql id="sq1">id,name,price</sql><sql id="sq2">id,name,price,production</sql></mapper>
测试3
FlowerMapper2 mapper = sqlSession.getMapper(FlowerMapper2.class);List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);//List<Flower> li = mapper.selectMore(list);List<Flower> li = mapper.selectMore2("花", "");System.out.println(li);