目录
MyBatis配置文件元素
1. property子元素
2. settings设置
3. typeAliases别名
4. typeHandler类型转换器
5. 引入映射器的方法
一、MyBatis配置文件元素
<!--配置-->
<configuration>
<!--属性-->
<properties/>
<!--设置-->
<settings>
<setting name="" value=""/>
</settings>
<!--类型别名-->
<typeAliases/>
<!--类型处理器-->
<typeHandlers/>
<!--对象工厂(不常用)-->
<objectFactory type=""/>
<!--插件-->
<plugins>
<plugin interceptor=""></plugin>
</plugins>
<!--配置环境-->
<environments default="">
<!--环境变量-->
<environment id="">
<!--事务管理器-->
<transactionManager type=""></transactionManager>
<!--数据源-->
<dataSource type=""></dataSource>
</environment>
</environments>
<!--数据库厂商标识(不常用)-->
<databaseIdProvider type=""/>
<!--映射器-->
<mappers/>
</configuration>
从dtd文档可以看出,MyBatis配置项的顺序不能颠倒
1.property子元素
有三种方式使用properties - properties子元素(不是很好) - properties文件 - 程序代码传递(对数据库用户和密码加密)这里主要介绍第二种和第三种方式
第二种:properties文件
dbconfig.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=root
mybatis-config.xml
<properties resource="dbconfig.properties"/>
<environments default="development">
<environment id="development">
<!--配置事务管理器,采用JDBC管理器方式-->
<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文件中.所以我们需要对密文进行解密(由系统提供解密工具类),然后把解密后的字符串重置到properties属性中
public class SqlSessionFactoryUtils {
private final static Class<SqlSessionFactoryUtils> LOCK = SqlSessionFactoryUtils.class;
private static SqlSessionFactory sqlSessionFactory = null;
// 加了private关键字,使得其它代码不能通过new的方式来创建它
private SqlSessionFactoryUtils(){ }
public static SqlSessionFactory getSqlSessionFactory(){
synchronized (LOCK) {
if(sqlSessionFactory != null) {
return sqlSessionFactory;
}
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
InputStream in = Resources.getResourceAsStream("dbconfig.properties");
Properties properties = new Properties();
properties.load(in);
String username = properties.getProperty("database.username");
String password = properties.getProperty("database.password");
// 解密用户和密码,并在属性中重置
properties.put("database.username", CodeUtils.decode(username));
properties.put("database.password", CodeUtils.decode(password));
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}
}
2. settings设置
常用的配置项- cacheEnabled:关于缓存
- lazyLoadingEnabled:级联相关
- aggressiveLazyLoading:级联相关
- autoMappingBehavior:自动映射相关
- mapUnderscoreToCamelCase:自动映射相关
- defaultExecutorType:关于执行器类型
配置项 | 作用 | 配置选项说明 | 默认值 |
---|---|---|---|
cacheEnabled | 该配置影响所有映射器中配置缓存的全局开关 | true或false | true |
lazyLoadingEnabled | 延迟加载的全局开关,开启时,所有关联对象都会延迟加载 | true或false | false |
aggressiveLazyLoading | 当启用时,对任意延迟属性的调用会使带有延迟加载属性的对象完整加载;反之,每种属性将会按需加载 | true或false | false |
autoMappingBehavior | 指定自动映射当中未知列时的行为,默认不处理 | NONE、WARING、FAILING | NONE |
mapUnderscoreToCamelCase | 是否开启自动驼峰命名规则映射 | true或fasle | false |
defaultExecutorType | 配置默认的执行器,SIMPLE是普通执行器,REUSE会重用预处理语句(prepared statements),BATCH执行器将重用语句并执行批量更新 | SIMPLE、REUSE、BATCH | SIMPLE |
3.typeAliases别名
typeAliases:该标签是给类型起别名的,类型别名处理器; 里面有多个typeAlas标签,该标签给指定的类起别名
typeAlias标签属性:
- type:指定类的全类名,默认的类的别名就是:简类名,别名不区分大小写
- alias:也可以通过该属性指定类型别名
- package:批量起别名,可以为指定的包及子包下的所有的类起别名,别名为:简类名,别名不区分大小写
注意:实际开发中不建议使用别名
<typeAliases>
<!--<typeAlias type="cn.whc.March_30.entity.Role" alias="role"/>-->
<package name="cn.whc.March_30.entity"/>
</typeAliases>
4.typeHandler类型转换器
MyBatis存在系统定义typeHandler和自定义typeHandler
系统定义typeHandler
5.引入映射器的方法
映射器(java接口和xml文件)是MyBatis最复杂、最核心的组件
映射器定义命名空间(namespace)的方法,命名空间对应的是一个接口的全路径,而不是实现类。
RoleMapper接口
package cn.whc.March_30.mapper;
import cn.whc.March_30.entity.Role;
import java.util.List;
public interface RoleMapper {
int insertRole(Role role);
int deleteRole(Long id);
int updateRole(Role role);
Role getRole(Long id);
List<Role> findRoles(String roleName);
}
RoleMapper.xml
<?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.whc.March_30.mapper.RoleMapper">
<insert id="insertRole" parameterType="role">
insert into t_role(role_name, note) values (#{roleName}, #{role})
</insert>
<delete id="deleteRole" parameterType="long">
delete from t_role where id = #{id}
</delete>
<update id="updateRole" parameterType="role">
update t_role set role_name = #{roleName}, note = #{note} where id = #{id}
</update>
<select id="getRole" parameterType="long" resultType="role">
select id, role_name as roleName,note from t_role where id = #{id}
</select>
<select id="findRoles" parameterType="string" resultType="role">
select id, role_name as roleName, note from t_role
where role_name like concat('%', #{roleName}, '%')
</select>
</mapper>
mybatis-config.xml中文件路径引入映射器
<mappers>
<mapper resource="mappers/RoleMapper.xml"/>
</mappers>