Spring Boot学习篇(三)之通用mapper的使用(oracle版)
1 配置pom.xml
<parent>
<artifactId>spring-boot-starter-parentartifactId>
<groupId>org.springframework.bootgroupId>
<version>2.7.2version>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.1.5version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.4.3version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.1version>
dependency>
<dependency>
<groupId>com.oracle.database.jdbcgroupId>
<artifactId>ojdbc6artifactId>
<version>11.2.0.4version>
dependency>
dependencies>
2.在resources文件夹下面创建application.yml文件
2.1 目录结构如下所示
2.2 在application.yml文件进行相应配置(数据源、mybatis、pageHelper)
#①配置数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource #连接池类型
driver-class-name: oracle.jdbc.driver.OracleDriver #驱动类
url: jdbc:oracle:thin:@localhost:1521:orcl #地址
username: 'scott'
password: '123'
#②mybatis的相关配置
mybatis:
type-aliases-package: com.zlz.entity #实体别名设置(写自定义sql时resultType里面就可以不用写全类名了)
mapper-locations: /mappers/*.xml #mapper.xml文件扫描
#③配置分页插件 pagehelper
pagehelper:
helper-dialect: oracle #数据库方言,选择的是哪种数据库,就采用哪种分页方法
reasonable: true
#④ 输出sql语句日志
logging:
level:
com.zlz.mapper: debug
3 在com包.zlz包下面创建Spring boot项目启动类OracleStart1
package com.zlz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.zlz.mapper")
public class OracleStart1 {
public static void main(String[] args) {
SpringApplication.run(OracleStart1.class);
}
}
4.在com包.zlz包.entity包下面创建实体类Emp
4.1 准备sq_emp序列,用于Emp表的自增
create sequence sq_emp
start with 8000;
4.2 实体类Emp类的内容如下所示
package com.zlz.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.KeySql;
import tk.mybatis.mapper.code.ORDER;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {
@Id
@KeySql(order = ORDER.BEFORE,sql = "select sq_emp.nextval from dual")
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
5 在zlz包下创建mapper包,并在mapper包下面创建EmpMapper接口
5.1 目录结构如下所示
5.2 Emp接口内容
package com.zlz.mapper;
import com.zlz.entity.Emp;
import tk.mybatis.mapper.common.Mapper;
public interface EmpMapper extends Mapper<Emp> {
}
6 配置好测试类的环境
import com.zlz.OracleStart1;
import com.zlz.mapper.EmpMapper;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest(classes = OracleStart1.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class Test01 {
@Resource
EmpMapper empMapper;
}
7.增删改查
7.1 查询
7.1.1 查全部
a 代码
@Test
public void findAll(){
empMapper.selectAll().forEach(System.out::println);
}
b 运行截图
7.2.1 根据主键查单条
a 代码
@Test
public void findoneByPrimaryKey(){
System.out.println(empMapper.selectByPrimaryKey(7369));
}
b 运行截图
7.3.1 查单条(验证)
a 代码
@Test
public void findOneisExists(){
Emp emp = new Emp();
emp.setEmpno(7499);
System.out.println(empMapper.selectOne(emp));
}
b 运行截图
7.2 新增
7.2.1 通用mapper(无需自己写sql)
a 代码
@Test
public void addOne01(){
Emp e=new Emp(null,"小红","会计",1123, Date.valueOf("2022-12-26"),null,null,30);
int i = empMapper.insertSelective(e);
System.out.println("插入结果: "+(i>0));
System.out.println("添加后: "+e);
}
b 运行截图
7.2.2 使用自定义sql语句
a 在empMapper接口里面定义一个add(Emp e)方法
int add(Emp e);
b 在resource文件夹下创建mappers文件夹并创立EmpMapper.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.zlz.mapper.EmpMapper">
<insert id="add">
<selectKey order="BEFORE" resultType="int" keyProperty="empno">
SELECT sq_emp.nextval FROM dual
</selectKey>
insert into emp values(#{empno},#{ename},#{job},#{mgr}
,#{hiredate},#{sal},#{comm},#{deptno})
</insert>
</mapper>
c 在application.yml配置插入的字段值可以为空
c.1 示例代码
configuration:
jdbc-type-for-null: 'null' #运行添加null值
c.2 示例图片
d.测试
d.1 代码
@Test
public void addOne02(){
Emp e=new Emp(null,"小红","会计",1123, Date.valueOf("2022-12-26"),null,null,30);
int i = empMapper.add(e);
System.out.println("插入结果: "+(i>0));
System.out.println("添加后: "+e);
}
d.2 运行截图
7.3 修改
a 代码
@Test
public void updateOne(){
Emp e=new Emp();
e.setEmpno(8001);
e.setEname("小明");
int i =empMapper.updateByPrimaryKeySelective(e);
System.out.println("修改结果: "+(i>0));
}
b 运行截图
b.o 控制台输出
b.1 修改前
b.2 修改后
7.4 删除(按照条件)
a 代码
@Test
public void deleteMore(){
Emp e=new Emp();
e.setJob("会计");
int i =empMapper.delete(e);
System.out.println("删除结果: "+(i>0));
}
b 运行截图
b.o 控制台结果
b.1 删除前
b.2 删除后
7.5 删除(按照主键)
a 代码
@Test
public void deleteone(){
Emp e=new Emp();
e.setEmpno(8003);
int i =empMapper.deleteByPrimaryKey(e);
System.out.println("删除结果: "+(i>0));
}
b 运行截图
b.o 控制台结果
b.1 删除前
b.2 删除后