1. 程式人生 > >Mybatis中like模糊查詢

Mybatis中like模糊查詢

需求:使用模糊查詢,查詢出年齡在23至27之間員工資訊?

員工表:t_user


1.使用mysql進行sql查詢

select * from t_user where u_name like 'etc%' and u_age between 23 and 72;
執行效果:


2.使用mybatis,實現查詢條件

1)employee.java:該實體類主要實現JavaBean物件屬性與表字段對應

package com.casv.model;

public class employee {

	private int uid;
	private String name;
	private String pwd;
	private int age;
	private department dept;

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public department getDept() {
		return dept;
	}

	public void setDept(department dept) {
		this.dept = dept;
	}

	public employee() {
		super();
	}
	
	public String toString() {
		return "employee [uid=" + uid + ", name=" + name + ", pwd=" + pwd
				+ ", age=" + age + ", dept=" + dept + "]";
	}

	public employee(int uid, String name, String pwd, int age, department dept) {
		super();
		this.uid = uid;
		this.name = name;
		this.pwd = pwd;
		this.age = age;
		this.dept = dept;
	}

}
2)employeeCondition.java:查詢條件實體類,主要是sql語句需要這些引數條件
package com.casv.model;

//查詢條件實體類
public class employeeCondition {

	private String uname;
	private int minAge;  
	private int maxAge;  

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public int getMinAge() {
		return minAge;
	}

	public void setMinAge(int minAge) {
		this.minAge = minAge;
	}

	public int getMaxAge() {
		return maxAge;
	}

	public void setMaxAge(int maxAge) {
		this.maxAge = maxAge;
	}

	public employeeCondition() {
		super();
	}

	@Override
	public String toString() {
		return "ConditionEmployee [uname=" + uname + ", minAge=" + minAge
				+ ", maxAge=" + maxAge + "]";
	}

	public employeeCondition(String uname, int minAge, int maxAge) {
		super();
		this.uname = uname;
		this.minAge = minAge;
		this.maxAge = maxAge;
	}

}
3)userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- namespace:名稱空間,對應dao介面 --> 
<mapper namespace="com.casv.dao.userMapper">
	<!-- 配置resultMap屬性,進行表字段與實體類屬性對映 -->
	<resultMap id="BaseResultMap" type="emps">
		<id column="u_id" property="uid"></id>
		<result column="u_name" property="name"></result>
		<result column="u_pwd" property="pwd"></result>
		<result column="u_age" property="age"></result>
	</resultMap>
	<!--
	   resultType:指定返回結果型別,可以是基本資料型別,也可以是java容器及javabean;
	   parameterType:指定引數型別,可以是基本資料型別,也可以是物件; 
	-->
	<!--  
	   1、實現模糊查詢t_user中年齡23至27之間的員工資訊
	   2、由於引數型別parameterType:“employeeCondition”,所以,
	      sql語句的條件引數是取employeeCondition實體類物件中欄位屬性(uname,minAge,maxAge
),需保持一致 --> <select id="employeelikebyage" parameterType="com.casv.model.employeeCondition" resultType="emps" resultMap="BaseResultMap"> select * from t_user where u_name like #{uname} andu_age between #{minAge} and #{maxAge}; </select> </mapper>
4)config.xml中註冊userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
	<!-- 配置實體類,起別名 -->
	<typeAliases>
		<typeAlias type="com.casv.model.employee" alias="emps" />
		<!-- 掃描實體類包,後續可以直接使用類名
		<package name="com.casv.model.User"/>
		-->
	</typeAliases>
	<!-- 配置資料來源 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/userdatabase" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- 註冊userMapper.xml -->
		<mapper resource="com/casv/dao/userMapper.xml"></mapper>
	</mappers>
</configuration>
5)編寫Test測試類
@Test
public void test3(){
session=MyBatisUtil.getSessionFactory().openSession();
String name="etc";
//獲取查詢條件實體類employeeCondition,傳入條件引數uname,minAge,maxAge
employeeCondition parameter=new employeeCondition(name+"%",23, 72);
List<employee> list=session.selectList("com.casv.dao.userMapper.employeelikebyage", parameter);
for(employee emps : list){
	System.out.println("姓名:"+emps.getName()+" "+"年齡: "+emps.getAge());
}
session.close();
}
執行結果:
姓名:etcxd    年齡: 23
姓名:etcsm    年齡: 53
姓名:etccbw   年齡: 72