公司建设网站价格多少钱,圣诞节网页设计模板图片,视频号怎么运营,开发个微网站多少钱学习目标 了解Mybatis核心对象的作用熟悉Mybatis配置文件中各个元素的作用掌握Mybatis映射文件中常用元素的使用 文章目录 1.Mybatis的核心对象
1.1 SqlSessionFactory
1.2 SqlSession
1.2.1 使用工具类创建SqlSession 2. 配置文件
2.1 主要元素
3. 映射文件
2.1主要元素…学习目标 了解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.通过配置文件构建 SqlSessionFactorySqlSessionFactory 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根元素propertiessettingstypeAliasestypeHandlersobjectFactorypluginsenvironmentsdatabaseIdProvidermappers
注configuration的子元素必须按照表中由上到下的顺序进行配置否则Mybatis在解析XML配置文件的时候会报错。 下面介绍几个元素的使用方法 1.properties元素
1在项目的src目录下添加一个全名为db.properties的配置文件,编辑后的代码如下所示
jdbc.drivercom.mysql.cj.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/mybatis
jdbc.usernameroot
jdbc.password123456
2在mybatis配置文件mybatis-config.xml中配置properties.../元素具体如下 properties resourcedb.properties /
3修改配置文件中数据库的连接的信息具体如下 dataSource typePOOLED!-- 数据库驱动 --property namedriver value${jdbc.driver} /!-- 连接数据库的url --property nameurl value${jdbc.url} /!-- 连接数据库的用户名 --property nameusername value${jdbc.username} /!-- 连接数据库的密码 --property namepassword value${jdbc.password} //dataSource 2. typeAliases元素
typeAliases元素用于为配置文件的Java类型配置一个简短的名字即设置别名。别名的设置与XML配置相关其使用的意义在于减少全限定类名的冗余。
使用 typeAliases元素配置别名的方法如下
!-- 定义别名 --
typeAliasestypeAlias aliascustomer typecom.mybatis.po.Customer //typeAliases
3. 映射文件
3.1主要元素
mapperselect 映射查询语句可自定义参数返回结果集等insert 映射插入语句执行后返回一个整数代表插入的条数update 映射更新语句执行后返回一个整数代表更新的条数delete 映射删除语句执行后返回一个整数代表删除的条数sql 用于定义一部分SQL然后可被其他语句引用此SQLcache 给命名空间的缓存配置cache-ref 其他命名空间缓存配置的引用resultMap 用于描述如何从数据库结果集中来加载对象下面是主要元素的使用方式 !-- 根据客户名编号查询客户信息列表 --
mapper namespacecom.nynu.qdy.mapper.CustomerMapperselect idfindCustomerById parameterTypeIntegerresultTypecom.nynu.qdy.po.Customerselect * from user where id #{id}/select!-- 根据客户名模糊查询客户信息列表 --select idfindCustomerByName parameterTypeStringresultTypecom.nynu.qdy.po.Customerselect * from user where username like %${value}%/select!-- 添加用户信息 --insert idaddCustomerparameterTypecom.nynu.qdy.po.Customerinsert into user(id,username,jobs)values(#{id},#{username},#{jobs})/insert!-- 更新用户信息 --update idupdateCustomerparameterTypecom.nynu.qdy.po.Customerupdate user setusername#{username},jobs#{jobs} whereid#{id}/update!-- 删除客户信息 --delete iddeletetCustomer parameterTypeIntegerdelete from userwhere id#{id}/delete!-- resultMap的元素结构 --resultMap type idconstructor !-- 类在实例化时用于注入结果到构造方法中 --idArg / !-- ID参数标记结果作为ID --arg / !-- 注入到构造方法中的一个普通结果 --/constructorid / !-- 用于表示哪个键是主键 --result / !-- 注入到字段或JavaBean属性的普通结果 --association property/association !-- 用于一对一关联 --collection property/collection !-- 用于一对多关联 --discriminator javaType !-- 使用结果集来决定使用哪个结果映射 --case value/case !-- 某些值的结构映射 --/discriminator/resultMap/mapper 其中resultMap的映射文件为
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.nynu.qdy.mapper.UserMapperresultMap typecom.nynu.qdy.po.User idresultMapid propertyid columnt_id /result propertyname columnt_name /result propertyage columnt_age //resultMapselect idfindAllUser resultMapresultMapselect * from t_user/select/mapper