MyBatis3核心配置

MyBatis依赖的JAR包

在这里插入图片描述

MyBatis默认使用log4j输出日志信息的,如果需要在控制台查看输出的SQL语句,需要在src目录下创建log4j.properties文件,将指定包下的所有类的日志级别设置为DEBUG。

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
<!--设置com.syl包下的类日志级别-->
log4j.logger.com.syl=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

MyBatis的核心对象

MyBatis框架主要有两个核心对象:SqlSessionFactory和SqlSession。

SqlSessionFactory

org.apache.ibatis.session.SqlSessionFactory
SqlSessionFactory主要作用就是用来创建SqlSession对象。通过如下方法创建SqlSessionFactory对象:

InputStream inputStream = Resources.getResourceAsStream("配置文件名");
SqlSessionFactory sqlSessionFactory = 
		new SqlSessionFactoryBuilder.build(inputStream);

SqlSession

org.apache.ibatis.session.SqlSession
SqlSession是应用程序与持久层之间执行交互操作的一个单线程对象,简单点就是使用JDBC执行数据库操作的对象。通过创建好的SqlSessionFactory对象创建SqlSession对象。

	SqlSession sqlSession = sqlSessionFactory.openSession();

MyBatis的配置文件

MyBatis的配置文件,包含了很多影响MyBatis行为的信息。通常设置为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="db.properties" />
	<!--设置表的别名-->
	<typeAliases>
		<!--自动扫描包下的PO类定义别名,将PO类首字母小写作为别名-->
		<package name="com.syl.po" />
	</typeAliases>
	<!--配置环境元素-->
	<environments default="mysql">
		<!--配置id为mysql的数据库环境-->
		<environment id="mysql">
			<!--使用JDBC事务管理器-->
			<transactionManager type="JDBC" />
			<!--数据库连接池-->
			<dataSource type="POOLED">
				<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="root" />
			</dataSource>
		</environment>
	</environments>
	<!--映射文件位置-->
	<Mappers>
		<mapper package="com.syl.mapper.Customer.xml" />
	</Mappers>

MyBatis的映射文件

MyBatis的映射文件,用于执行SQL语句,及其PO类属性名对数据库表字段的映射。通常设置为PO类名+Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper 
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace表示命名空间同
	通常设置为包名+映射文件主名
-->
<mapper namespace="com.syl.mapper.CustomerMapper">
	<!--resultMap用于返回的PO类型的属性
		和数据库表字段的对应
		id:唯一标识,后面的SQL语句返回值属性resultMap可指向
		id:主键
		result:字段名
	-->
	<resultMap type="com.syl.po.Customer" id="resultMap">
		<id property="id" column="t_id"/>
		<result property="name" column="t_name" />
		<result property="age" column="t_age" />
	</resultMap>
	<!--id:该SQL的唯一标识
		parameterType:接受的参数类型
		resultType:返回的类型
	-->
	<select id="findCustomerById"
		parameterType="Integer" 
		resultType="resultMap">
			select * from t_customer where id=#{id}
	</select>
	<update.......>
	</update>
	<insert.......>
	</insert>
	<delete.......>
	</delete>
</mapper>

MyBatis的动态SQL

动态SQL语句通常应用在映射文件中的SQL语句中,完成一些特殊的功能

< if>元素

判断语句

	<!--判断接受的参数的username和jobs是否为空
		test:判断语句
		test若为true则输出元素内语句
	-->
	<select id="findCustomerByNameAndJobs"
		parameterType="com.syl.po.Customer"
		resultType="com.syl.po.Customer">
			select * from t_customer where 1=1
	<if test="username != null and username=''">
		and username like cocat('%',#{username},'%')
	</if>
	<if test="jobs != null and jobs=''">
		and jobs = #{jobs}
	</if>
	</select>

< choose>元素

判断语句

	<!--判断接受的参数的username和jobs是否为空
		test:判断语句
		test若为true则输出元素内语句
		若所有<when>语句都为空,则输出<otherwise>元素内语句
	-->
	<select id="findCustomerByNameAndJobs"
		parameterType="com.syl.po.Customer"
		resultType="com.syl.po.Customer">
			select * from t_customer where 1=1
	<choose>
		<when test="username != null nad username != ''">
			and username like cocat('%',#{username},'%')
		</when>
		<when test="jobs != null nad jobs != ''">
			and jobs=#{jobs}
		</when>
		<otherwise>
			and phone is not null
		</otherwiese>
	</choose>
	</select>

< where>、< trim>元素

SQL中的where子句,使用此语句,就不需要在SQL语句中使用条件where子句。

	<!--判断接受的参数的username和jobs是否为空
		test:判断语句
		test若为true则输出元素内语句
	-->
	<select id="findCustomerByNameAndJobs"
		parameterType="com.syl.po.Customer"
		resultType="com.syl.po.Customer">
			select * from t_customer
	<where>
	<if test="username != null and username=''">
		and username like cocat('%',#{username},'%')
	</if>
	<if test="jobs != null and jobs=''">
		and jobs = #{jobs}
	</if>
	</where>
	</select>

< set>元素

update专属语句,MyBatis与Hibernate的不同之处,Hibernate使用update更新字段时,是需要发送全部字段的。MyBatis使用< set>元素,就可以指定更新某些字段。

	<!--set元素可以替换UPDATE语句中的set子句-->
	<update id="updateCustomer" 
			parameterType="com.syl.po.Customer">
		update t_customer
		<set>
			<if test="username != null and username=''">
				username=#{username},
			</if>
			<if test="jobs != null and jobs=''">
				jobs=#{jobs},
			</if>
			<if test="phone != null and phone=''">
				phone=#{phone},
			</if>
		</set>
	</update>
			

< foreach>元素

循环语句

	<!--
		item:循环中当前的元素
		index:元素在集合的下标
		collection:传递过来的集合类型,首字母小写
		open:以什么符号开头
		close:以什么符号结尾
		separator:间隔符
	-->
	<select id="findCustomerByIds 
			parameterType="List"
			resultType="com.syl.po.Customer">
				select * from t_customer where id in
		<foreach item="id" index="index" collection="list" open="(" separator="," close=")" >
			#{id}
		</foreach>
	</select>

@syl 2021/09/03 周五 22:30 晴 回宿舍吧,明天周末

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值