需求:使用模糊查询,查询出年龄在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} and u_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