目录
1.查询要求
对学生表进行条件查询,涉及姓名、性别和年龄三个字段
2.打开前面的mybatisone项目
3.对学生表实现条件查询
(1).创建学生映射器配置文件
<?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">
<mapper namespace="ztw.mybatis.mapper.StudentMapper">
<select id="findByCondition" parameterType="java.util.Map" resultMap="studentResultMap">
SELECT * FROM t_student
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="name != null">
s_name Like CONCAT(#{name},'%')
</if>
<if test="gender != null">
AND s_gender = #{gender}
</if>
<if test="age != null">
AND s_age = #{age}
</if>
</trim>
</select>
<resultMap id="studentResultMap" type="Student">
<result column="s_id" property="id"/>
<result column="s_name" property="name"/>
<result column="s_gender" property="gender"/>
<result column="s_age" property="age"/>
<association property="clazz" column="class_id" javaType="Clazz" select="getClazz"/>
</resultMap>
<select id="getClazz" resultType="Clazz">
SELECT c_id id, c_name name FROM t_class WHERE c_id = #{id};
</select>
</mapper>
(2).在MyBatis配置文件里注册学生映射器配置文件
<?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>
<typeAlias type="ztw.mybatis.bean.User" alias="User"/>
<typeAlias type="ztw.mybatis.bean.Clazz" alias="Clazz"/>
<typeAlias type="ztw.mybatis.bean.Teacher" alias="Teacher"/>
<typeAlias type="ztw.mybatis.bean.Student" alias="Student"/>
</typeAliases>
<!--配置数据库环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="15183644715"/>
</dataSource>
</environment>
</environments>
<!--配置实体关系映射器文件-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/ClazzMapper.xml"/>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
(3).创建学生映射器接口
package ztw.mybatis.mapper;
import ztw.mybatis.bean.Student;
import java.util.List;
import java.util.Map;
/**
* 学生映射器接口
*/
public interface StudentMapper {
List<Student> findByCondition(Map<String, Object> condition);
}
(4).创建测试类TestStudentMapper
package ztw.mybatis.mapper;
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.After;
import org.junit.Before;
import org.junit.Test;
import ztw.mybatis.bean.Student;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 测试学生映射器接口
*/
public class TestStudentMapper {
private SqlSession sqlSession; // SQL会话
private StudentMapper studentMapper; // 学生映射器
@Before
public void init() {
try {
// 读取MyBatis配置文件作为字符输入流
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 基于MyBatis配置文件构建SQL会话工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
// 利用SQL会话工厂获取SQL会话
sqlSession = factory.openSession();
// 利用SQL会话获取学生映射器对象
studentMapper = sqlSession.getMapper(StudentMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testFindCodition() {
HashMap<String,Object> condition = new HashMap<String, Object>();
condition.put("gender","女");
condition.put("age",20);
List<Student> students = studentMapper.findByCondition(condition);
students.forEach(student -> System.out.println(student));
}
@After
public void destroy() {
// 关闭SQL会话
sqlSession.close();
}
}