MyBatis配置详解
一、mybatis-config.xml的基本配置
environments 标签:
- 它表示配置数据源环境
- 它里面可以放多个environment
- 属性default,就是用来控制当前使用的数据源(值为environment标签中的Id的值)
environment标签:
- 表示一个具体的数据源环境信息
- 属性id,多个environment设置为不同的id,分别表示的意思也不同
- test表示测试环境下的数据源
- product表示生产环境下的数据源
- 它里面的标签ransactionManager(代表事务管理器)
- 有一个属性type:
- =“JDBC” 表示当前MyBatis采用JDBC的事务
- =“MANAGERED” 表示mybaits不管理事务,交给其他的框架管理
- 有一个属性type:
- 它里面有一个dataSource标签来表示数据源
dataSource标签:
- 使用标准的JDBC数据源接口来配置JDBC连接对象的资源
- 属性type:
- =“POOLED” 使用数据库连接池
- =“UNPOOLED” 不使用连接池
- 它里面的标签property,表示具体的配置信息
typeAliases标签:
- 为类设置别名,(typeAliases标签一定要写在environment标签的前面)
- 设置别名后,在配置文件中使用POJO对象就不需要使用全限定名了
- 里面有typeAlias标签,typeAlias标签有属性type值为具体的类的全限定名,使用alias属性也可以为该类自定义一个新名字
- package标签,批量扫描类,该包中的所有类都可以直接写类名使用
mappers标签:
- 用来注册SQL映射文件
- 它里面有一个mapper标签,可以为mapper设置不同的属性:
- resource属性,加载SQL映射文件,值对应一个xml文件
- class属性,使用注解来配置Mapper接口,值对应一个Mapper接口类
- package标签,批量扫描注册映射文件,name属性,值对应一个包名
<?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="com.bdit.pojo.User"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/case01?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="userMapper.xml"/>
<mapper class="com.bdit.dao.IUserMapper"/>
<package name="com.bdit.mapper"/>
</mappers>
</configuration>
properties配置
上面在< dataSource>标签中,直接给value值配置了数据库的连接信息,还可以使用properties标签配置连接信息
还是使用property子标签设置属性name和value进行匹配
<properties >
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/case01?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
还可以将数据库连接信息写在properties配置文件中:
1、在properties标签中有一个resource属性可以匹配和它同路径下的一个properties文件
<properties resource="db.properties"></properties>
2、也可以使用url属性,配置一个properties文件的绝对路径名即可
<properties url="F:\IdeaProjects\javaweb\myBatis_day01\src\db.properties"></properties>
properties文件中:
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/case01?serverTimezone=GMT%2B8
username = root
password = root
Mybatis九大类全局配置节点(标签)位置不能随意放,一定按照如下顺序排序:
properties => settings => typeAliases => typeHandlers => objectFactory => plugins => environment => databaseIdProvider => mappers
官网学习:https://mybatis.org/mybatis-3/zh/configuration.html
二、Mapper.xml的基本配置
该文件是对SQL语句的具体业务实现的配置
顶级标签
该文件中只有很少的几个顶级标签:
- insert:映射插入语句
- delete:映射删除语句
- update:映射更新语句
- select:映射查询语句
- sql:可被其他语句引用的语句块
- cache:缓存配置
- cache-ref:引用其他文件中的缓存配置
- resultMap:描述结果集(最复杂、最有用的标签)
#{}和${}
#{}是预编译处理,${}是字符串替换。
-
Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值;
-
Mybatis在处理${}时,就是把${}替换成变量的值。
-
使用#{}可以有效的防止SQL注入,提高系统安全性。
在使用模糊查询时,就会发现区别:
使用#{}不需要模糊查询的字符串不需要加 ’ ’ ,使用${}则需要加 ’ ’ 因为它就是把sql语句拼接起来,不做任何处理
<select id="findByName" resultType="User" parameterType="String">
select * from t_user where username like #{name}
</select>
<select id="findByName" resultType="User" parameterType="String">
select * from t_user where username like '${name}'
</select>
子标签selectKey
用在sql语句标签内,表示在主sql语句执行前后,可以执行一个selectKey中的语句
- 属性:
- order:设置before(先执行它里面的sql语句),设置为after(后执行它的sql语句)
- keyProperty:设置该语句执行后的结果应该被设置到哪个目标中,多个目标可以用逗号分开表示
可以用于添加数据后的到添加进去的数据的自增主键
<insert id="saveUser" parameterType="User">
<!-- 插入执行后在执行selectKey中的语句,将返回的结果放入属性id里面-->
<selectKey resultType="Integer" keyProperty="id" order="AFTER">
select last_insert_id();<!--返回上条插入的id-->
</selectKey>
insert into t_user(username,password,state,email,create_time,remark)
values(#{username},#{password},#{state},#{email},#{create_time},#{remark})
</insert>
这里是为了展示selectKey的用法,其实获得添加后数据的自增主键还有更简单的方法:
<!-- insert标签中有属性 useGeneratedKeys="true"得到插入后该数据的主键 keyProperty="id"将值给id属性 -->
<insert id="saveUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into t_user(username,password,state,email,create_time,remark)
values(#{username},#{password},#{state},#{email},#{create_time},#{remark})
</insert>
手动映射
当pojo和我们数据库中表的列名不同时,除了使用起别名的方式,就可以使用resultMap标签进行手动映射
<select id="findAll" resultMap="userMap">
SELECT * FROM user
</select>
<resultMap id="userMap" type="user">
<!-- id标签代表主键列 column数据库中的表名 propertyPOJO中的属性名-->
<id column="user_id" property="userId"/>
<result column="password" property="password"/>
<result column="user_email" property="email"/>
<result column="create_time" property="createTime"/>
</resultMap>