1. 程式人生 > >【Mybatis】mapper動態代理和對映檔案配置標籤

【Mybatis】mapper動態代理和對映檔案配置標籤

提要:主要演示了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>