当前位置:网站首页>Data Persistence Technology - MP
Data Persistence Technology - MP
2022-07-31 11:22:00 【m0_67401920】
文章目录
mybatis-plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生.
1 环境搭建
1.1 创建一个maven的project
1.2 pom.xml文件导入web开发依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.3 创建SpringBoot项目的主程序入口
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
2 代码生成器
顾名思义,代码生成器就是用来直接生成代码的一个程序.首先导入相关依赖
<!--Mybatis-Plus生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<!--freemarker 模板引擎(没有用原生的模板引擎)-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
生成的代码需要使用lombok简化实体类开发导入相关依赖
<!--lombok简化实体类开发,如果之前没有下载过依赖的同名插件的话需要下载安装,然后重启一下idea-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
创建utils包,拷贝代码生成器,需要修改的两个地方:数据库连接的数据库名、项目目录名.运行main方法,报错java.lang.ClassNotFoundException:com.mysql.jdbc.Driver,没有驱动,需要导入数据库连接依赖
<!--数据库连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
生成成功之后,mapper层的接口上加@mapper注解或者在主程序入口出添加,简单说一下service的方法都从哪里来,也就是Ctrl进源码看他继承方法内部又实现的接口里定义了后面需要使用到的所有方法.
3 配置数据库
导入mybatis-plus的场景启动器
<!--mybatis-plus的场景启动器 内置了jdbc的启动器无需重复引用-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
resources目录下创建配置文件application.yml,配置数据库的相关信息
server:
# 修改后端项目运行时的端口号信息
port: 8888
spring:
# 配置数据源信息
datasource:
url: jdbc:mysql://localhost:3306/music
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
上面使用到了pool(池子),数据库连接池也就是数据源,所谓的数据库连接池从字面意思上翻译就是将数据库连接放到一个池子里.相比之前的好处就是只需要创建一次连接,后面的都直接用不用一次次创建.数据源使用到了Druid的,需要导入Druid的依赖
<!--整合druid的数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
4 导入前端页面
resources目录下创建templates文件夹,将前端页面导入.由于SpringBoot默认的打包方式是jar包,但是JSP不支持在jar包(一种压缩包)中编译,所以SpringBoot默认不支持JSP,于是我们需要引入第三方的模板引擎技术——Thymeleaf实现页面的渲染.
导入thymeleaf引擎的场景启动器
<!--thymeleaf引擎的场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
5 开始前端控制器的编码
复制controllerThe overall frame of the layer,The page that interacts with the front end
@RequestMapping("/queryAll")
public List<Singer> queryAll() {
// 先演示普通的查询,再逆序查询
return null;
}
@RequestMapping("/queryByName")
public List<Singer> queryByName(String name) {
// 使用lambdaQueryWrapper 的like方法模糊查询
// list方法查询到所有的符合条件值并返回
return null;
}
@PostMapping("/save")
public String save(HttpServletRequest request) {
Singer singer = new Singer();
singer.setName(request.getParameter("name"))
.setSex(Integer.parseInt(request.getParameter("sex")))
.setPic(request.getParameter("pic"))
.setBirth(new Date())
.setLocation(request.getParameter("location"))
.setIntroduction(request.getParameter("introduction"));
// 调用save方法,并判断返回值的正误,返回前端对应的字符串
return null;
}
@RequestMapping("/update")
public String update(int id, String introduction) {
// 使用eq和set去设置条件构造器
// 调用update传入条件构造器,并判断返回值的正误,返回前端对应的字符串
return null;
}
@RequestMapping("/delete")
public String delete(Integer id) {
// 调用removeById方法直接删
return null;
}
SQLStatements are the core of data persistence technology,jdbc使用String字符串拼接SQL语句;mybatis使用xmlMapping files are written in tagsSQL语句;但是,MP无需使用SQL语句(Some complex business can useSQL语句来完成),It can be implemented at the bottom of the framework by directly calling the methodSQLStatement concatenation and execution.
Check it out heremp的官方开发文档,Briefly describe the usefulness of several methods,然后摁住Ctrl查看源码,Know why you can use these methods to complete the corresponding function.
The first is to injectSingerService对象
@Autowired
private SingerService service;
这里的用法是spring框架的控制反转,The designed class object is about to be registeredspring的IOCThe container transfers control of the object to the container,Use it when you need [email protected] let the container create an object to use.The effect of completion is equivalent to usenewCreate an object by calling the class's constructor
查询所有的记录
@RequestMapping("/queryAll")
public List<Singer> queryAll() {
List<Singer> singers = service.list();
return singers;
}
If you want to query in reverse order, you need toSQL语句拼接order by id desc,This splicing condition function needs to be usedwrapperThe constructor is done
5.1 wrapper构造器
总而言之,wrapperThe role of the constructor is to assemblewhere,order bythe role of conditional statements,If your additions, deletions, modifications and inspections do not require conditions,可以不用wrapper,Whenever neededSQLIf the statement needs to concatenate conditions, it needs to be usedwrapper.更加具体的wrapperCase study reference blogwrapperA brief case study of constructors
A method to query all records in reverse order
Query all in reverse order
http://localhost:8888/singer/queryAll
@RequestMapping("/queryAll")
public List<Singer> queryAll() {
LambdaQueryWrapper<Singer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.orderByDesc(Singer::getId);
List<Singer> singers = service.list(lambdaQueryWrapper);
return singers;
}
按照id查询
按照id查询
http://localhost:8888/singer/queryByName?name=王
@RequestMapping("/queryByName")
public List<Singer> queryByName(String name) {
LambdaQueryWrapper<Singer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(Singer::getName,name);
List<Singer> singers = service.list(lambdaQueryWrapper);
return singers;
}
编写添加记录的方法
@PostMapping("/save")
public String save(HttpServletRequest request) {
Singer singer = new Singer();
singer.setName(request.getParameter("name"))
.setSex(Integer.parseInt(request.getParameter("sex")))
.setPic(request.getParameter("pic"))
.setBirth(new Date())
.setLocation(request.getParameter("location"))
.setIntroduction(request.getParameter("introduction"));
boolean save = service.save(singer);
String result = save == true ? "添加成功" : "添加失败";
return result;
}
修改方法
修改
http://localhost:8888/singer/update?id=64&introduction=歌声甜美
@RequestMapping("/update")
public String update(int id, String introduction, HttpServletRequest request) {
LambdaUpdateWrapper<Singer> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(Singer::getId,id)
.set(Singer::getIntroduction,introduction);
boolean update = service.update(lambdaUpdateWrapper);
String result = update == true ? "修改成功" : "修改失败";
return result;
}
删除方法
删除
http://localhost:8888/singer/delete?id=64
@RequestMapping("/delete")
public String delete(Integer id) {
boolean remove = service.removeById(id);
String result = remove == true ? "删除成功" : "删除失败";
return result;
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
SQL - Left join, Right join, Inner join
cesium-Web网页优化进阶
LeetCode 1161.最大层内元素和:层序遍历
Experience innovation and iteration through the development of a lucky draw applet
Intranet Penetration Learning (IV) Domain Lateral Movement - SMB and WMI Service Utilization
Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
众多mock工具,这一次我选对了
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
Master SSR
随机推荐
Redis缓存面临的缓存雪崩问题
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
最新MySql安装教学,非常详细
Android studio连接MySQL并完成简单的登录注册功能
Redis-基础
3.网页信息解析方法:Xpath与BeautifulSoup
多线程学习笔记-2.final关键字和不变性
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
瑞吉外卖项目:文件的上传与下载
Usage of JOIN in MySQL
R语言做面板panelvar例子
Life is endless, there are more questions, simple questions to learn knowledge points
CoCube群机器人预览→资讯剧透←
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
7 天学个Go,Go 结构体 + Go range 来学学
瑞吉外卖项目:新增菜品与菜品分页查询
lotus-local-net 2k v1.17.0-rc4
「R」使用ggpolar绘制生存关联网络图
PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用