什么网站出项目找人做,三亚市住房和城乡建设厅网站,广州网站维护,php网站搬家教程Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能#xff0c;它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。 实际开发中可以通过以下几种标签进行动态SQL拼接。
1 if标签
根据test属性所对应的表达式计算结果决定标签中的内容是否拼接到sql中。 …Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。 实际开发中可以通过以下几种标签进行动态SQL拼接。
1 if标签
根据test属性所对应的表达式计算结果决定标签中的内容是否拼接到sql中。
Test
public void dynamicSqlMapperTest(){SqlSession sqlSession SqlSessionUtil.getSqlSession();DynamicSqlMapper dynamicSqlMapper sqlSession.getMapper(DynamicSqlMapper.class);ListEmp empList dynamicSqlMapper.queryEmpList(new Emp(null, 张三, , 男, null));System.out.println(empList);
}ListEmp queryEmpList(Emp emp);select idqueryEmpList resultTypeEmpselect * from t_empwhere 11if testuserName ! null and userName ! and user_name #{userName}/ifif testpassWord ! null and passWord ! and pass_word #{passWord}/ifif testsex ! null and sex ! and sex #{sex}/if
/select2 where标签
当where标签中有内容时会自动生成where关键字并将内容前多余的and或or去掉当where标签中没有内容时不会生成where关键字也即不会拼接条件语句注意where标签不能去掉内容后多余的and或or
ListEmp queryEmpListUsingWhereTag(Emp emp);select idqueryEmpListUsingWhereTag resultTypeEmpselect * from t_empwhereif testuserName ! null and userName ! and user_name #{userName}/ifif testpassWord ! null and passWord ! and pass_word #{passWord}/ifif testsex ! null and sex ! and sex #{sex}/if/where
/select3 trim标签
trim prefixwhere suffix prefixOverrides suffixOverridesand|or /trim若标签中有内容则 prefix|suffix用于设定在trim标签中的内容前面或后面添加指定的内容prefixOverrides|suffixOverrides: 用于设定将trim标签中的内容前面或后面去掉指定的内容若标签中没有内容则trim标签没有任何效果
ListEmp empList dynamicSqlMapper.queryEmpList(new Emp(null, 张三丰, , 男, null));ListEmp queryEmpListUsingTrimTag(Emp emp);select idqueryEmpListUsingTrimTag resultTypeEmpselect * from t_emptrim prefixwhere suffixOverridesand|orif testuserName ! null and userName ! user_name #{userName} and/ifif testpassWord ! null and passWord ! pass_word #{passWord} and/ifif testsex ! null and sex ! sex #{sex} and/if/trim
/select4 choose-when-otherwise标签
相当于if-else if-elsewhen至少有一个otherwise最多有一个成立。
ListEmp empList dynamicSqlMapper.queryEmpListUsingChooseWhenTag(new Emp(null, 张三丰, , 男, null));ListEmp queryEmpListUsingChooseWhenTag(Emp emp);select idqueryEmpListUsingChooseWhenTag resultTypeEmpselect * from t_empwherechoosewhen testuserName ! null and userName ! user_name #{userName}/whenwhen testpassWord ! null and passWord ! pass_word #{passWord}/whenwhen testsex ! null and sex ! sex #{sex}/whenotherwisedept_id 1/otherwise/choose/where
/select5 foreach标签 collection: 设置需要循环的数组或集合item表示数组或集合中的每一个数据separator: 循环体之间的分隔符open: foreach循环的所有内容的开始字符close: foreach循环的所有内容的结束字符批量新增
Emp emp1 new Emp(null, 张三1, 123, 男, null);
Emp emp2 new Emp(null, 张三2, 123, 男, null);
Emp emp3 new Emp(null, 张三3, 123, 男, null);
Emp emp4 new Emp(null, 张三4, 123, 男, null);
Emp emp5 new Emp(null, 张三5, 123, 男, null);
Emp emp6 new Emp(null, 张三6, 123, 男, null);
ListEmp empList Arrays.asList(emp1, emp2, emp3, emp4, emp5, emp6);
int i dynamicSqlMapper.batchInsertEmpUsingForeach(empList);int batchInsertEmpUsingForeach(Param(empList) ListEmp empList);insert idbatchInsertEmpUsingForeachinsert into t_emp valuesforeach collectionempList itememp separator,(null, #{emp.userName}, #{emp.passWord}, #{emp.sex}, null)/foreach
/insert
批量删除
Test
public void batchDeleteEmpUsingForeachTest(){SqlSession sqlSession SqlSessionUtil.getSqlSession();DynamicSqlMapper dynamicSqlMapper sqlSession.getMapper(DynamicSqlMapper.class);int i dynamicSqlMapper.batchDeleteEmpUsingForeach(Arrays.asList(6,7,8));System.out.println(i);
}int batchDeleteEmpUsingForeach(Param(empIdList) ListString empIdList);delete idbatchDeleteEmpUsingForeachdelete from t_emp where id inforeach collectionempIdList itemempId separator, open( close)#{empId}/foreach
/delete6 sql标签
6.1 使用方式一
定义一
sql idcommonColumnIdid, user_name, pass_word, sex, dept_id/sql使用方式一
include refidcommonColumnId/includeTest
public void sqlSegmentTest(){SqlSession sqlSession SqlSessionUtil.getSqlSession();DynamicSqlMapper dynamicSqlMapper sqlSession.getMapper(DynamicSqlMapper.class);ListEmp empList dynamicSqlMapper.queryAllEmpInfo();System.out.println(empList);
}ListEmp queryAllEmpInfo();sql idcommonColumnIdid, user_name, pass_word, sex, dept_id/sql
select idqueryAllEmpInfo resultTypeEmpselectinclude refidcommonColumnId/includefromt_emp
/select6.2 使用方式二
定义二
sql iduserColumns ${alias}.id,${alias}.username,${alias}.password /sql使用方式二
include refiduserColumnsproperty namealias valuet1//include