提要:主要演示了mybatis中,在类中的其他自定义类,在sql语句中如何取值。sql语句中,范围sql与集合的对应
一、目录结构

二、相关实体类
QueryVo
package com.test.domain;
import java.io.Serializable;
import java.util.List;
public class QueryVo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private List<Integer> listId;
private Integer[] arrId;
public List<Integer> getListId() {
return listId;
}
public void setListId(List<Integer> listId) {
this.listId = listId;
}
public Integer[] getArrId() {
return arrId;
}
public void setArrId(Integer[] arrId) {
this.arrId = arrId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
orders类
package com.test.domain;
public class orders {
private int id;
private int userid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "orders [id=" + id + ", userid=" + userid + "]";
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
//必须要有的
public orders() {
super();
}
public orders(int id, int userid) {
super();
this.id = id;
this.userid = userid;
}
}
OrdersMapper接口
package com.test.mapper;
import java.util.List;
import com.test.domain.QueryVo;
import com.test.domain.orders;
public interface OrdersMapper {
public List<orders> queryList();
public orders queryOne(orders one);
public List<orders> queryByIdlist(QueryVo vo);
public List<orders> queryByIdArr(QueryVo vo);
public List<orders> queryByIdlist2(List<Integer> list);
public List<orders> queryByIdArr2(Integer[] arr);
}
VoMapper接口
package com.test.mapper;
import java.util.List;
import com.test.domain.QueryVo;
import com.test.domain.User;
public interface VoMapper {
public List<User> queryList(QueryVo vo);
public int queryCount();
}
三、相关配置文件
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.test.domain"/>
<!--
<typeAlias type="com.test.domain.User" alias="User"/>
-->
</typeAliases>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="a" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="MapConfig\UserMapper.xml"/>
<mapper resource="MapConfig\VoMapper.xml"/>
<mapper resource="MapConfig\OrdersMapper.xml"/>
<!--
-->
</mappers>
</configuration>
OrdersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 注意不要弄错了,配置文件是config,这个是映射mapper -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.OrdersMapper">
<resultMap type="com.test.domain.orders" id="OrdersResultMap">
<id column="id" property="id"/>
<result column="user_id" property="userid"/>
</resultMap>
<select id="queryByIdArr2" parameterType="java.lang.Integer" resultMap="OrdersResultMap">
select * from orders
<where>
<foreach collection="array" item = "item" open="id in (" close = ")" separator=",">
#{item}
</foreach>
</where>
</select>
<select id="queryByIdArr" parameterType="com.test.domain.QueryVo" resultMap="OrdersResultMap">
select * from orders
<where>
<foreach collection="arrId" item = "item" open="id in (" close = ")" separator=",">
#{item}
</foreach>
</where>
</select>
<select id="queryByIdlist" parameterType="com.test.domain.QueryVo" resultMap="OrdersResultMap">
select * from orders
<where>
<foreach collection="listId" item = "item" open ="id IN(" close =")" separator=",">
#{item}
</foreach>
</where>
</select>
<select id="queryList" resultMap="OrdersResultMap">
select id,user_id from orders
</select>
<select id="queryOne" parameterType="orders" resultMap="OrdersResultMap" >
select id,user_id from orders
<where>
<if test="id != null ">
and id = #{id}
</if>
<if test="userid != null">
and user_id= #{userid}
</if>
</where>
</select>
</mapper>
VoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 注意不要弄错了,配置文件是config,这个是映射mapper -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.VoMapper">
<select id="queryList" parameterType="com.test.domain.QueryVo" resultType="com.test.domain.User">
select * from user where username like '%${user.username}%'
</select>
<select id="queryCount" resultType="Integer">
select count(id) from user
</select>
</mapper>