Mybatis入门学习笔记

一、mybatis简介

MyBatis 本是apache的一个开源项目iBatis,后迁移google code并且改名为MyBatis,后迁移Github。是一款优秀的持久层框架,支持定制化 SQL、存储过程、高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

二、使用方法,如何在eclipse中使用mybatis

  1. 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可。
<!-- 若使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中:-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>
</dependency>

1. 创建maven工程 与 该项目的目录结构

首先在eclipse中创建maven工程。File -> Maven Project -> next -> 选择对应的原型,输入Group Id、Artifact Id -> finish。(注:左边的是本项目的目录结构)
在这里插入图片描述

2. 导入响应jar包

在pom.xml中导入响应的jar包

<!-- MySQL驱动包 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.48</version>
</dependency>
<!-- mybatis驱动包 -->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.5.3</version>
</dependency>

3. 创建全局配置文件mybatis-comfig.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>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<!-- 配置数据库连接池用的 -->
			<dataSource type="POOLED">	<!-- POOLED : 使用数据库连接池。UNPOOLED:不使用数据库连接池。 -->
				<property name="driver" value="com.mysql.jdbc.Driver" />	<!-- 驱动 -->
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/数据库名称" />
				<property name="username" value="用户名" />
				<property name="password" value="密码" />
			</dataSource>
		</environment>
	</environments>
	<!-- 配置映射文件路径 -->
	<mappers>
		<mapper resource="mapper/PersonMapper.xml" />
	</mappers>
</configuration>

4. 在main包下创建实体类Person.java

public class Person {
	private int pid;
	private String pname;
	private int age;
	private String tel;
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + pid;
		result = prime * result + ((pname == null) ? 0 : pname.hashCode());
		result = prime * result + ((tel == null) ? 0 : tel.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (pid != other.pid)
			return false;
		if (pname == null) {
			if (other.pname != null)
				return false;
		} else if (!pname.equals(other.pname))
			return false;
		if (tel == null) {
			if (other.tel != null)
				return false;
		} else if (!tel.equals(other.tel))
			return false;
		return true;
	}
	public Person(int pid, String pname, int age, String tel) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.age = age;
		this.tel = tel;
	}
	public Person() {
		super();
	}
	@Override
	public String toString() {
		return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age + ", tel=" + tel + "]";
	}
}

5. 创建数据库test

create table person(
pid int primary key auto_increment,
pname varchar(100) not null,
age int not null,
tel varchar(15)
)ENGINE=InnoDB auto_increment=1001 default charset=utf8 collate=utf8_bin;

6. 创建映射文件 PersonMapper.xml

<?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="mapper.Person">	<!-- 命名空间 -->	<!-- 或com.hx.myBatis.demo.Person -->
	<insert id="add" parameterType="com.hx.myBatis.demo.Person">
		insert into person values(0,#{pname},#{age},#{tel})
	</insert>
	<select id="finds" resultType="com.hx.myBatis.demo.Person">
		select pid,pname,age,tel from person
	</select>	
	<select id="findByPid" resultType="com.hx.myBatis.demo.Person">
		select pid,pname,age,tel from person person where pid=#{_parameter}
	</select>
</mapper>

7. 创建测试类,读取配置、创建SqlSessionFactory对象、打开SqlSession会话

private SqlSessionFactory ssf;
	{
		try {
			// 读取全局配置文件
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			//创建SqlSessionFactory对象,即读取配置文件后,获取连接数据库信息,创建好连接池
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void testInsert() {
		//获取一个连接执行sql语句  ->  获取打开一个SqlSession会话对象
		SqlSession ss = ssf.openSession();
		//执行sql语句
		int result = ss.insert("mapper.Person.add", new Person(0,"钉钉",19,"15620200219"));
		if(result > 0){
			ss.commit();	//事务提交
			System.out.println("添加成功");
		}else{
			ss.rollback();	//事务回滚
			System.out.println("添加失败");
		}
		ss.close();
	}

三、mybatis配置文件简介

(一)全局配置文件

1. 别名配置

<!-- 别名 -->
<typeAliases>
	<!-- 给指定的类启用别名。 type:类路径,alias:别名-->
	<typeAlias type="com.hx.myBatis.demo.Person" alias="Person"/>
	<!-- 如果这个实体类非常多 , 一个个的指定过于麻烦,亦可采用按照包指定的方式,即这个包下所有类都启用别名,默认别名为类名-->
	<package name="com.hx.myBatis.demo"/>
</typeAliases>

别名配置好之后,在映射文件中的参数类型、结果类型等就可以直接使用alias中配置的值了。

(二)映射文件

1. xml中的命名空间

xml中的命名空间类似于Java中的包,用来解决命名冲突问题,同一个命名空间不可以出现相同id

2. id:唯一标识符

id:唯一标识符,同一命名空间不可出相同id,mybatis是通过<命名空间>,<id>值来获取要运行的sql的。

3. 参数类型parameterType何paramterMap

  1. parameterType:指定这个sql语句的参数类型,这个和封装的DBHelper中的List<Object>类似,因为封装的是有序集合,故我们通过索引下标来取值,这是一般是一个实体类对象或是一个map或是一个值。
  2. {_parameter}:如果参数是一个普通的值,不是map,就仅仅一个值可以通过{_parameter}注值。
  3. parameterMap:参数为集合值

4. 取值方式:#{}、${}

  1. #{对象的属性名或map的键}-采用预编译方式取值
  2. ${对象属性名或map的键名}-采用字符串拼接方式 ,故这种方式会有sql注入的风险
  3. mybatis参数取值
<delete id="delete" parameterType="int">
	delete from person where pid=#{0}<!-- #{0}取到传过来的参数中索引为0的,即第一个 -->
</delete>
<delete id="delete1" parameterType="int">
	delete from person where pid=#{_parameter}	<!-- _parameter 指传的parameter参数-->
</delete>
<delete id="delete2" parameterType="int">
	delete from person where pid=#{pid}	<!-- 直接写参数名 -->
</delete>

5. 返回值类型

  1. 因为insert、delete、update执行后只有一个整型的结果返回,故无需指定返回类型,默认即整型值,即更新语句执行后的影响行数
  2. resultType:一个对象,列名与属性名一致。也可以是int,map,list 等
  3. resultMap:可以处理列名与属性名不一致的情况
    type:表示该结果最终要返回的值。id:一般用来表示主键列。result:用来表示其他列。
    property:对应实体类中的属性名。column:对应查找结果集中的列的列名。
    autoMapping,当属性名与列名一致时,可以不指定,mybatis会自动注入。
    示例:
<resultMap type="Person" id="personObj">
	<id property="pid" column="empno" />
	<result property="pname" column="ename" />
	<result property="tel" column="tel" />
</resultMap>

<!-- 写法二:加上autoMapping,当属性名与列名一致时,可以不指定,mybatis会自动注入 -->
<resultMap type="Person" id="personObj" autoMapping="true">
	<id property="pid" column="empno" />
	<result property="pname" column="ename" />
</resultMap>

四、动态SQL

1. if、where 多条件查询

<select id="find2" parameterType="Emp" resultType="Emp">
	select empno,ename,deptno,tel from emp
	<where>
		<!-- 会自动检测关键字,若多了,就会自动去除,但少了,会报错 -->
		<if test="ename != null and ename!=''">
			and ename like concat('%',#{ename},'%')
		</if>
		<if test="deptno != null and ename != ''">
			and deptno = #{deptno}
		</if>
	</where>
</select>

2. foreach循环拼接条件

<!-- 若想查询,101,103,105号部门的所有员工信息,此时我们需要从集合中循环出来,用foreach -->
<select id="find5" parameterType="list" resultType="Emp">
	select empno,ename,deptno,tel from emp where deptno in
	<!-- collection:循环集合的类型,可以是array或list。
		open:循环开始时拼接的字符。
		close:循环结束时需要拼接的字符。
		item:迭代变量。
		separate:分隔符,即每项间用什么分割 -->
	<foreach collection="array" open="(" close=")" item="item" separator=",">
		#{item}
	</foreach>
</select>

3. choose、when、otherwise

<!-- 如果给定部门,则根据部门查询,若没有,则根据姓名查询choose when otherwise;类似与Java中的switch  when相当于case,otherwise相当于default -->
<select id="find4" parameterType="map" resultType="Emp">
	select empno,ename,deptno,tel from emp
	<where>
			<choose>
		<when test="deptno != null">
				deptno = #{deptno}
			</when>
			<otherwise>
				ename like concat('%',#{ename},'%')
			</otherwise>
		</choose>
	</where>
</select>

4. set、trim

<!-- set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。
set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号。 -->
<!-- 更新表中的ename、deptno -->
<update id="update" parameterType="Emp">
	update emp
	<set>
		<if test="ename != null and ename != '' ">
			ename = #{ename},
		</if>
		<if test="deptno != null">
			deptno = #{deptno}
		</if>
	</set>
	where empno = #{empno}
</update>
<!-- 两种写法作用相同,但个人觉得第一种较简单 -->
<update id="update1" parameterType="Emp">
	update emp
	<trim prefix="set" suffixOverrides=",">
		<if test="ename != null and ename != '' ">
			ename = #{ename},
		</if>
		<if test="deptno != null">
			deptno = #{deptno}
		</if>
	</trim>
	where empno = #{empno}
</update>

五、缓存

1. 一级缓存

mybatis默认开启一级缓存。一级缓存是session级别的缓存。只要session没有close,缓存有效。
即session不close,两次相同的查询操作只会发送一次sql语句。但如果session关闭,则会发送两次请求。

2. 二级缓存

二级缓存需要手动开启。二级缓存是SQLSessionFactory级别的。只要SQLSessionFactory不关闭,缓存有效!
即SQLSessionFactory不关闭,两次相同查询只会请求一次。关闭会重新请求。
二级缓存被缓存的对象必须实现序列化接口!

<!-- 开启二级缓存,这是SQLSessionFactory级别的。
此时被缓存的对象必须实现序列化接口。 -->
<mapper namespace="mapper.Dept">
	<cache/>
</mapper>

六、sql语句中的< >判断 &符号

  1. 使用实体符号进行转义
< &lt;
> &rt;
& &amp;
2. 使用 **<![CDATA[ < ]]>** 包括起来

附录:

1. 本文使用到的sql脚本

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `dept`
-- ----------------------------
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `deptno` int(11) NOT NULL AUTO_INCREMENT,
  `dname` varchar(100) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`deptno`),
  UNIQUE KEY `dname` (`dname`)
) ENGINE=InnoDB AUTO_INCREMENT=106 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO dept VALUES ('105', '人事部');
INSERT INTO dept VALUES ('104', '市场部');
INSERT INTO dept VALUES ('102', '技术部');
INSERT INTO dept VALUES ('101', '行政部');
INSERT INTO dept VALUES ('103', '财务部');

-- ----------------------------
-- Table structure for `emp`
-- ----------------------------
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
  `empno` int(11) NOT NULL AUTO_INCREMENT,
  `ename` varchar(100) COLLATE utf8_bin NOT NULL,
  `deptno` int(11) DEFAULT NULL,
  `tel` varchar(15) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `FK_emp_deptno` (`deptno`),
  CONSTRAINT `FK_emp_deptno` FOREIGN KEY (`deptno`) REFERENCES `dept` (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=114 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO emp VALUES ('101', '周天', '101', '15096092222');
INSERT INTO emp VALUES ('102', 'hhaha', '101', '15096092223');
INSERT INTO emp VALUES ('103', '弯弯', '101', '15096092224');
INSERT INTO emp VALUES ('104', '天帝', '102', '15096092225');

-- ----------------------------
-- Table structure for `person`
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `pid` int(11) NOT NULL AUTO_INCREMENT,
  `pname` varchar(100) COLLATE utf8_bin NOT NULL,
  `age` int(11) NOT NULL,
  `tel` varchar(15) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO person VALUES ('1', '张三', '19', '00-2893266');
INSERT INTO person VALUES ('2', '李四', '20', '20200307');
INSERT INTO person VALUES ('3', 'huathy', '22', '20200307');

2. 参考资料:

  1. mybatis官方文档地址:https://mybatis.org/mybatis-3/zh/index.html
  2. 推荐阅读:Mybatis教程-实战看这一篇就够了https://blog.csdn.net/hellozpc/article/details/80878563
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Huathy-雨落江南,浮生若梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值