mybatis-spring-boot-starter 1.3.0 操作实体类的SpringBoot例子
2024-08-27 19:49:06
例程下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200428-02.zip
需求:使用mybatis实现对hy_emp表的CRUD。
实现步骤:
1.添加依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
注:《SpringBoot实战派》P201提供的写法,version2.0.0会报错,故而改成1.3.0.
2.书写表对应的实体类。
package com.ufo.gatling.entity; public class Emp {
private long id; private String name; private int salary; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
3.书写Mapper接口
package com.ufo.gatling.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update; import com.ufo.gatling.entity.Emp; @Mapper
public interface EmpMapper {
@Select("select * from hy_emp where id=#{id}")
Emp findById(@Param("id") long id); @Select("select * from hy_emp")
List<Emp> findAll(); @Update("update hy_emp set name=#{name},salary=#{salary} where id=#{id}")
int updateById(Emp emp); @Delete("delete from hy_emp where id=#{id}")
int deleteById(Emp emp); @Insert("insert into hy_emp(id,name,salary) values(#{id},#{name},#{salary})")
int insert(Emp emp); @SelectProvider(type=EmpSql.class,method="findHighLevel")
List<Emp> findHighLevelEmps();
}
这种写法把SQL和接口写到了一起,比早期MyBatis版本的分两个文件明白多了(早期项目里通过Java类查找翻到接口,再去找同名xml实在是有够反人类)。如果SQL够长够复杂则可以通过SelectProvider的方式,分到一个文件里去好好写,下面就是涉及到的EmpSql类。
4.EmpSQl类:
package com.ufo.gatling.mapper; public class EmpSql {
public String findHighLevel() {
return "select * from hy_emp where salary>15000";
}
}
有人会说这个也不复杂,例子当然不复杂,真写起项目来想不复杂都不行。
5.测试类,请单个函数调试或执行。
package com.ufo.gatling; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import com.ufo.gatling.entity.Emp;
import com.ufo.gatling.mapper.EmpMapper; @SpringBootTest
class GatlingApplicationTests {
@Autowired
private EmpMapper mapper=null; @Test
void test01_findById() {
Emp emp=mapper.findById(6);
assertEquals("Felix", emp.getName());
assertEquals(20000, emp.getSalary());
} @Test
void test02_findAll() {
List<Emp> empList=mapper.findAll();
assertEquals(6, empList.size());
} @Test
void test03_updateById() {
Emp emp=new Emp();
emp.setId(1);
emp.setName("Gates");
emp.setSalary(123456); int changedCount=mapper.updateById(emp);
assertEquals(1, changedCount); Emp found=mapper.findById(1);
assertEquals("Gates", found.getName());
assertEquals(123456, found.getSalary());
} @Test
void test04_deleteById() {
Emp emp=new Emp();
emp.setId(3); int changedCount=mapper.deleteById(emp);
assertEquals(1, changedCount); emp=mapper.findById(3);
assertEquals(null, emp);
} @Test
void test05_insert() {
Emp emp=new Emp();
emp.setId(7);
emp.setName("Bear");
emp.setSalary(300000); int changedCount=mapper.insert(emp);
assertEquals(1, changedCount); Emp found=mapper.findById(7);
assertEquals("Bear", found.getName());
assertEquals(300000, found.getSalary());
} @Test
void test06_findHighLevelEmps() {
List<Emp> empList=mapper.findHighLevelEmps();
assertEquals(4, empList.size());
}
}
好了,全文就到这里,具体细节请下载代码。
Java无论是编码还是配置看网文或是书籍都觉得简单,自己动手一操作发现很多细节挡害,不用心解决都执行不下去。
真是纸上得来终觉浅,绝知此事要躬行。
--2020-04-28--
最新文章
- `UnityEditor.EditorUtility&#39; does not contain a definition for `GetMiniThumbnail&#39;
- Html5的一些引擎使用感触
- Python-类的继承
- C#5.0 .net 4.5示例
- [ActionScript 3.0] AS3.0 动态加载显示内容
- js 函数function的几种形式
- CentOS 7下安装xampp和testlink
- Day02
- 深入Objective-C的动态特性 Runtime
- 创建基本的2D场景(part1)
- MAC下安装MAMP的Mongodb
- FtpUtil.java测试 (淘淘商城第三课文件上传)
- &#39;abc&#39; 转换成[a, b, c]一道面试题的思考
- SQL语句取多列的最小值(排除0)
- Java模拟耗时任务异步执行
- 实现两线程的同步二(lockSupport的park/unpark)
- Android抓包方法(转)
- html的文字样式、下行线、删除线、上标、下标等实现方式
- 2. select下拉框获取选中的值
- [Asp.net mvc]Asp.net mvc 中使用LocalStorage
热门文章
- Docker容器网络-实现篇
- 字节跳动:[编程题]万万没想到之聪明的编辑 Java
- C#图解教程(第四版)—03—类和继承
- Java爬取先知论坛文章
- 洛谷P1036.选数(DFS)
- python基础 Day10
- C++ Templates (2.1 类模板Stack的实现 Implementation of Class Template Stack)
- 6. 二十不惑,ObjectMapper使用也不再迷惑
- 从Vessel到二代裸金属容器,云原生的新一波技术浪潮涌向何处?
- 基于id3算法根据房价数据进行画图预测python