2017 06 14
本次笔记主要记录 mapper 映射文件
讨论mybatis中where条件查询的一些应用
mapper映射文件代码示例:
<?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">
<!-- 类似于包名,但是不允许其重复 命名控件:package 规则: 1.使用类的全路径 com.shxt.servlet.UserServlet -->
<mapper namespace="com.shxt.model.User">
<!-- 结果集处理标签,只能是针对于查询 -->
<resultMap type="User" id="BaseResultMapper">
<id column="user_id" jdbcType="INTEGER" property="userId" javaType="java.lang.Integer" />
<result column="account" jdbcType="VARCHAR" property="account" javaType="java.lang.String" />
<result column="password" jdbcType="VARCHAR" property="pwd" javaType="java.lang.String" />
<result column="user_name" jdbcType="VARCHAR" property="userName" javaType="java.lang.String" />
</resultMap>
<sql id="sys_user_columns">
user_id,account,password,user_name
</sql>
<!-- 01.Like -->
<select id="like01" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
user_name like '%管%'
</select>
<!-- 02.Like Java代码解决方案 -->
<select id="like02" parameterType="string" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
user_name like #{name}
</select>
<!-- 03.Like 数据库SQL语句解决方案 -->
<select id="like03" parameterType="string" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
user_name like CONCAT('%',#{name},'%')
</select>
<!-- 04.使用${}完成查询,但是面试题问MyBatis${}和#{}之区别 -->
<!-- ${} 必须有KEY或属性 -->
<!-- ${} 原封不动,不进行转义 #{}根据你的数据会自动进行转义 -->
<!-- ORDER BY user_name|account|id -->
<!-- 别忽略${} 如果以后匿名涉及特别赋值的业务逻辑的SQL语句的时候,会使用${} -->
<select id="like04" parameterType="string" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
user_name like CONCAT('%','${shxt}','%')
</select>
<!-- 05.请查询数据USER_ID小于 10 的所有数据 -->
<!-- < 小于号在xml文件里是不被允许的符号 需要方法来对它进行转化 -->
<!-- <![CDATA[ 这里的内容教会转化为纯文本内容 ]]> -->
<!-- 方法一 : 利用提供的特殊字符来进行小于号的表示 -->
<select id="less01" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
<!-- XML不允许使用5个特殊字符 -->
user_id <= 10
</select>
<!-- 方法二 : 利用<![CDATA[?]]> 特殊标志来进行转化 -->
<select id="less02" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE
<!-- XML不允许使用5个特殊字符 -->
<![CDATA[
user_id <= 10
]]>
</select>
<!-- &表示 & 特殊字符转化 -->
<select id="if01" parameterType="map" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE 1=1
<!-- test中写的类中的属性或者Map中的KEY -->
<if test="user_name !=null && user_name != ''">
AND user_name like CONCAT('%',#{user_name},'%')
</if>
<if test="account != null and account != ''">
AND account like CONCAT('%',#{account},'%')
</if>
</select>
<select id="if02" parameterType="map" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
WHERE 1=1
<!-- test中写的类中的属性或者Map中的KEY -->
<if test="user_name !=null && user_name != ''">
AND user_name like CONCAT('%',#{user_name},'%')
</if>
<if test="account != null and account != ''">
AND account like CONCAT('%',#{account},'%')
</if>
</select>
<select id="if03" parameterType="map" resultMap="BaseResultMapper">
SELECT
<include refid="sys_user_columns" />
FROM
sys_user
<!-- test中写的类中的属性或者Map中的KEY -->
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="user_name !=null && user_name != ''">
AND user_name like CONCAT('%',#{user_name},'%')
</if>
<if test="account != null and account != ''">
AND account like CONCAT('%',#{account},'%')
</if>
</trim>
</select>
<update id="update01" parameterType="User">
UPDATE
user
<set>
<if test="account !=null and account !='' ">
account = #{account},
</if>
<if test="userName !=null and userName !='' ">
user_name = #{userName},
</if>
<if test="pwd !=null and pwd !='' ">
password = #{pwd},
</if>
</set>
WHERE
user_Id = #{userId}
</update>
<update id="update02" parameterType="User">
UPDATE
user
<set>
<if test="account !=null and account !='' ">
account = #{account},
</if>
<if test="userName !=null and userName !='' ">
user_name = #{userName},
</if>
<if test="pwd !=null and pwd !='' ">
password = #{pwd},
</if>
</set>
WHERE
user_Id = #{userId}
</update>
<delete id="delete01">
DELETE FROM user WHERE user_Id in
<foreach collection="array" item="v" open="(" close=")" separator=",">
#{v}
</foreach>
</delete>
</mapper>
新建UserDao文件 作为接口定义实现方法
新建UserDaoImpl文件 作为UserDao文件的接口实现类
在test文件中接口回调创建对象 调用接口实现类(UserDaoImpl)实现的方法
对应的test文件内容 :
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.shxt.dao.UserDao;
import com.shxt.dao.impl.UserDaoImpl;
import com.shxt.model.User;
public class UserDaoTest {
@Test
public void testLike01Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.like01();
System.out.println(userList);
}
@Test
public void testLike02Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.like02("管");
System.out.println(userList);
}
@Test
public void testLike03Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.like03("管");
System.out.println(userList);
}
@Test
public void testLike04Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("shxt", "管");
List<User> userList = userDao.like04(tempMap);
System.out.println(userList);
}
@Test
public void testLess01Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.less01();
System.out.println(userList);
}
@Test
public void testLess02Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
List<User> userList = userDao.less02();
System.out.println(userList);
}
@Test
public void testIf01Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("user_name", "管");
//tempMap.put("account", "s");
List<User> userList = userDao.if01(tempMap);
System.out.println(userList);
}
@Test
public void testIf03Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("user_name", "管");
//tempMap.put("account", "s");
List<User> userList = userDao.if03(tempMap);
System.out.println(userList);
}
@Test
public void testUpdate01Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
User user = new User();
user.setAccount("test");
user.setPwd("12312313");
user.setUserName("test");
user.setUserId(1);
userDao.Update01(user);
}
@Test
public void testUpdate02Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
User user = new User();
user.setAccount("test");
user.setPwd("12312313");
user.setUserName("test");
user.setUserId(1);
userDao.Update01(user);
}
@Test
public void testDelete01Method(){
//接口回调
UserDao userDao = new UserDaoImpl();
int[]arr = {1,3};
userDao.batchDel(arr);;
}
}