【Mybatis学习路线】day03配置文件的解析及优化

一:配置文件的解析

1.mybatis.config.xml解析

<?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>
    <!-- environments指mybatis可以配置多个环境,default指向默认的环境,
          每个SqlSessionFactory对应一个环境environment -->
          <properties resource="db.properties">
          </properties>
          <typeAliases>
             <!-- 为指定类型指定别名使得在mapper映射文件中可以简化引用 
             <typeAlias type="net.xyz.entiy.User"  alias="User"/>
             -->
             <!--为某个包下所有类指定别名,默认别名是对应的类名  -->
             <package name="net.xyz.entiy"/>
          </typeAliases>
      <environments default="development">    
	  <environment id="development">
	  <!-- JDBC这个配置直接使用JDBC的提交和回滚功能。它依赖于从数据源中获得链接来管理事务的生命周期
	     -MANAGED这个配置基本上什么都不做。它从不提交或者回滚一个连接的事务。而是让容器(例如:spring或者J2EE应用服务器)来管理事务的生命周期 -->      
		  <transactionManager type="JDBC"/> 
		  <!--  
		           数据源类型:
		           UNPOOLED -这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。耗费时间
		           POOLED -这个数据源的实现缓存了JDBC连接对象,用于避免每次创建新的数据库连接时都初始化
		                              和进行认证,加快程序响应。并发web应用通常通过这种做法来快速响应。
		           
		  -->     
			  <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>  
  <mappers>    
    <!-- 定义映射SQL语句的文件 -->
  	<mapper resource="net/xyz/entiy/user.mapper.xml"/>  
  </mappers> 
</configuration>

2.mapper文件解析

<?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"> 
<!--  namespace命名空间,防止sql语句的id重名
      namespace命名包名+类名/包名+mapper文件名
      parameterType指sql语句参数类型
      resultType指返回结果类型
      useGeneratedKeys="true" 使用自增主键
      -->
<mapper namespace="net.xyz.entiy.UserMapper">
    <!--查询单个用户
    id在该文件中唯一  --> 
	<select id="selectUser" resultType="User">    
	  select * from user where id = #{id}  
	</select> 
	<insert id="addUser" parameterType="net.xyz.entiy.User"  useGeneratedKeys="true">
	   insert into user(username,userage) values(#{username},#{userage})
	</insert>
	<update id="updateUser" parameterType="net.xyz.entiy.User" >
	   update user set username = #{username},userage=#{userage} where id=#{id}
	</update>
	<delete id="deleteUser" parameterType="net.xyz.entiy.User">
	    delete from user where id=#{id}
	</delete>
	<select id="selectAllUser" resultType="User">
	   select * from user
	</select>
</mapper>

3.mybatisUtil

public class MybatisUtil {
	/**
	 * 
	 * 通过配置文件创建SqlSessionFactory是一个SqlSession的工厂类
	 */
    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
    	String resource = "mybatis.config..xml"; 
    	InputStream inputStream = Resources.getResourceAsStream(resource); 
    	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    	return sqlSessionFactory;
    }
    /**
     * SqlSession通过id找到对应的sql语句,执行sql语句
     * @return
     * @throws IOException
     */
    public static SqlSession getSession() throws IOException {
    	SqlSessionFactory sqlSessionFactory =getSqlSessionFactory();
    	SqlSession sqlSession =sqlSessionFactory.openSession();
    	return sqlSession;
    }
}

4.mybatis的执行流程
读取核心配置文件—>sqlSessionFactory---->sqlSession---->(执行相关操作)

二:配置文件的优化

1.在src下导入properties配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
username=root
password=root

config.xml中引入properties文件

 <properties resource="db.properties">

配置文件参数的替换

<transactionManager type="JDBC"/> 
		  <!--  
		           数据源类型:
		           UNPOOLED -这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。耗费时间
		           POOLED -这个数据源的实现缓存了JDBC连接对象,用于避免每次创建新的数据库连接时都初始化
		                              和进行认证,加快程序响应。并发web应用通常通过这种做法来快速响应。
		           
		  -->     
			  <dataSource type="POOLED">        
			  <property name="driver" value="${driver}"/>    
			  <property name="url" value="${url}"/>        
			  <property name="username" value="${username}"/>        
			  <property name="password" value="${password}"/>      
		  </dataSource>  

好处:便于对各种参数的修改

2.为指定类型指定别名
在configuration中加入<typeAliases>属性
既可以为指定类型指定别名,也可以为某个包下所有类指定别名

  <typeAliases>
             <!-- 为指定类型指定别名使得在mapper映射文件中可以简化引用 
             <typeAlias type="net.xyz.entiy.User"  alias="User"/>
             -->
             <!--为某个包下所有类指定别名,默认别名是对应的类名  -->
             <package name="net.xyz.entiy"/>
          </typeAliases>

猜你喜欢

转载自blog.csdn.net/qq_44867340/article/details/107855599