sql映射文件-高级映射
2.1sql映射文件
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="cn.smbms.dao.StudentMapper">
......
</mapper>
2.好处
- 面向接口编程,方法的定义在接口中。实现的方法在xml中。
3.mapper
- namespace指定的是哪个接口
- 接口的名称和xml的名称是一致的并且方法中需用id去指定是哪个方法
4.单条件查询
- id
- parameterType,传入值的类型
- resultType结果类型
<!--单条件查询-->
<select id="getStudentListByName" parameterType="string" resultType="Student">
select *
from student
where name like concat('%', #{name}, '%')
</select>
测试代码
//模糊查询
@Test
public void testListVague() {
SqlSession sqlSession = null;
int count = 0;
try {
sqlSession = MyBatisUtil.createSqlSession();
List<Student> list = sqlSession.getMapper(StudentMapper.class).getStudentListByName("张");
System.out.println(list.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
count = 0;
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
}
5.多条件查询
- 就是变变sql语句
- 传入的参数可以是 (id,name)可以是一个(map)
下面是采用的map的形式
方法
// 根据id和name精确查询
Student getStudentByIdAndName(Map map);
sql
- #中取得都是key的值
<!-- 多条件查询-->
<select id="getStudentByIdAndName" resultType="cn.smbms.pojo.Student" parameterType="map">
select *
from student
where name = #{name} and id=#{id}
</select>
yinsg
测试代码
sqlSession = MyBatisUtil.createSqlSession();
Map map=new LinkedHashMap();
map.put("id",2);
map.put("name","李四");
Student student = sqlSession.getMapper(StudentMapper.class). getStudentByIdAndName(map);
System.out.println(student.toString());
6.自定义的结果映射buzyi
- 为了显示查询后的结果
- resultType做自动映射的时候要是名称不一致的情况下需要指定别名
- resulteMap
案例对course和student进行连表查询
下面分别是course和student表
解决要映射的名称和类的名称不一致的情况’
- 假设student类的叫studentname,而数据库中是name,就需要做映射
- 用到resultMap设置映射
- result只能设置一些简单的属性
<!-- 设置映射-->
<resultMap id="student" type="Student">
<result property="id" column="id"/>
<result property="student_name" column="name"/>
</resultMap>
<!-- 多条件查询-->
<select id="getStudentByIdAndName" resultMap="student" parameterType="map">
select *
from student
where name = #{name} and id=#{id}
</select>
7.取消自动映射
<resultMap id="student" type="Student">
<!-- <result property="id" column="id"/>-->
<result property="student_name" column="name"/>
</resultMap>
结果是自动映射。
如何取消???在myabtis的中的配置文件中进行指定,此时查询出来的结果是不是做自动映射的,不设置的话就是不会进行显示的。
<settings>
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
2.2增加操作+事务
默认返回的值是影响的结果的行数,int,没有resulteMap和Type
- id
- parameterType,要添加的数据类型的限定名或者是别名
<!-- 添加信息-->
<insert id="addStudent" parameterType="student" >
insert into student (name)
values(#{student_name} )
</insert>
测试代码,采用事务
sqlSession = MyBatisUtil.createSqlSession();
Integer u = sqlSession.getMapper(StudentMapper.class).addStudent(new Student(null,"张三"));
//事务
sqlSession.commit();
- 修改代码
<update id="updateStudent" parameterType="student" >
update student set name=
#{student_name} where id=#{id}
</update>
- 测试代码
sqlSession = MyBatisUtil.createSqlSession();
Integer line = sqlSession.getMapper(StudentMapper.class).updateStudent(new Student(3,"张三"));
System.out.println("修改的用户的信息");
System.out.println(line.toString());
sqlSession.commit();
3.采用注解的形式传入多个参数
其实把作用就是改变map中key的叫法而已
- @Param注解,传入参数
- 有些场景传入参数的值为对象的类型并不是很合适,需要指定需要传入的类型,提高代码的可读性
接口的代码。将student_name的值变成了name
int updateStudentBiming(@Param("id") Integer id,@Param("name") String student_name);
- sql代码。此时的name的值就是@Param中传入的name,把原先的student_name变成了name。
<!-- 采用传入参数的方式-->
<update id="updateStudentBiming" parameterType="student">
update student
set name=
#{name}
where id = #{id}
</update>
测试代码
sqlSession = MyBatisUtil.createSqlSession();
Integer line = sqlSession.getMapper(StudentMapper.class).updateStudentBiming(3,"张三嘻嘻嘻");
System.out.println("修改的用户的信息");
System.out.println(line.toString());
sqlSession.commit();
同理
2.3高级结果映射(比较难)
2.3.1resultMap配置
- id,唯一
- type,映射的结果类型
- 子节点
- id,结果集的标识
- result,简单属性
- association,复杂类型
- collection,集合
<resultMap id="student" type="Student">
<result property="id" column="id"/>
<result property="student_name" column="name"/>
</resultMap>
2.3.2resultMap之association
基于学生和身份证之间的关系
public class Student {
private Integer id;
private String student_name;
private Card card;
}
接口
List<Student> getStudentAndCardList();
sql代码
<resultMap id="studentlist" type="Student">
<result property="id" column="id"/>
<result property="student_name" column="name"/>
<!--association映射的复杂类型-->
<association property="card">
<result property="id" column="id"/>
<result property="cardNum" column="cardNum"/>
<result property="sid" column="sid"/>
</association>
</resultMap>
<!-- getStudentAndCardList-->
<select id="getStudentAndCardList" resultMap="studentlist">
select s.*,c.* from student s join card c on s.id=c.sid;
</select>
查询结果
2.3.3resultMap之collection
涉及到的是一对多的关系才是
1.查询语句
<resultMap id="studentCourse" type="Student">
<result property="id" column="id"/>
<result property="student_name" column="name"/>
<collection property="courses" ofType="course" resultMap="courseList"/>
</resultMap>
<resultMap id="courseList" type="course">
<!--就是为了方便维护-->
<id property="sid" column="sid"/>
<result property="name" column="cname"/>
<result property="id" column="cid"/>
</resultMap>
<!-- 返回一个学生选的所有课程-->
<select id="getStudentAndCourseList" resultMap="studentCourse" parameterType="int">
select s.*,c.id as cid ,c.name as cname ,c.sid as sid from student s join course c on s.id = c.sid where s.id=#{id}
</select>
下面是改正的代码
<resultMap id="studentCourse" type="Student">
<result property="id" column="id"/>
<result property="student_name" column="name"/>
<collection property="courses" ofType="course" resultMap="courseList"/>
</resultMap>
<resultMap id="courseList" type="course">
<!--就是为了方便维护-->
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<result property="csid" column="sid"/>
</resultMap>
<!-- 返回一个学生选的所有课程-->
<select id="getStudentAndCourseList" resultMap="studentCourse" parameterType="int">
select s.*,c.id as cid ,c.name as cname ,c.sid as sid from student s join course c on s.id = c.sid where s.id=#{id}
</select>
注意:一个比较容易出错的地方查询出来的结果数据可能只有一个原因就是要查询出来的两个类之间的名称冲突了。
- 解决的方法就是设置不重名的属性即可
- Mybatis Collection查询集合只出现一条数据 方案解决的地址
2.3.4resultMap的自动映射级别和MyBtais缓存
- 如何配置自动映射的级别
- 了解如何设置缓存
三种方式
- FULL,不管是嵌套还是不嵌套都是会自动的进行匹配的
- NONE默认的就是不进行匹配的
- PARTIAL,默认的方式,如果说存在嵌套的话就不进行默认的匹配
<settings>
<setting name="autoMappingBehavior" value="NONE"/>
</settings>
2.3.5mybatis的缓存
存在着一级和二级的缓存
1.一级缓存
- 自带的基于HASHMAP的缓存。作用域是session,当session进行flush或者是进行close的时候就没有了。
2.二级缓存
- 基于全局的缓存,不在session之中的,可以被所有的sqlSession进行共享的。开启的话必须要设置mybatis的配置文件。也就是mybatis-config.xml就可以了。
首先在全局文件中进行设置二级缓存的配置,也就是打开缓存
<setting name="cacheEnabled" value="true"/>
设置mapper的缓存的配置
<!-- 设置全局的缓存-->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
当进行查询的时候就可以使用缓存了
- 只需要设置一个UserCache为true就可以了
useCache="true"
注意这种只是简单的具体需要由其他的缓存服务器去提供缓存的服务。