1. 程式人生 > >javaEE Mybatis,動態sql,if標籤、where標籤、foreach標籤,sql片段

javaEE Mybatis,動態sql,if標籤、where標籤、foreach標籤,sql片段

UserMapper.xml(實體類的Sql配置檔案,動態sql(標籤),sql片段):

<?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">

<!-- 配置原生Sql語句   -->
<mapper namespace="com.xxx.mybatis.mapper.UserMapper"> 
	
	<!-- Sql片段 -->
	<sql id="selector">
		select id, username, birthday, sex, address from user
	</sql>
	
	<!-- <if>標籤, <where>標籤 -->
	<!-- 根據性別和名字查詢使用者(多個where條件)。  <where>標籤可以自動去掉條件前面多餘的and (條件後面多餘的and不能去掉,所以and要寫在條件前面)   -->
	<select id="selectUserBySexAndUsername" parameterType="User" resultType="User">
		<!-- <include refid="selector"/> -->  <!-- 可以通過include標籤引入sql片段,避免書寫大量重複的sql -->
		select * from user 
		<where>
		 	<if test="sex != null and sex != ''">  <!-- <if>標籤。 如果輸入引數user物件的sex屬性不為null且不為'' -->
				and sex = #{sex} 
			</if>
			<if test="username != null and username != ''">
				and username = #{username}
			</if>
		</where>
	</select>
	
	<!-- <foreach>標籤 -->
	<!-- 根據多個ID查詢。  id in (1,2,3) -->
	<select id="selectUserByIds" resultType="User">  <!-- 可以通過包裝類屬性設定要遍歷的集合(或陣列)。 parameterType="QueryVo" -->
		<include refid="selector"/>  <!-- 可以通過include標籤引入sql片段,避免書寫大量重複的sql -->
		<where>
			<!-- id in (1,2,3) -->
			<!-- collection="ids" 可以遍歷包裝類的ids屬性(List型別或陣列型別)。 
				collection="list" 表示遍歷的是集合。
				collection="array" 表示遍歷的是陣列 -->
			<foreach collection="list" item="id" separator=","  open="id in ("  close=")">
				#{id}
			</foreach>
		</where>
	</select>

</mapper>

QueryVo.java(包裝類,封裝sql佔位符的輸入引數):

package com.xxx.mybatis.pojo;

import java.io.Serializable;
import java.util.List;

// pojo包裝物件
public class QueryVo implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	//private User user;  // 簡單pojo物件作為包裝物件的屬性
	
	List<Integer> ids;  //動態sql;在mapper.xml檔案中可以通過foreach標籤遍歷生成動態sql。
	
	//Integer[] ids;
	
	
	public List<Integer> getIdsList() {
		return ids;
	}
	public void setIdsList(List<Integer> ids) {
		this.ids = ids;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
}

Test.java(測試類):

package com.xxx.mybatis.junit;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.xxx.mybatis.mapper.UserMapper;
import com.xxx.mybatis.pojo.User;

public class Test {
	
	//根據性別和名字查詢使用者
	@Test
	public void testfindUserBySexAndUsername() throws Exception {
		String resource = "sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//---------------------------------------------------------------------
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user = new User();
		user.setSex("1");
		//user.setUsername("張小明");
		List<User> users = userMapper.selectUserBySexAndUsername(user);
		for (User user2 : users) {
			System.out.println(user2);
		}
	}
	
	//根據多個ID查詢
	@Test
	public void testfindUserIDs() throws Exception {
		String resource = "sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//---------------------------------------------------------------------
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		//Integer[] ids = new Integer[]{16,24,22};
		List<Integer> ids  = new ArrayList<Integer>();
		ids.add(16);
		ids.add(22);
		ids.add(24);
		List<User> users = userMapper.selectUserByIds(ids);  //ids可以是List也可以是陣列

		/* QueryVo vo = new QueryVo();  //包裝類
		vo.setIdsList(ids);
		List<User> users = userMapper.selectUserByIds(vo); */
		
		for (User user : users) {
			System.out.println(user);
		}
	}
	
}

UserMapper.java(遵循四個原則的Dao層介面):

package com.xxx.mybatis.mapper;

import java.util.List;

import com.xxx.mybatis.pojo.QueryVo;
import com.xxx.mybatis.pojo.User;


//其實就是Dao層介面
public interface UserMapper {
	
	//遵循四個原則:
	//User.xml中配置的名稱空間要與該介面的全類名保持一致(com.xxx.mybatis.mapper.UserMapper) 
	//介面中的方法名  == User.xml中配置的sql語句的id名
	//返回值型別  與  User.xml檔案中配置的返回值型別(resultType)要一致
	//方法的輸入引數型別 與User.xml中配置的輸入引數的型別(parameterType)要一致
	
	
	//根據性別和名字查詢使用者
	public List<User> selectUserBySexAndUsername(User user);
	//根據多個id查詢使用者資訊  
	//public List<User> selectUserByIds(Integer[] ids);  //引數是陣列
	public List<User> selectUserByIds(List<Integer> ids);   //引數是List
	//public List<User> selectUserByIds(QueryVo vo);  //引數是包裝類
	
}