学习笔记——Mybatis核心配置文件概述及根标签

打印 上一主题 下一主题

主题 595|帖子 595|积分 1785

2023-01-09
一、Mybatis核心配置文件概述及根标签
1、核心配置文件的概述(即“mybatis-config.xml”)
MyBatis的配置文件包含了会深深影响MyBatis行为的设置和属性信息。
2、标签
(1)configuration(配置)
(2)properties(属性)
作用:定义或引入外部属性文件
在“day02_mybatis.src.main.resources”文件夹下创建“数据库的连接文件”,命名为“db.properties”
①db.properties里面的内容
  1. #里面存放的数据格式为key=value
  2. db.driver=com.mysql.cj.jdbc.Driver
  3. db.url=jdbc:mysql://localhost:3306/db220106?serverTimezone=UTC
  4. db.username=设置自己的数据库的用户名称
  5. db.password=数据库的密码
复制代码
②“day02_mybatis.src.main.resources”下的“mybatis-config.xml”中的内容
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. <settings>
  4. <typeAliases>
  5.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  6. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  7. </settings>PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  8. <settings>
  9. <typeAliases>
  10.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  11. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  12. </settings>"https://mybatis.org/dtd/mybatis-3-config.dtd">
  13. <configuration>
  14.     <properties resource="db.properties"></properties>
  15.     <environments default="development">
  16. <settings>
  17. <typeAliases>
  18.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  19. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  20. </settings><environment id="development">
  21. <settings>
  22. <typeAliases>
  23.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  24. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  25. </settings>    <transactionManager type="JDBC"/>
  26. <settings>
  27. <typeAliases>
  28.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  29. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  30. </settings>    <dataSource type="POOLED">
  31. <settings>
  32. <typeAliases>
  33.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  34. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  35. </settings><settings>
  36. <typeAliases>
  37.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  38. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  39. </settings><property name="driver" value="${db.driver}"/>
  40. <settings>
  41. <typeAliases>
  42.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  43. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  44. </settings><settings>
  45. <typeAliases>
  46.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  47. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  48. </settings><property name="url" value="${db.url}"/>
  49. <settings>
  50. <typeAliases>
  51.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  52. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  53. </settings><settings>
  54. <typeAliases>
  55.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  56. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  57. </settings><property name="username" value="${db.username}"/>
  58. <settings>
  59. <typeAliases>
  60.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  61. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  62. </settings><settings>
  63. <typeAliases>
  64.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  65. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  66. </settings><property name="password" value="${db.password}"/>
  67. <settings>
  68. <typeAliases>
  69.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  70. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  71. </settings>    </dataSource>
  72. <settings>
  73. <typeAliases>
  74.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  75. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  76. </settings></environment>
  77.     </environments>
  78.    
  79.     <mappers>
  80. <settings>
  81. <typeAliases>
  82.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  83. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  84. </settings><mapper resource="mapper/EmployeeMapper.xml"/>
  85.     </mappers>
  86. </configuration>
复制代码
③“day02_mybatis.src.test.java.TestMybatis”中的代码
  1. import com.hh.mybatis.mapper.EmployeeMapper;
  2. import com.hh.mybatis.pojo.Employee;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. public class TestMybatis {
  11.     @Test
  12.     public void TestMybatis(){
  13. <settings>
  14. <typeAliases>
  15.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  16. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  17. </settings>try {
  18. <settings>
  19. <typeAliases>
  20.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  21. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  22. </settings>    String resource = "mybatis-config.xml";
  23. <settings>
  24. <typeAliases>
  25.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  26. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  27. </settings>    InputStream inputStream = Resources.getResourceAsStream(resource);
  28. <settings>
  29. <typeAliases>
  30.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  31. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  32. </settings>    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  33. <settings>
  34. <typeAliases>
  35.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  36. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings>    //通过sqlSessionFactory获取sqlSession对象
  38. <settings>
  39. <typeAliases>
  40.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  41. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  42. </settings>    SqlSession sqlSession = sqlSessionFactory.openSession();
  43. <settings>
  44. <typeAliases>
  45.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  46. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  47. </settings>    //获取EmployeeMapper的代理对象
  48. <settings>
  49. <typeAliases>
  50.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  51. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  52. </settings>    EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
  53. <settings>
  54. <typeAliases>
  55.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  56. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  57. </settings>    System.out.println("employeeMapper.getClass().getName() = " + employeeMapper.getClass().getName());
  58. <settings>
  59. <typeAliases>
  60.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  61. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  62. </settings>    Employee employee = employeeMapper.selectEmployee(1);
  63. <settings>
  64. <typeAliases>
  65.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  66. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  67. </settings>    System.out.println("employee = " + employee);
  68. <settings>
  69. <typeAliases>
  70.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  71. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  72. </settings>} catch (IOException e) {
  73. <settings>
  74. <typeAliases>
  75.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  76. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  77. </settings>    e.printStackTrace();
  78. <settings>
  79. <typeAliases>
  80.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  81. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  82. </settings>}
  83.     }
  84. }
复制代码
④测试代码,代码OK
(3)settings(设置)
①作用:这是MyBatis中极为重要的调整设置,它们会改变MyBatis的运行行为
②例如:mapUnderscoreToCamelCase属性:表示是否开启驼峰命名自动映射,默认值是false,如设置true会自动将字段last_name与lastName属性自动映射。
③在“day02_mybatis.src.main.resources”下的“mybatis-config.xml”中的“”设置
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings>
复制代码
(4)typeAliases(类型别名)
①作用:类型别名可为java类型设置一个缩写名字。
②xml代码:
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings>
复制代码
其中的xml代码也可以简写为
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings>
复制代码
③之后,可以将“day02_mybatis.src.mian.resources.mapper”下的“EmployeeMapper.xml”中的“resultType”设置为“employee”,如下:
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings>    SELECT<settings>
  6. <typeAliases>
  7.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  8. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  9. </settings>    id,<settings>
  10. <typeAliases>
  11.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  12. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  13. </settings>    last_name,<settings>
  14. <typeAliases>
  15.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  16. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  17. </settings>    email,<settings>
  18. <typeAliases>
  19.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  20. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  21. </settings>    salary<settings>
  22. <typeAliases>
  23.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  24. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  25. </settings>FROM<settings>
  26. <typeAliases>
  27.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  28. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings>    tbl_employee<settings>
  30. <typeAliases>
  31.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  32. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings>WHERE<settings>
  34. <typeAliases>
  35.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  36. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings>    id=#{empId}   
复制代码
(5)typeHandlers(类型处理器)
(6)objectFactory(对象工厂)
(7)plugins(插件)
(8)environments(环境配置)
①作用:设置数据库的连接环境
②实例xml代码
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings><settings>
  6. <typeAliases>
  7.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  8. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  9. </settings><settings>
  10. <typeAliases>
  11.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  12. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  13. </settings><settings>
  14. <typeAliases>
  15.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  16. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  17. </settings><settings>
  18. <typeAliases>
  19.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  20. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  21. </settings><settings>
  22. <typeAliases>
  23.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  24. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  25. </settings><settings>
  26. <typeAliases>
  27.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  28. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  29. </settings><settings>
  30. <typeAliases>
  31.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  32. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  33. </settings><settings>
  34. <typeAliases>
  35.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  36. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  37. </settings><settings>
  38. <typeAliases>
  39.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  40. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  41. </settings><settings>
  42. <typeAliases>
  43.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  44. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  45. </settings><settings>
  46. <typeAliases>
  47.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  48. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  49. </settings><settings>
  50. <typeAliases>
  51.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  52. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  53. </settings><settings>
  54. <typeAliases>
  55.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  56. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  57. </settings><settings>
  58. <typeAliases>
  59.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  60. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  61. </settings>  
复制代码
(9)databaseldProvider(数据库厂商标识)
(10)mappers(映射器)
 ①作用:设置映射文件路径
②xml代码:
例如:
  1. <settings>
  2. <typeAliases>
  3.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  4. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  5. </settings><settings>
  6. <typeAliases>
  7.         <typeAlias type="com.hh.mybatis.pojo.Employee" alias="employee"></typeAlias>
  8. </typeAliases><setting name="mapUnderscoreToCamelCase" value="true"/>
  9. </settings>
复制代码
3、注意事项
核心配置中的子标签,是有顺序要求的。顺序如下:

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表