mybatis
Wei Jieyang Lv4

简介

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis,是一个基于Java的持久层框架。

  • 持久层: 可以将业务数据存储到磁盘,具备长期存储能力,只要磁盘不损坏,在断电或者其他情况下,重新开启系统仍然可以读取到这些数据。
  • 优点: 可以使用巨大的磁盘空间存储相当量的数据,并且很廉价
  • 缺点:慢(相对于内存而言)

为什么使用 MyBatis ?

传统的 JDBC 中,我们除了需要自己提供 SQL 外,还必须操作 Connection、Statment、ResultSet,不仅如此,为了访问不同的表,不同字段的数据,我们需要些很多雷同模板化的代码,闲的繁琐又枯燥

而我们在使用了 MyBatis 之后,只需要提供 SQL 语句就好了,其余的诸如:建立连接、操作 Statment、ResultSet,处理 JDBC 相关异常等等都可以交给 MyBatis 去处理,我们的关注点于是可以就此集中在 SQL 语句上,关注在增删改查这些操作层面上。

并且 MyBatis 支持使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

使用

环境搭建

Mapper

Mapper.xml

模版

1
2
3
4
5
6
<?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.auction.system.mapper.AcAuctionMapper">
</mapper>
  • com.auction.system.mapper.AcAuctionMapper:接口文件 mapper.java 的路径
  • mapper.java 和 mapper.xml文件中的方法函数是一一对应的

设置:

Idea:New -> Edit File Templates 加上模版 MybatisMapper

截屏2021-07-02 上午10.06.26

属性

1
2
3
<select id="selectAcAuctionList" parameterType="AcAuction" resultMap="AcAuctionResult"> <!-- 属性填写 -->
sql ..
</select>

parameterType()

定义参数输入类型

https://www.cnblogs.com/lcngu/p/5470695.html

resultType()

定义sql输出类型

resultMap

定义sql输出的map的实体类和sql变量之间的映射关系

1
2
3
4
5
6
7
8
<resultMap type="AcBidHistory" id="AcBidHistoryResult">
<result property="bidHistoryId" column="bid_history_id" />
<result property="auctionId" column="auction_id" />
<result property="bidderId" column="bidder_id" />
<result property="ethAddress" column="eth_address" />
<result property="ethPrice" column="eth_pric" />
<result property="abbPrice" column="abb_price" />
</resultMap>
  • id:定义是哪一个 resultMap,在之后的sql中使用
  • type:类对象

sql

  1. #{} 占位符对应传入的参数(相当于sql中的 ),可以随意写字符串标识符,但最好还是和传入参数对应起来,保持良好的可读性
    • 适用于单个参数

mapper.java 接口:

1
public void deleteData(int id) throws Exception;

mapper.xml 文件:

1
2
3
<delete id="deleteData" parameterType="int">
delete from product_info where ProductID = #{id}
</delete>
  1. 使用param注解参数
    • 适用于多个参数

mapper.java 接口:

1
public ArrayList<Data> selectAll(@Param("DateString") String DateString, @Param("BranchNo") String BranchNo) throws Exception;

mapper.xml 文件:

1
2
3
<select id="selectAll" resultMap="DataMap">
select * from townbranch where datetime = #{DateString} and branchno like #{BranchNo};
</select>
  1. 使用JavaBean传递参数
    • 当参数过多时,封装到一个类中

mapper.java 接口:

1
public int insertData(ProductsBean product) throws Exception

Bean 文件:

  • 一个参数类,这里是类 ProductsBean

mapper.xml 文件:

1
2
3
4
<insert id="insertData" keyProperty="ProductID">
insert into Products (ProductID, ProductName, Price, ProductDescription)
values (#{ProductID},#{ProductName},#{Price},#{ProductDescription})
</insert>

#{}${} 都可以替代参数,即占位符

  • #{}**:在SQL语句运行的时候显示的是一个的 **?
  • #{} 在处理接受的字段会当做字符串处理,会把传入的参数自动加上引号””,
  • ${ }**:不会自动添加引号”” ,就是简单的字符串拼接,容易引发SQL语句注入的安全隐患,所以一般还是推荐使用** #{ }

注解

@transactional

事务注解,在方法前面加上本注解,方法中的sql语句将会按照事务模式自动执行

  • Post title:mybatis
  • Post author:Wei Jieyang
  • Create time:2021-07-01 12:16:42
  • Post link:https://jieyang-wei.github.io/2021/07/01/mybatis/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.