1、mybatis在xml文件中处理大于号小于号的方法 第一种方法:
用了转义字符把>和<替换掉,然后就没有问题了。
SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DATE
附:XML转义字符
dao调用
int insertRoleNewList(List list);
注释:list为向xml文件中传递的参数
4、迭代写法
xml文件写法
<select id="findUserIdRole" resultMap="roleResultMap" parameterType="java.util.Map">
select * from t_role
<trim prefix="where" prefixOverrides="and" >
<if test="userRoleIdList!=null">
and id in
<foreach item="item" index="index" collection="userRoleIdList" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</trim>
</select>
注释:collection="userRoleIdList"中userRoleIdList为集合
调用类写法
package com.gamexun.support.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gamexun.support.model.Role;
import com.gamexun.support.service.RoleService;
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("gx-support-common-context-test.xml");
RoleService roleService = (RoleService)context.getBean("roleService");
try {
Map<String,Object> params = new HashMap<String,Object>();
List userRoleIdList = new ArrayList();
userRoleIdList.add(1);
userRoleIdList.add(2);
userRoleIdList.add(3);
params.put("userRoleIdList",userRoleIdList);
List<Role> list = roleService.findUserIdRole(params);
System.out.println("---------------------------------------------");
System.out.println(list.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、在mybatis中使用模糊查询
<!-- 查询用户 -->
<select id="getUser" resultMap="usertMap" parameterType="java.util.Map">
select * from user
<where>
1=1
<if test="account!=null"><![CDATA[ and account like '${account}%' ]]></if>
<if test="name!=null"><![CDATA[ and name like '${name}%' ]]></if>
<if test="tel!=null"><![CDATA[ and phone like '${tel}%' ]]></if>
<if test="beginNum!=null and count!=null">
order by id limit #{beginNum},#{count}
</if>
</where>
</select>
说明:like后面必须用$;