目录
需要的插件:MyBatisX,Lombok
点击 file — Settings — Plugins — 搜索 — install — Apply — OK
四、Mapper.xml映射文件
SQL 映射文件只有很少的几个顶级元素(按照应被定义的顺序列出):
cache
– 该命名空间的缓存配置。 ----缓存处理
cache-ref
– 引用其它命名空间的缓存配置。 -----缓存处理
resultMap
– 描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素。 ----关联关系映射
parameterMap
– 老式风格的参数映射。此元素已被废弃,并可能在将来被移除!
sql
– 可被其它语句引用的可重用语句块。 ----动态sql语句
insert
– 映射插入语句。
update
– 映射更新语句。
delete
– 映射删除语句。
select
– 映射查询语句。
4.1 创建Mapper.xml映射文件
先创建一个实体类 Dept,再创建接口 DeptMapper 和 DeptMapper.xml 映射文件
Dept 实体类:
使用注解自动添加构造器,get/set方法 以及 toString方法
package com.jr.pojo;
import lombok.*;
@AllArgsConstructor //自动添加 全参构造
@NoArgsConstructor //自动添加 无参构造
@Data //自动添加 get/set方法
@ToString
public class Dept {
private Integer deptno;
private String dname;
private String loc;
}
DeptMapper 接口:
package com.jr.mapper;
import com.jr.pojo.Dept;
import java.util.ArrayList;
public interface DeptMapper {
ArrayList<Dept> selectAll();
int insertDept(Dept record);
int updateDept(Dept record);
int deleteDeptById(Integer id);
}
DeptMapper.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="com.jr.mapper.DeptMapper"><!-- 设置二级缓存的范围(一般会定位到接口) -->
</mapper>
会有小鸟图标出现
4.2 编写DeptMapper.xml 映射文件
在接口:ArrayList<Dept> selectAll();
查询使用 <select> 标签
id 是方法名,resultType 是返回值类型,parameterType 是参数类型,标签里写 sql 语句
如果在核心配置文件里取别名了,这里的 resultType 和 parameterType 可以直接使用别名(参考上一篇 MyBatis框架(概念,环境搭建,核心配置文件部分)-CSDN博客 的3.3 typeAliases 类型别名)
<select id="selectAll" resultType="com.jr.pojo.Dept">
select * from dept
</select>
插入 使用 <insert> 标签
<insert id="insertDept" parameterType="com.jr.pojo.Dept">
insert into dept values(#{deptno},#{dname},#{loc})
</insert>
占位符号:#{ } ${ }
#{ } 是预编译处理,$ { } 是字符串替换。
mybatis在处理 #{ } 时,会将sql中的 #{ } 替换为 ? 号,调用 PreparedStatement 的 set 方法来赋值;
mybatis 在处理 $ { } 时,就是把 ${ } 替换成变量的值。使用 #{ } 可以有效的防止SQL注入,提高系统安全性。但是表名用参数传递进来的时候,只能使用 ${}
修改 使用 <update> 标签
<update id="updateDept" parameterType="com.jr.pojo.Dept">
update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
</update>
删除 使用 <delete> 标签
<delete id="deleteDeptById" parameterType="integer">
delete from dept where deptno=#{deptno}
</delete>
4.3 编写测试类
public class DeptTest {
SqlSessionFactory sqlSessionFactory=null;
@Before
public void before() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");//1.加载 mybatis 核心配置文件
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//2.获得连接对象:session 会话对象
}
@Test
public void test01() throws IOException {
//3.通过session对象.方法();
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Dept> list = sqlSession.selectList("selectAll");
for(Dept dept:list){
System.out.println(dept);
}
//4.关闭会话对象
sqlSession.close();
}
}
查询结果:
添加:
@Test
public void test02() throws IOException {
//通过session对象.方法
SqlSession sqlSession = sqlSessionFactory.openSession();
Dept dept = new Dept(55,"后勤部门","沈阳");
int i = sqlSession.insert("insertDept",dept);
if(i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
sqlSession.commit();
sqlSession.close();
}
注意:添加、删除、修改操作后 记得提交事务— sqlSession.commit(); — 后再关闭
本次新加的代码
Dept 实体类
DeptMapper 接口
DeptMapper.xml 映射文件
DeptTest 测试类
package com.jr.test;
import com.jr.pojo.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class DeptTest {
SqlSessionFactory sqlSessionFactory=null;
@Before
public void before() throws IOException {
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");//1.加载 mybatis 核心配置文件
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//2.获得连接对象:session 会话对象
}
@Test
public void test01() throws IOException {
//3.通过session对象.方法();
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Dept> list = sqlSession.selectList("selectAll");
for(Dept dept:list){
System.out.println(dept);
}
//4.关闭会话对象
sqlSession.close();
}
@Test
public void test02() throws IOException {
//通过session对象.方法
SqlSession sqlSession = sqlSessionFactory.openSession();
Dept dept = new Dept(55,"后勤部门","沈阳");
int i = sqlSession.insert("insertDept",dept);
if(i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void test03() throws IOException {
//通过session对象.方法
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.delete("deleteDeptById",66);
if(i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void test04() throws IOException {
//通过session对象.方法
SqlSession sqlSession = sqlSessionFactory.openSession();
Dept dept = new Dept(40,"后勤部门","沈阳");
int i = sqlSession.update("updateDept",dept);
if(i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
sqlSession.commit();
sqlSession.close();
}
}