MyBatis--------配置文件详解

一、MyBatis配置xml文件解析:mybatis-config.xml


1.前提引入数据库配置文件:jdbc.properties

driver = com.mysql.jdbc.Driver
url= jdbc:mysql://localhost:3306/mybatis
username = root
password = 123456

2.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><!--配置-->

	<properties resource="jdbc.properties"><!-- 1.属性:引入properties配置文件 -->
		<!-- <property name="driver" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
		<property name="username" value="root"/>
		<property name="password" value="123456"/> -->
	</properties>
	
	<settings><!-- 2.设置  -->
		<setting name="useGeneratedKeys" value="false"/><!-- 是否允许JDBC自动生成主键 -->
		<setting name="autoMappingBehavior" value="PARTIAL"/><!-- 指定MyBatis的自动映射类型 -->
		<setting name="mapUnderscoreToCamelCase" value="true"/><!-- 是否开启自动驼峰命名规则,即从经典数据库列名字A_COLUMN到java对象属性名aColumn的映射 -->
		<!-- 注意:只有当设置为true且满足驼峰命名规则时,才可以实现从数据库到POJO对象的自动映射! -->
		<!-- 还有其他设置就不一一列举了,等用到了再回来添加 -->
	</settings>
	
	<typeAliases><!-- 3.类型别名 -->
		<!-- 设置student代替com.cyn.po.Student类,上下文均可使用,注意别名不区分大小写! -->
		<typeAlias alias="student" type="com.cyn.po.Student" />
	</typeAliases>
		<!-- 在java传递到数据库的参数和从数据库读出的数据中间起类型转换作用 -->
	<typeHandlers><!-- 4.类型处理器  -->
		
	</typeHandlers>
	
	<!-- <objectFactory type="">5.对象工厂 
	</objectFactory>-->
	
	<!-- <plugins/>6.插件  -->
	
	<environments default="development"><!-- 7.配置环境 -->
		<environment id="development"><!-- 8.环境变量 -->
			<transactionManager type="JDBC"><!-- 9.事务管理 -->
				<property name="autoCommit" value="false"/><!-- 设置为手动提交 -->
			</transactionManager>
			<dataSource type="POOLED"><!-- 10.数据源 -->
				<!-- 第一种:自定义配置数据库连接信息 -->
				<!-- <property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
				<property name="username" value="root"/>
				<property name="password" value="123456"/> -->
				
				<!-- 第二种:引入properties文件配置数据库连接信息或者上下文引用定义,推荐这种方法 -->
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- <databaseIdProvider type="">11.数据库厂商标识 
		MyBatis可能会运行在不同厂商的数据库中,它的作用在于指定SQL
		到对应的数据库厂商提供的数据库中运行! 
	</databaseIdProvider> -->
	
	<mappers><!-- 12.映射器 -->
		<!-- 第一种方法:用文件路径引入映射器,默认采取这一种方法 -->
		<mapper resource="com/cyn/mapper/StudentMapper.xml"/>
		
		
		<!-- 第二种方法:用包名引入映射器 -->
		<!-- <package name="com.cyn.mapper"/> -->
		
		<!-- 第三种方法:用类注册映入映射器 -->
		<!-- <mapper class="com.cyn.mapper.StudentMapper"/> -->
	</mappers>
	
</configuration>

二、映射器配置xml文件解析:mapper.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="com.learn.chapter2.mapper.StudentMapper"><!-- 空间名在映射器代理中一定要与映射器接口类路径保持一致!-->
	
	 <select id="selectStudentBySname" parameterType="string" resultType="student">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 </select>
	 <!-- 
	 ·id标出了这条SQL,与映射器接口中的方法名要保持一致
	 ·parameterType定义参数类型,与映射器接口中对应方法参数类型保持一致
	 ·resultType定义返回值类型,与映射器接口中对应方法返回值类型保持一致
	 -->
	  
	 <!-- 
	 1.自动映射:数据库规范命名,POJO对象属性名符合驼峰式命名且在mybatis的配置文件中设置
	  <setting name="mapUnderscoreToCamelCase" value="true"/> 
	 -->
	  
	 <!-- 
	 2.自定义映射:大多数情况下,当不符合规范命名时候, 我们可以自定义实体类
	  属性和对应数据库表字段的映射关系,即可实现查询结果自动映射成POJO对象 
	  例如:
	 -->
	 <resultMap id="studentResultMap" type="com.learn.chapter2.pro.Student">
	 	<id property="Sno" column="S_NO"/>
	 	<result property="Sname" column="S_NAME"/>
	 	<result property="Ssex" column="S_SEX"/>
	 	<result property="Sage" column="S_AGE"/>
	 	<result property="Sdept" column="S_DEPT"/>
	 </resultMap>
	 
	 <!-- 
	 自定义映射resultMap标签的配置详解:
	 ·定义一个唯一标识(id)为studentResultMap的resultMap,用type属性去定义
	 这是对哪个实体类和返回值字段名进行的映射关联
	 ·通过id元素定义主键和对应实体类属性名的对应关系,通过result元素定义普通列字段名和对
	 应实体类属性名的对应关系
	 ·这样select查询语句就不再需要只用自动映射的规则,直接用resultMap属性指定返回类型
	studentResultMap即可,这样MyBatis就会使用我们自定义的映射规则!
	例如: 
	-->
	 <select id="selectStudentBySno" parameterType="int" resultMap="studentResultMap">
	 	select * from t_student where s_no = #{Sno}
	 </select>
	 
	<!-- 
	1.多个参数的传递之Map的使用(将多个参数保存在集合中)
	例如:
	-->
	 <select id="selectStudentBySnameAndSdept1" parameterType="map" resultType="student">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 	and s_dept like concat('%',#{Sdept},'%')
	 </select>
	 
	<!-- 
	2.多个参数的传递之注解方式的使用(将多个参数进行注解)
	例如:
	-->
	<select id="selectStudentBySnameAndSdept2" resultType="student">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 	and s_dept like concat('%',#{Sdept},'%')
	</select>
	
	<!--  
	3.多个参数的传递之JavaBean的使用(将多个参数与JavaBean关联)
	 例如:
	-->
	<select id="selectStudentByJaveBean" parameterType="student" resultType="student">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 	and s_dept like concat('%',#{Sdept},'%')
	</select>
	
	<!-- 
	1.存储结果集之Map的使用(不推荐)
	切记:返回结果类型标签是resultType
	例如: 
	-->
	<select id="selectStudentSaveByMap" parameterType="student" resultType="map">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 	and s_dept like concat('%',#{Sdept},'%')
	</select>
	
	<!-- 
	2.存储结果集之POJO的使用(推荐)
	切记:返回结果集类型标签是resultMap-仅自定义POJO映射下这一种情况才使用该类型返回标签!     
	例如: 
	-->
	<select id="selectStudentSavePOJO" parameterType="student" resultMap="studentResultMap">
	 	select * from t_student where s_name like concat('%',#{Sname},'%')
	 	and s_dept like concat('%',#{Sdept},'%')
	</select>
	
	<!-- 
	1.开启一级缓存(共享同一个SqlSession):
	·不需要配置
	·说明:默认情况下开启,同一个SqlSession对象在commit之前调用同一个Mapper
	方法,仅执行一次SQL。因为使用SqlSession第一次查询后,mybatis会将其
	SQL语句和对应的查询结果放在缓冲中,以后再对其查询的时候,如果没有声明需要刷
	新缓存或者缓存未超时的情况下,SqlSession都只会取出当前缓冲中的数据,而不
	会再次发送SQL 
	-->
	
	<!-- 
	2.开启二级缓存(共享同一个SqlSessionFactory):
	·需要配置:执行以下标签配置,同时POJO对象实现Serializable接口即可!
	·说明:同一个SqlSessionFactory对象创建的不同SqlSession共享同一个
	二级缓存!
	-->
	<cache/>
	 
</mapper>

猜你喜欢

转载自blog.csdn.net/qq_37230121/article/details/81811557