简单介绍一下mybatis全局配置文件中的配置标签:
一、properties:用于配置数据源连接信息
配置方式:
1.配置在dataSource 中的property中
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://hadoop202:3306/hymc"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
2.配置在configuration 下的 properties 中,这用方式可以直接配置property子标签,也可以引入配置文件
注意一般会采用这种方式结合配置文件来配置,便于维护与修改
1. property子标签方式
<properties >
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://hadoop202:3306/hymc"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
2. 配置文件使用方式(建议使用)
2.1创建jdbc.properties文件,添加如下内容
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://hadoop202:3306/hymc
username=root
password=root
2.2 引入配置文件
<properties resource="jdbc.properties"/>
3. 配置使用方式
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
3.程序参数传递:此种方式可用于对数据库连接信息的加密安全操作,配置文件里面的是加密后的数据,在代码中对数据进行解析
/**
* 参数传递设置数据源信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://hadoop202:3306/hymc
username=root
password=root
*/
@Test
public void source(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
InputStream jdbcStream = null;
InputStreamReader jdbcReader = null;
SqlSession openSession = null;
try {
//1. 读取配置
inputStream = Resources.getResourceAsStream(resource);
jdbcStream = Resources.getResourceAsStream("jdbc.properties");
jdbcReader = new InputStreamReader(jdbcStream);
Properties props = new Properties();
props.load(jdbcReader);
//2. 如果数据库账号加密,可以在这里进行解密设置
props.setProperty("username", props.getProperty("username"));
props.setProperty("password", props.getProperty("password"));
//3. 创建sqlsession
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
openSession = sqlSessionFactory.openSession();
//4 访问数据库
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.selectEmp(10);
System.out.println(employee);
/**
2019-07-26 10:24:35 INFO com.cjy.mybatis.typehandler.ListTypeHandler - method ====>>> getResult(ResultSet rs, String columnName)
2019-07-26 10:24:35 INFO com.cjy.mybatis.typehandler.MyDateTypeHandler - getResult(ResultSet rs, String columnName)....
Employee [id=10, userName=yanjiu, gender=1, email=yanjiu@163.com, createtime=Wed Jul 24 15:05:58 CST 2019, hobbys=[yuwen, shuxue, yingyu, zhengzhi]]
*/
} catch (IOException e) {
e.printStackTrace();
}finally{
openSession.close();
}
}
- 三种加载方式优先级
参数传递 > properties配置 > property
加载顺序是从后向前的property先开始,所以最后加载的会覆盖前面的。
注意: 三种方式不要混合使用,不方便管理,建议首选properties配置文件方式,需要加密则结合参数传递方式
二、settings 配置
https://blog.csdn.net/fageweiketang/article/details/80767532
配置示例:
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
三、别名配置
别名:一个代指的名称,因为开发过程中我们所定义的类名称过长,包过深所以在使用时麻烦,因此系统用一个简洁的名称代指,而这个名称可以在mybatis上下文路径中使用,也就是sql xml中使用
1. 系统默认定义别名:org.apache.ibatis.type.TypeAliasRegistry
public TypeAliasRegistry() {
registerAlias("string", String.class);
registerAlias("byte", Byte.class);
registerAlias("long", Long.class);
registerAlias("short", Short.class);
registerAlias("int", Integer.class);
registerAlias("integer", Integer.class);
registerAlias("double", Double.class);
registerAlias("float", Float.class);
registerAlias("boolean", Boolean.class);
registerAlias("byte[]", Byte[].class);
registerAlias("long[]", Long[].class);
registerAlias("short[]", Short[].class);
registerAlias("int[]", Integer[].class);
registerAlias("integer[]", Integer[].class);
registerAlias("double[]", Double[].class);
registerAlias("float[]", Float[].class);
registerAlias("boolean[]", Boolean[].class);
registerAlias("_byte", byte.class);
registerAlias("_long", long.class);
registerAlias("_short", short.class);
registerAlias("_int", int.class);
registerAlias("_integer", int.class);
registerAlias("_double", double.class);
registerAlias("_float", float.class);
registerAlias("_boolean", boolean.class);
registerAlias("_byte[]", byte[].class);
registerAlias("_long[]", long[].class);
registerAlias("_short[]", short[].class);
registerAlias("_int[]", int[].class);
registerAlias("_integer[]", int[].class);
registerAlias("_double[]", double[].class);
registerAlias("_float[]", float[].class);
registerAlias("_boolean[]", boolean[].class);
registerAlias("date", Date.class);
registerAlias("decimal", BigDecimal.class);
registerAlias("bigdecimal", BigDecimal.class);
registerAlias("biginteger", BigInteger.class);
registerAlias("object", Object.class);
registerAlias("date[]", Date[].class);
registerAlias("decimal[]", BigDecimal[].class);
registerAlias("bigdecimal[]", BigDecimal[].class);
registerAlias("biginteger[]", BigInteger[].class);
registerAlias("object[]", Object[].class);
registerAlias("map", Map.class);
registerAlias("hashmap", HashMap.class);
registerAlias("list", List.class);
registerAlias("arraylist", ArrayList.class);
registerAlias("collection", Collection.class);
registerAlias("iterator", Iterator.class);
registerAlias("ResultSet", ResultSet.class);
}
1. 自定义别名:
方式一:
<!--别名配置 :
注意: 切记使用默认别名的情况下不同包下的相同类名的类会使用同一个别名-->
<typeAliases>
<!--alias: alias属性不赋值,默认是类名首字母小写 -->
<typeAlias type="com.cjy.mybatis.entity.Employee"/>
<!-- <typeAlias type="com.cjy.mybatis.entity.Employee" alias="employee"/> -->
</typeAliases>
<!--int:为系统默认别名
employee : 自定义别名 -->
<select id="selectEmp" resultType="employee" parameterType="int">
select id,user_name userName,gender,email,hobbys,createtime from employee where id = #{id}
</select>
方式二:
//@Alias() : 不设置别名名称会报错
@Alias("employee")
public class Employee {}
<!--别名配置 :
注意: 切记使用默认别名的情况下不同包下的相同类名的类会使用同一个别名-->
<typeAliases>
<!--配置方式二:结合注解,扫描当前包下面的 @Alias 注解 -->
<package name="com.cjy.mybatis.entity"/>
</typeAliases>
四、typeHandler :
https://blog.csdn.net/qq_35241080/article/details/96998533
五、objectFactory:构建结果返回值时使用,就是查询结果封装成对象时所使用的。
默认:org.apache.ibatis.reflection.factory.DefaultObjectFactory
选择:可以自定义
- 自定义objectfactory
package com.cjy.mybatis.objectFacotry;
import java.util.List;
import java.util.Properties;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.log4j.Logger;
import com.cjy.mybatis.entity.Employee;
public class MyObjectFactory extends DefaultObjectFactory{
private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(MyObjectFactory.class);
@Override
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
logger.info("定制属性:setProperties" + properties);
super.setProperties(properties);
}
@Override
public <T> T create(Class<T> type) {
// TODO Auto-generated method stub
logger.info("使用定制对象工厂的create方法构建单个对象 -- " + type);
if(type == Employee.class){
logger.info("return new Employee() -- ");
Employee e = new Employee();
e.setObjectFacotry("我是自定义对象工程创建的实例对象~~~~");
return (T) e;
}
return super.create(type);
}
@Override
public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes,
List<Object> constructorArgs) {
// TODO Auto-generated method stub
logger.info("使用定制对象工厂的create方法构建列表对象 -- " + type);
return super.create(type, constructorArgTypes, constructorArgs);
}
@Override
public <T> boolean isCollection(Class<T> type) {
// TODO Auto-generated method stub
return super.isCollection(type);
}
}
- 配置objectFactory使其生效 mybatis-config文件
<objectFactory type="com.cjy.mybatis.objectFacotry.MyObjectFactory">
<property name="name" value="MyObjectFactory"/>
</objectFactory>
- 测试其效果
@Test
public void query(){
SqlSessionFactory sqlSessionFactory = MySqlSessionFacoty.getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee selectEmp = mapper.selectEmp(10);
System.out.println(selectEmp);
System.out.println("getObjectFacotry:"+selectEmp.getObjectFacotry());
}
日志记录
2019-07-26 15:22:13 INFO com.cjy.mybatis.objectFacotry.MyObjectFactory - 定制属性:setProperties{name=MyObjectFactory}
2019-07-26 15:22:23 DEBUG mybatis.sql.com.cjy.mybatis.dao.EmployeeMapper.selectEmp - ==> Preparing: select id,user_name userName,gender,email,hobbys,createtime from employee where id = ?
2019-07-26 15:22:23 DEBUG mybatis.sql.com.cjy.mybatis.dao.EmployeeMapper.selectEmp - ==> Parameters: 10(Integer)
2019-07-26 15:22:23 INFO com.cjy.mybatis.objectFacotry.MyObjectFactory - 使用定制对象工厂的create方法构建单个对象 -- interface java.util.List
2019-07-26 15:22:23 INFO com.cjy.mybatis.objectFacotry.MyObjectFactory - 使用定制对象工厂的create方法构建列表对象 -- interface java.util.List
2019-07-26 15:22:23 INFO com.cjy.mybatis.objectFacotry.MyObjectFactory - 使用定制对象工厂的create方法构建单个对象 -- class com.cjy.mybatis.entity.Employee
2019-07-26 15:22:23 INFO com.cjy.mybatis.objectFacotry.MyObjectFactory - return new Employee() --
2019-07-26 15:22:23 INFO com.cjy.mybatis.typehandler.ListTypeHandler - method ====>>> getResult(ResultSet rs, String columnName)
2019-07-26 15:22:23 INFO com.cjy.mybatis.typehandler.MyDateTypeHandler - getResult(ResultSet rs, String columnName)....
2019-07-26 15:22:23 DEBUG mybatis.sql.com.cjy.mybatis.dao.EmployeeMapper.selectEmp - <== Total: 1
Employee [id=10, userName=yanjiu, gender=1, email=yanjiu@163.com, createtime=Wed Jul 24 15:05:58 CST 2019, hobbys=[yuwen, shuxue, yingyu, zhengzhi]]
getObjectFacotry:我是自定义对象工程创建的实例对象~~~~
大部分情况都是使用默认ObjectFactory
六、plugins先不讲解
七、environments 配置环境
配置环境可以注册多个数据源。
数据源:分为两个部分,数据库源配置、数据库事物配置
下面先看一个完整的数据源配置,一般情况下一个工程都是配置一个数据源。
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"> <!--事务管理 -->
<property name="autoCommit" value="false"/>
</transactionManager>
<dataSource type="POOLED"> <!--连接方式 -->
<!-- 使用配置-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 测试数据库连接2
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://hadoop202:3306/hymc"/>
<property name="username" value="root"/>
<property name="password" value="root"/> -->
</dataSource>
</environment>
</environments>
接着解析一下配置环境的标签:
environments:标签中的default标明在默认情况下启用哪个数据源
environment:是配置一个数据源的开始,树形id是设置这个数据源的标志,以便mybatis上下文使用它
transactionManager:配置的是数据库事物,其type属性有三种配置方式;
1. JDBC:采用JDBC方式管理事物,经常使用此方式
2. MANAGED:采用容器方式管理事物,在JNDI数据源中常用(本人目前未使用过)。
3. 自定义:由使用者自定义数据库事物管理办法,适用于特殊用于(本人目前未使用过)
property:可以配置数据源的各类属性。
dataSource:是配置数据源连接的信息,type属性提供对数据库连接方式,其子标签property就是定义数据的各类参数
连接方式:1. UNPOOLED,非连接池数据库
2.POOLED,连接池数据库
3.JNDI,JNDI数据源
4.自定义数据源
通常只需要使用 UNPOOLED、POOLED、JNDI即可
八、数据库厂商标识
具体开发中使用极少,这里不做介绍,想了解可查看其它资料
九、引入映射器的方式
映射器是mybatis中最复杂、最核心的组件。这里只做引入方式介绍,具体介绍在后续。
引入方式:
<mappers>
<!--
下面就是四种引入映射器的四种方式:
①resource : 直接引入类路径下的文件,一般使用这种方式会将sql文件放置在resource 路径下(常用)
②class:接口名与映射文件必须同名(必须)并且放在同一路径下;如果没有映射文件,sql语句使用注解的方式就需要注册接口
③package:使用批量注册, 接口和映射文件必须同名,而且放在同一个路径里
④url:引用网络上或磁盘路径下文件,文件在磁盘上的路径
-->
<mapper url="file:///D:\workspace2014\source-mybatis\src\main\java\com\cjy\mybatis\dao\EmployeeMapper.xml"/>
<!--
<mapper resource="com/cjy/mybatis/dao/EmployeeMapper.xml"/>
<mapper class="com.cjy.mybatis.dao.EmployeeMapper"/>
<package name="com.cjy.mybatis.dao"/>
-->
</mappers>
https://blog.csdn.net/bestcxx/article/details/72966768