MyBatis 快速学习01:第一个程序
MyBatis 快速学习02:MyBatis的CRUD操作
目录
MyBatis主配置文件
mybatis-config.xml是MyBatis的主配置文件,只是用于配置MyBatis数据源及属性信息
全局配置文件结构
configuration 配置
properties 属性:可以加载properties配置文件的信息
settings 设置:可以设置mybatis的全局属性
typeAliases 类型命名
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境
environment 环境变量
transactionManager 事务管理器
dataSource 数据源
databaseIdProvider 数据库厂商标识
mappers 映射器
<!-- 元素节点的顺序,顺序不对会报错 -->
properties标签
用于设置键值对或加载属性文件
再resources
目录下创建db.properties
文件,配置键值对如下:
mysql_driver = com.mysql.jdbc.Driver
mysql_url = jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSl=true&useUnicode=true
mysql_username= root
mysql_password = admin123
在mybatis-config.xml配置文件通过properties
引用db.properties
文件,在配置environment
时可以直接使用其key获取对应的value。
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysql_driver}"/>
<property name="url" value="${mysql_url}"/>
<property name="username" value="${mysql_username}"/>
<property name="password" value="${mysql_password}"/>
</dataSource>
</environment>
</environments>
settings标签
<!--设置mybatis的属性-->
<settings>
<!-- 启动二级缓存-->
<setting name="cacheEnabled" value="true"/>
<!-- 启动延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
typeAliases标签
类别名是为Java类型设置一个短的名字,它只和XML配置有关,减少类完全限定名的冗余
<!--typeAliases标签用于给实体类取别名,在映射文件中可以直接使用别名来替代实体类的全限定名-->
<typeAliases>
<typeAlias type="com.qgc.pojo.User" alias="User"></typeAlias>
</typeAliases>
<!--指定一个包名,MyBatis会在包名下搜索需要的Java Bean-->
<typeAliases>
<package name="com.qgc.pojo"></package>
</typeAliases>
plugins标签
<!--plugins标签,用于配置MyBatis插件(分页插件)-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugins>
environments标签
<!-- 在environments配置数据库连接信息 -->
<!-- 在environments标签中可以定义多个environment标签,每个environment标签可以定义一套连接配置 -->
<!-- default属性,用来指定使用哪个environment标签 -->
<environments default="development">
<!-- environment 标签用于配置数据库连接信息 -->
<environment id="development">
<!--transactionManager标签用于配置数据库管理方式
type="JDBC" 可以进行事务的提交和回滚操作
type="MANAGED" 依赖容器完成事务管理,本身不进行事务的提交和回滚操作 -->
<transactionManager type="JDBC"></transactionManager>
<!--dataSource标签就是用来配置数据库连接信息 POOLED|UNPOOLED -->
<dataSource type="POOLED">
<property name="driver" value="${mysql_driver}"/>
<property name="url" value="${mysql_url}"/>
<property name="username" value="${mysql_username}"/>
<property name="password" value="${mysql_password}"/>
</dataSource>
</environment>
</environments>
mappers标签
映射器:定义映射SQL语句文件,加载映射配置(映射文件、DAO注解)
MyBatis的行为其他元素已经配置完了,就要定义SQL映射语句。但是要告知MyBatis到哪里去找到这些SQL语句
Java在自动查找这方面没有提供一个很好的方法,最佳方法是告诉MyBatis到哪里去找映射文件
引入资源方式(注册mapper)
<!--使用相对于类路径-->
<mappers>
<mapper resource="mappers/UserMapper.xml"></mapper>
</mappers>
映射文件
MyBatis Mapper初始化
XML文件解析:读取xml文件中的标签配置封装到Java对象中
mapper根标签
mapper文件相当于DAO接口的
实现类
namespace属性中的名称必须要与对应接口的完整包名一致
<mapper namespace="com.qgc.dao.UserDAO">
</mapper>
<!--对应接口-->
package com.qgc.dao;
public interface UserDAO {...}
insert标签
声明添加操作(sql: insert ...)
常用属性
id属性,绑定对应DAO接口中的方法
parameterType属性,用以指定接口中对应方法的参数类型(可省略)
useGeneratedKeys属性, 设置添加操作是否需要回填生成的主键
keyProperty属性,指定回填的id设置到参数对象中的哪个属性
timeout属性,设置此操作的超时时间,如果不设置则一直等待
主键回填
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="stuId">
insert into tb_students(stu_num, stu_name, stu_gender, stu_age)
values (#{stuNum}, #{stuName}, #{stuGender}, #{stuAge})
</insert>
<insert id="insertStudent" >
<selectKey keyProperty="stuId" resultType="java.lang.Integer">
select last_insert_id()
</selectKey>
insert into tb_students(stu_num, stu_name, stu_gender, stu_age)
values (#{stuNum}, #{stuName}, #{stuGender}, #{stuAge})
</insert>
delete标签
声明删除操作
update标签
声明修改操作
select标签
声明查询操作
id属性, 指定绑定方法的方法名(接口中的方法与映射文件中的SQL语句
id
一一对应)parameterType属性,设置参数类型
resultType属性,指定当前sql返回数据封装的对象类型(实体类)
resultMap属性,指定从数据表到实体类的字段和属性的对应关系
useCache属性,指定此查询操作是否需要缓存
timeout属性,设置超时时间
resultMap标签
<!-- resultMap标签用于定义实体类与数据表的映射关系(ORM) -->
<resultMap id="studentMap" type="Student">
<id column="sid" property="stuId"/>
<result column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_gender" property="stuGender"/>
<result column="stu_age" property="stuAge"/>
</resultMap>
cache标签
设置当前DAO进行数据库操作时的缓存属性设置
<cache type="" size="" readOnly="false"/>
sql和include
SQL片段
<sql id="wanglaoji">sid , stu_num , stu_name , stu_gender , stu_age</sql>
<select id="listStudents" resultMap="studentMap">
select <include refid="wanglaoji"/> from tb_students
</select>