一、configuration(配置)-全局配置文件
1、属性(properties)
a:可使用加载properties配置文件。
<properties resource="application.properties"/>
b:可以直接在标签内部配置属性
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
c:如果一个属性在不只一个地方进行了配置,那么,MyBatis 将按照下面的顺序来加载:
- 首先读取在 properties 元素体内指定的属性。
- 然后根据 properties 元素中的 resource 属性读取类路径下属性文件,或根据 url 属性指定的路径读取属性文件,并覆盖之前读取过的同名属性。
- 最后读取作为方法参数传递的属性,并覆盖之前读取过的同名属性。
2、设置(settings)
作用概述:MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。—一般设置缓存,驼峰配置
a:mapUnderscoreToCamelCase —驼峰
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3、映射器(mappers)
作用概述:定义 SQL 映射语句
注意:定义了mapper.xml文件,需要告诉全局配置文件mybatis-config.xml它在什么地方。须在mybatis-config.xml里注册mapper.xml
a:使用resource类路径下去找
<!-- 使用相对于类路径的资源引用 -->
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
</mappers>
b:使用class用映射接口的完全限定类名
注意:使用注解版映射接口,可以不用mapper.xml文件,若使用普通映射接口,需保证他们在同包下且mapper.xml以映射接口名命名。
<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
c:使用url完全限定资源定位符
<!-- 使用完全限定资源定位符(URL) -->
<mappers>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
d:使用批量扫描package
注意:要保证编译后映射接口与mapper.xml在同包下
<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
二、XML 映射器
1.自增主键获取
注意:Insert, Update, Delete 元素的属性特有
useGeneratedKeys–使用自增主键
keyProperty–自增主键的值赋值的属性-对象的
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into teacher (id,te_name,te_age) values (#{id},#{teName},#{teAge})
</insert>
2.参数处理
原理:
a:单个参数
StudentMapper
Student findById(Integer id);
StudentMapper.xml
<select id="findById" resultType="cdtu.ll.pojo.Student">
select * from student where id=#{hh}
</select>
注意:由于参数只有一个,所以id=#{}里面的值可以随便取,推荐方法参数名id
b:javaBean
StudentMapper
void update(Student stu);
StudentMapper.xml
<update id="update" >
update student set stu_name=#{stuName},stu_age=#{stuAge} where id=#{id}
</update>
注意:#{}里面写实体对象属性
c:多个参数
c1:默认规则
底层会将参数封装成一个map,默认以arg1…作为map得key,参数值作为value。
Student findByAgeAndName( Integer age, String name);
<select id="findByAgeAndName" resultType="cdtu.ll.pojo.Student">
select * from student where stu_age=#{arg0} and stu_name=#{arg1}
</select>
c2:自定义规则
底层会将参数封装成一个map,默认以arg1…作为map得key,参数值作为value,也可以自定义map key
Student findByAgeAndName( @Param("age") Integer age, @Param("name") String name);
<select id="findByAgeAndName" resultType="cdtu.ll.pojo.Student">
select * from student where stu_age=#{age} and stu_name=#{name}
</select>
d:传入map
直接以map封装的key就是,没啥好说的
e.交错传参
Student find(Integer id,Student stu,String other);
这里只举个例,并无实际逻辑意义
<select id="find" resultType="cdtu.ll.pojo.Student">
select * from student where id=#{arg0} and stu_name=#{arg1.stuName}........ and other={arg3}
</select>
三.结果集映射处理
1、简单映射
<select id="findById" resultType="cdtu.ll.pojo.Student">
select * from student where id=#{hh}
</select>
resultType里放结果类型
2、resultMap
- 待解决问题: 结果集(表)中的列名和对象中的属性名称不匹配
- 解决方案: 使用resultMap元素
<resultMap id="book" type="cdtu.ll.pojo.Student">
<!--主键类型-->
<id property="id" column="id"></id>
<!--实体类属性 property--实体类属性名 colume--数据库表字段名 -->
<result property="stuName" column="stu_name"></result>
<result property="stuAge" column="stu_age"></result>
<!--集合映射 ofType--集合里实体类全限定类名-->
<collection property="books" ofType="cdtu.ll.pojo.Book">
<id property="id" column="id"></id>
<result property="bookName" column="book_name"></result>
<result property="stuId" column="stu_id"></result>
<!--实体类映射 javaType--实体类全限定类名-->
<association property="stu" javaType="cdtu.ll.pojo.Student">
<id property="id" column="id"></id>
<result property="stuName" column="stu_name"></result>
<result property="stuAge" column="stu_age"></result>
</association>
</collection>
</resultMap>
public class Student {
private int id;
private String stuName;
private int stuAge;
private List<Book> books;
//getter、setter....
}
public class Book {
private int id;
private String bookName;
private int stuId;
private Student stu;
//getter、setter....
}
四、注解开发
// 查询某个用户
@Select("SELECT id AS u_id, name AS u_name, pwd AS u_pwd FROM user WHERE id = ${id}")
@ResultMap("BaseResultMap")
User getOneUser(int id);
// 查询所有用户
@Select("SELECT id AS u_id, name AS u_name, pwd AS u_pwd FROM user")
@Results(id = "BaseResultMap", value = {
@Result(column = "u_id", property = "id"),
@Result(column = "u_name", property = "name"),
@Result(column = "u_pwd", property = "pwd")
})
List<User> getUserList();