2023-01-09
一、Mybatis核心配置文件概述及根标签
1、核心配置文件的概述(即“mybatis-config.xml”)
MyBatis的配置文件包含了会深深影响MyBatis行为的设置和属性信息。
2、标签
(1)configuration(配置)
(2)properties(属性)
作用:定义或引入外部属性文件
在“day02_mybatis.src.main.resources”文件夹下创建“数据库的连接文件”,命名为“db.properties”
①db.properties里面的内容- #里面存放的数据格式为key=value
- db.driver=com.mysql.cj.jdbc.Driver
- db.url=jdbc:mysql://localhost:3306/db220106?serverTimezone=UTC
- db.username=设置自己的数据库的用户名称
- db.password=数据库的密码
复制代码 ②“day02_mybatis.src.main.resources”下的“mybatis-config.xml”中的内容- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>"https://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <properties resource="db.properties"></properties>
- <environments default="development">
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><environment id="development">
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> <transactionManager type="JDBC"/>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> <dataSource type="POOLED">
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><property name="driver" value="${db.driver}"/>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><property name="url" value="${db.url}"/>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><property name="username" value="${db.username}"/>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><property name="password" value="${db.password}"/>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> </dataSource>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings></environment>
- </environments>
-
- <mappers>
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><mapper resource="mapper/EmployeeMapper.xml"/>
- </mappers>
- </configuration>
复制代码 ③“day02_mybatis.src.test.java.TestMybatis”中的代码- import com.hh.mybatis.mapper.EmployeeMapper;
- import com.hh.mybatis.pojo.Employee;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import org.junit.Test;
- import java.io.IOException;
- import java.io.InputStream;
- public class TestMybatis {
- @Test
- public void TestMybatis(){
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>try {
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> String resource = "mybatis-config.xml";
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> InputStream inputStream = Resources.getResourceAsStream(resource);
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> //通过sqlSessionFactory获取sqlSession对象
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> SqlSession sqlSession = sqlSessionFactory.openSession();
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> //获取EmployeeMapper的代理对象
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> System.out.println("employeeMapper.getClass().getName() = " + employeeMapper.getClass().getName());
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> Employee employee = employeeMapper.selectEmployee(1);
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> System.out.println("employee = " + employee);
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>} catch (IOException e) {
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> e.printStackTrace();
- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>}
- }
- }
复制代码 ④测试代码,代码OK
(3)settings(设置)
①作用:这是MyBatis中极为重要的调整设置,它们会改变MyBatis的运行行为
②例如:mapUnderscoreToCamelCase属性:表示是否开启驼峰命名自动映射,默认值是false,如设置true会自动将字段last_name与lastName属性自动映射。
③在“day02_mybatis.src.main.resources”下的“mybatis-config.xml”中的“”设置- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 (4)typeAliases(类型别名)
①作用:类型别名可为java类型设置一个缩写名字。
②xml代码:- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 其中的xml代码也可以简写为- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 ③之后,可以将“day02_mybatis.src.mian.resources.mapper”下的“EmployeeMapper.xml”中的“resultType”设置为“employee”,如下:- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> SELECT<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> id,<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> last_name,<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> email,<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> salary<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>FROM<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> tbl_employee<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>WHERE<settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings> id=#{empId}
复制代码 (5)typeHandlers(类型处理器)
(6)objectFactory(对象工厂)
(7)plugins(插件)
(8)environments(环境配置)
①作用:设置数据库的连接环境
②实例xml代码- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 (9)databaseldProvider(数据库厂商标识)
(10)mappers(映射器)
①作用:设置映射文件路径
②xml代码:
例如:- <settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings><settings>
- <typeAliases>
- <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
- </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
- </settings>
复制代码 3、注意事项
核心配置中的子标签,是有顺序要求的。顺序如下:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |