复合条件查询(动态SQL)
MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
if
choose (when, otherwise)
trim (where, set)
foreach
创建PO类 作为导航
package com.foreknow.bean;
public class UserInfo extends User {
}
UserQueryInfo.java
package com.foreknow.bean;
import java.util.List;
/**
* 根据多个条件查询用户的信息
* @author Administrator
*
*/
public class UserQueryInfo {
//传入多个id
private List<Integer> ids;
//在这里包装所需要的查询条件
//用户查询条件
private UserInfo userInfo;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public static void main(String[] args) {
}
}
UserMapper.java 接口
// 用户信息综合查询
public List<UserInfo> findUserList(UserQueryInfo userQueryInfo) throws Exception;
// 用户信息综合查询总数
public int findUserCount(UserQueryInfo userQueryInfo) throws Exception;
UserMapper.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">
<!--namespace命名空间 ,它的作用是对SQL进行分类化管理 -->
<mapper namespace="com.foreknow.dao.UserMapper">
<!--
Java代码:
public User findUserById(int id)
id:唯一标识 相当于方法的名称
parameterType:输入参数的类型 相当于方法的参数类型
resultType:方法返回值的类型 注意:全路径(包名+类名)
#{
id}:相当于一个占位符
-->
<!-- 所有的select标签都能引用他 -->
<sql id="query_user_where">
<if test="userInfo!=null">
<if test="userInfo.sex!=null and userInfo.sex!=''">
and USER .sex = #{
userInfo.sex}
</if>
<if test="userInfo.username!=null and userInfo.username!=''">
and USER.username LIKE '%${userInfo.username}%'
</if>
<if test="ids!=null">
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
<!-- 每个遍历需要拼接的串 -->
id=#{
user_id}
</foreach>
</if>
</if>
</sql>
<select id="findUserById" parameterType="int" resultType="com.foreknow.bean.User" >
select * from user where id=#{
id}
</select>
<!-- public void insertUser(User user) -->
<insert id="insertUser" parameterType="com.foreknow.bean.User">
insert into User(username,birthday,sex,address) values(#{
username},#{
birthday},#{
sex},#{
address})
</insert>
<!--
${
value}表示拼接sql字符串
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="com.foreknow.bean.User">
select * from user where username like '%${value}%'
</select>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{
id}
</delete>
<update id="updateUser" parameterType="com.foreknow.bean.User">
update user set username=#{
username},birthday=#{
birthday},sex=#{
sex},address=#{
address} where id=#{
id}
</update>
<select id="findUserList" parameterType="com.foreknow.bean.UserQueryInfo" resultType="com.foreknow.bean.UserInfo">
select*from user
<!-- 导航语音userInfo -->
<where>
<!-- include是包含 refid是引用 引用sql 的 ID -->
<include refid="query_user_where"></include>
</where>
</select >
<!-- 查询订单关联查询用户信息,使用resultmap -->
<select id="findOrderUser" resultType="com.foreknow.bean.OrderUserInfo">
SELECT
orders.*,
USER.username,
USER.sex,
USER.address
FROM
orders,
USER
WHERE orders.user_id = user.id
</select>
<!-- 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 -->
<!-- 查询订单关联查询用户信息,使用resultmap association (一对一)-->
<resultMap type="com.foreknow.Orders" id="OrdersUserResultMap">
<!-- 配置映射的订单信息 -->
<!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
column:订单信息的唯 一标识 列
property:订单信息的唯 一标识 列所映射到Orders中哪个属性
-->
<id column="id" property="id"