Mybatis的核心配置

学习目标

 

  • 了解Mybatis核心对象的作用
  • 熟悉Mybatis配置文件中各个元素的作用
  • 掌握Mybatis映射文件中常用元素的使用

     

文章目录

 

1.Mybatis的核心对象

1.1 SqlSessionFactory

1.2 SqlSession

1.2.1 使用工具类创建SqlSession

 2. 配置文件

2.1 主要元素

3. 映射文件

2.1主要元素


1.Mybatis的核心对象

1.1 SqlSessionFactory

SqlSessionFactory是Mybatis框架中十分重要的对象,他是单个数据库映射关系经过编译后的内存镜像,其主要对象是创建SqlSession

SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来构建,而SqlSessionFactoryBuilder则可以通过XML配置文件或一个预先定义好的Configuration实例构建出SqlSessionFactory的实例

// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);

// 2.通过配置文件构建 SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactory对象是线程安全的,它一旦被创建,整个应用执行期间都会存在。如果我们多次地创建同一个数据库的SqlSessionFactory,那么此数据库的资源很容易被耗尽。为了解决此问题,通常每一个数据库都会只对应一个SqlSessionFactory,所以在构建SqlSessionFactory实例时,建议使用单列模式。

1.2 SqlSession

SqlSession是Mybatis框架中另一个十分重要的对象,它是应用程序与持久层之间执行交互操作的一个单线程对象,其主要作用是执行持久化操作。SqlSession对象包含了数据库中执行SQL操作的方法,由于其底层封装了JDBC连接,所以可以直接使用其实例来执行已映射SQL语句。

1.2.1 使用工具类创建SqlSession

package com.mybatis.util;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/*
 * 工具类
 */
public class MybatisUtils {
	private static SqlSessionFactory sqlSessionFactory;

	// 初始化SqlSessionFactory对象
	static {
		try {
			// 使用Mybatis提供的Resources类加载Mybatis的配置文件
			Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
			// 构建SqlSessionFactory工厂
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 获取SqlSeeion对象的静态方法
	public static SqlSession getSession() {
		return sqlSessionFactory.openSession();
	}
}

 2. 配置文件

2.1 主要元素

<configuration>根元素<properties>
<settings>
<typeAliases>
<typeHandlers>
<objectFactory>
<plugins>
<environments>
<databaseIdProvider>
<mappers>

注:<configuration>的子元素必须按照表中由上到下的顺序进行配置,否则Mybatis在解析XML配置文件的时候会报错。

下面介绍几个元素的使用方法:

 1.<properties>元素

(1)在项目的src目录下,添加一个全名为db.properties的配置文件,编辑后的代码如下所示:

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

(2)在mybatis配置文件mybatis-config.xml中配置<properties.../>元素,具体如下:

	<properties resource="db.properties" />

(3)修改配置文件中数据库的连接的信息,具体如下:

	<dataSource type="POOLED">
				<!-- 数据库驱动 -->
				<property name="driver" value="${jdbc.driver}" />
				<!-- 连接数据库的url -->
				<property name="url" value="${jdbc.url}" />
				<!-- 连接数据库的用户名 -->
				<property name="username" value="${jdbc.username}" />
				<!-- 连接数据库的密码 -->
				<property name="password" value="${jdbc.password}" />
			</dataSource>

 2. <typeAliases>元素

<typeAliases>元素用于为配置文件的Java类型配置一个简短的名字,即设置别名。别名的设置与XML配置相关,其使用的意义在于减少全限定类名的冗余。

使用 <typeAliases>元素配置别名的方法如下:

<!-- 定义别名 -->
<typeAliases>
		<typeAlias alias="customer" type="com.mybatis.po.Customer" />
	</typeAliases>

3. 映射文件

3.1主要元素

<mapper><select> 映射查询语句,可自定义参数,返回结果集等
<insert> 映射插入语句,执行后返回一个整数,代表插入的条数
<update> 映射更新语句,执行后返回一个整数,代表更新的条数
<delete> 映射删除语句,执行后返回一个整数,代表删除的条数
<sql> 用于定义一部分SQL,然后可被其他语句引用此SQL
<cache> 给命名空间的缓存配置
<cache-ref> 其他命名空间缓存配置的引用
<resultMap> 用于描述如何从数据库结果集中来加载对象

下面是主要元素的使用方式:

<!-- 根据客户名编号查询客户信息列表 -->
<mapper namespace="com.nynu.qdy.mapper.CustomerMapper">
	<select id="findCustomerById" parameterType="Integer"
		resultType="com.nynu.qdy.po.Customer">
		select * from user where id = #{id}
	</select>

	<!-- 根据客户名模糊查询客户信息列表 -->
	<select id="findCustomerByName" parameterType="String"
		resultType="com.nynu.qdy.po.Customer">
		select * from user where username like '%${value}%'
	</select>
	<!-- 添加用户信息 -->
	<insert id="addCustomer"
		parameterType="com.nynu.qdy.po.Customer">
		insert into user
		(id,username,jobs)
		values
		(#{id},#{username},#{jobs})
	</insert>
	<!-- 更新用户信息 -->
	<update id="updateCustomer"
		parameterType="com.nynu.qdy.po.Customer">
		update user set
		username=#{username},jobs=#{jobs} where
		id=#{id}
	</update>
	<!-- 删除客户信息 -->
	<delete id="deletetCustomer" parameterType="Integer">
		delete from user
		where id=#{id}
	</delete>
	
<!-- resultMap的元素结构 -->
	<resultMap type="" id="">
		<constructor>	<!-- 类在实例化时,用于注入结果到构造方法中 -->
			<idArg />	<!-- ID参数;标记结果作为ID -->
			<arg />		<!-- 注入到构造方法中的一个普通结果 -->
		</constructor>
		<id />			<!-- 用于表示哪个键是主键 -->
		<result />		<!-- 注入到字段或JavaBean属性的普通结果 -->
		<association property=""></association>			<!-- 用于一对一关联 -->
		<collection property=""></collection>			<!-- 用于一对多关联 -->
		<discriminator javaType="">						<!-- 使用结果集来决定使用哪个结果映射 -->
			<case value=""></case>						<!-- 某些值的结构映射 -->
		</discriminator>
	</resultMap>

</mapper>	

 其中resultMap的映射文件为:

<?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.nynu.qdy.mapper.UserMapper">

	<resultMap type="com.nynu.qdy.po.User" id="resultMap">
		<id property="id" column="t_id" />
		<result property="name" column="t_name" />
		<result property="age" column="t_age" />
	</resultMap>

	<select id="findAllUser" resultMap="resultMap">
		select * from t_user
	</select>

</mapper>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平卉陌路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值