mybatis
2020-10-16 09:28:49 108 举报
AI智能生成
mybatis自学个人总结
作者其他创作
大纲/内容
mybatis简单使用步骤
创建一个maven工程
导入相关的依赖jar包
mybatis依赖
MySQL驱动依赖
junit依赖
编写实体类
编写接口
编写与接口相关的XML映射文件
编写mybatis核心配置文件,用于配置全局环境
测试
注解开发
映射的语句可以不用 XML 来配置,而可以使用 Java 注解来配置
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
需要在mybatis-config.xml配置文件中绑定Mapper接口:<mapper class="com.ruicai.mapper.UserMapper"/>
局限性:只能执行简单SQL语句,无法处理复杂的SQL语句
@Param
@Param注解的作用是给参数命名
参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中
#{}和${}
#{}是预编译处理
$ {}是字符串替换
#{}可以防止SQL注入
mybatis映射文件
namespace<br>
namespace="com.ruicai.mapper.UserMapper"
包名和接口的一致
resultMap<br>
结果集映射
描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
sql<br>
sql语句碎片
可以用来定义可重用的 SQL 代码片段,以便在其它语句中使用
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
insert<br>
映射插入语句
<span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"><insert</span><span class="pln" style="text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"> </span><span class="atn" style="text-align: left; color: rgb(102, 0, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">id</span><span class="pun" style="text-align: left; color: rgb(102, 102, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">=</span><span class="atv" style="text-align: left; color: rgb(0, 136, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">"insertAuthor"</span><span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">></span>
需要进行事务的提交
update<br>
映射修改语句
<span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"><update</span><span class="pln" style="text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"> </span><span class="atn" style="text-align: left; color: rgb(102, 0, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">id</span><span class="pun" style="text-align: left; color: rgb(102, 102, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">=</span><span class="atv" style="text-align: left; color: rgb(0, 136, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">"updateAuthor"</span><span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">></span>
需要进行事务的提交
delete
映射删除语句
<span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"><delete</span><span class="pln" style="text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;"> </span><span class="atn" style="text-align: left; color: rgb(102, 0, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">id</span><span class="pun" style="text-align: left; color: rgb(102, 102, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">=</span><span class="atv" style="text-align: left; color: rgb(0, 136, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">"deleteAuthor"</span><span class="tag" style="text-align: left; color: rgb(0, 0, 136); text-transform: none; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; word-spacing: 0px; white-space: pre-wrap; orphans: 2; -webkit-text-stroke-width: 0px;">></span>
需要进行事务的提交
select
映射查询语句
参数
id
引用的方法名
在命名空间中唯一的标识符,可以被用来引用这条语句
resultType
返回值的类型
期望从这条语句中返回结果的类全限定名或别名
parameterType<br>
传入的参数类型
将会传入这条语句的参数的类全限定名或别名
resultMap
对外部resultMap命名的引用<br>
<select id="selectPerson" parameterType="int" resultType="hashmap">
多对一的处理
多对一
子查询
先将表中的字段查询出来,<br><select id="selectAll" resultMap="stMap"><br> select * from student;<br> </select>
做结果集映射<br><resultMap id="stMap" type="Student"><br> <association property="teacher" column="tid" javaType="Teacher" select="selectTeacher"/><br> </resultMap>
再通过已查询出来的外键作为条件<br><select id="selectTeacher" resultType="Teacher"><br> select * from teacher where id = #{id};<br> </select>
联表查询
先做联表查询<br><select id="selectAll2" resultMap="stMap2"><br> select s.id sid,s.name sname,t.name tname from student as s,teacher as t where s.tid = t.id;<br> </select>
再做结果集映射<br><resultMap id="stMap2" type="Student"><br> <result property="id" column="sid"/><br> <result property="name" column="sname"/><br> <association property="teacher" javaType="Teacher"><br> <result property="name" column="tname"/><br> </association><br> </resultMap>
JDBC
描述
是java提供访问关系型数据库的接口
JDBC API为java语言提供易于使用的java语言与数据库交互的接口
作用:JDBC API可以执行SQL语句,检索SQL结果以及将数据更改写回底层的数据源
使用步骤
建立数据源连接
无连接池连接
加载数据库驱动:Class.forName("com.mysql.jdbc.Driver")
获取连接:Connection connection = DriverManager.getConnection(url,username,password);
使用数据库连接池连接<br>以mybatis为例
创建DataSource实例:DataSource dataSource = new UnpooledDataSource(driver,url,username,password)
使用mybatis的DataSourceFactory创建数据库连接
创建DataSourceFactory实例:DataSourceFactory dsf = new UnpooledDataSourceFactory();
加载配置文件:<br>1.创建配置文件对象:Properties p = new Properties();<br>2.获取流:InputStream input = Thread.crrentThread().getContextClassLoader().getResourceAsStream("dataSource.properties")<br>3.加载配置文件:<br>p.load(input);<br>dsf.getProperties(p);<br>4.获取数据源<br>DataSource dataSource = dsf.getDataSource();<br>5.获取数据库连接<br>Connection connection = dataSource.getConnection();<br>
执行SQL语句
Statement stat = connection.createStatement();
执行静态SQL语句。通常通过Statement实例实现
PreparedStatement pstmt = con.prepareStatement(sql) ;
执行动态SQL语句。通常通过PreparedStatement实例实现
CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;
执行数据库存储过程。通常通过CallableStatement实例实现
查询:executeQuery()
更新:executeUpdate();
查询结果
通过ResultSet获取结果集
关闭连接
connection.close();
概念
mybatis是支持自定义SQL,存储过程和高级映射的持久层框架<br>
mybatis是对JDBC API的轻量级的封装
持久层
持久层是完成数据持久化的代码
持久层:在系统逻辑上,一个专著于实现数据持久化的一个相对独立的领域,就是将数据保存到掉电式存储设备中
数据持久化:就是将程序中的数据瞬时状态转化为持久状态的过程
持久化:数据持久状态和瞬时状态转化的机制,就是将瞬时状态的数据转化为持久状态的数据
mybatis核心组件
SqlSessionFactoryBuilder(构造器)<br>
作用:创建SqlSessionFactory实例
生命周期:不能长期存在,当SqlSessionFactory实例化了,SqlSessionFactoryBuilder就没有存在的意义了<br>
作用域:存在于方法作用域中(局部方法变量)
SqlSessionFactory(工厂接口)<br>
作用:可以认为是数据库连接池,用于获取Sqlsession实例
生命周期:存在程序运行期间,保证数据库连接不会中断
作用域:存在于应用作用域
SqlSession(会议接口)
作用:相当于数据库连接对象,用于执行SQL语句,事务的提交,回滚等
生命周期:一个请求或业务执行期间,当执行完毕后,该连接就会被回收
作用域:方法域中
SQLMapper(SQL映射)<br>
mybatis核心配置文件
properties(属性)
属性可以在外部进行配置,并可以进行动态替换
可以在 properties 元素的子元素中设置
可以在典型的 Java 属性文件中配置这些属性
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
settings(设置)
MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为
日志(logImpl)
开启日志
<setting name="logImpl" value="STDOUT_LOGGING"/>
STDOUT_LOGGING:标准日志输出
LOG4J使用
<setting name="logImpl" value="LOG4J"/>
导入maven依赖
在classpath路径下创建log4j.properties文件
typeAliases(类型别名)
<typeAlias alias="Author" type="domain.blog.Author"/>
类型别名可为 Java 类型设置一个缩写名字
environments(环境配置)
可以配置多套环境,但SqlSessionFactory实例只使用一种环境
environment(环境变量)
transactionManager(事务管理器)
JDBC – 这个配置直接使用了 JDBC 的提交和回滚设施,它依赖从数据源获得的连接来管理事务作用域
MANAGED – 这个配置几乎没做什么
dataSource(数据源)
dataSource(数据源)种类
三种数据源
type="[UNPOOLED|POOLED|JNDI]"
JNDI – 这个数据源实现是为了能在如 EJB 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的数据源引用
POOLED– 这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来,避免了创建新的连接实例时所必需的初始化和认证时间
UNPOOLED– 这个数据源的实现会每次请求时打开和关闭连接
属性
driver – 这是 JDBC 驱动的 Java 类全限定名
url – 这是数据库的 JDBC URL 地址
username – 登录数据库的用户名
password – 登录数据库的密码
defaultTransactionIsolationLevel – 默认的连接事务隔离级别
defaultNetworkTimeout – 等待数据库操作完成的默认网络超时时间
mappers(映射器)
注册绑定Mapper文件
使用相对于类路径的资源引用
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
使用映射器接口实现类的完全限定类名<br>
<mapper class="org.mybatis.builder.AuthorMapper"/>
注意:
接口和Mapper配置文件必须同名
Mapper配置文件和接口必须在同一个包下
将包内的映射器接口实现全部注册为映射器
<package name="org.mybatis.builder"/>
执行流程
缓存
概念:
存储在内存的临时数据
将查询的数据放到内存中,可以提供数据查询效率,解决了高并发系统的性能问题<br>
使用原则:经常查询且不经常改变的数据
分类
一级缓存
本地缓存(SqlSession级别):一次会话期间查询到数据会存放在本地缓存中<br>
默认开启一级缓存
缓存失效:
增删改语句会刷新缓存
手动清理缓存
二级缓存
全局缓存
开启缓存:在SQL映射文件中添加<cache><br>
工作机制:会话关闭后,将一级缓存的数据添加到二级缓存中,当新的会话开启后,会将二级缓存的数据添加一级缓存中
缓存顺序
先查看二级缓存
如果二级缓存没有,再查看一级缓存
一级缓存没有,再查询数据库
0 条评论
下一页