一、介绍

1. springboot是spring项目的总结+整合

  当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间还会出现冲突,让你的项目出现难以解决的问题。基于这种情况,springboot横空出世,在考虑到Struts控制层框架有漏洞,springboot放弃(大多数企业同样如此)了Struts,转而代之是springMVC,不过,springboot是自动集成springMVC的,不需要任何配置,不需要任何依赖,直接使用各种控制层注解。springboot是springcloud的基础,是开启微服务时代的钥匙。

2. mybatis-plus

  mybatis-plus是国内大佬基于mybatis基础上的一个增强版,且只做增强,不做减少,其核心是BaseMapper,这是一个通用版的dao接口,有着比较完善的CRUD操作,使用时只需将自己的dao接口继承BaseMapper即可,类似于之前【demo】中的核心dao思想。

3.对比mybatis优点

  不需要mapper.xml文件,dao直接继承BaseMapper<T> 即可使用。

二、新建springboot工程

1. 使用idea2019新建project,选择spring Initializr,next

2. 填写坐标信息,next

3. Developer Tools选择Lombok, Web选择Spring Web Starter,SQL选择MySQL Driver,next

  由于工程依赖中不包括mybatis-plus,所以稍后需要在pom中额外加入mybatis-plus依赖

  lombok是为了省去实体类中getter/setter方法,使之在运行时动态添加getter/setter

 

4. 填写项目名已经存放位置,finish

三、项目构建

1. 数据库准备

create database mybatis_plus;

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
); DELETE FROM user; INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

2. pom.xml

  在原有基础上添加

<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>

3. 配置文件

  application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root

4.项目包结构

5. 实体类User

package club.xcreeper.springboot_mybatis_plus.entity;

import lombok.Data;

@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}

6. dao层

package club.xcreeper.springboot_mybatis_plus.dao;

import club.xcreeper.springboot_mybatis_plus.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserDao extends BaseMapper<User> { }

7. service层

package club.xcreeper.springboot_mybatis_plus.service;

import club.xcreeper.springboot_mybatis_plus.entity.User;

public interface UserService  {
User getOne(long id);
User findByNameAndAge(String name,Integer age);
}
package club.xcreeper.springboot_mybatis_plus.service.impl;

import club.xcreeper.springboot_mybatis_plus.dao.UserDao;
import club.xcreeper.springboot_mybatis_plus.entity.User;
import club.xcreeper.springboot_mybatis_plus.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao; @Override
public User getOne(Long id) {
return userDao.selectById(id);
} @Override
public User findByNameAndAge(String name, Integer age) {
return userDao.selectOne(new QueryWrapper<User>().eq("name",name).eq("age",age));
} }

8. controller层

package club.xcreeper.springboot_mybatis_plus.controller;

import club.xcreeper.springboot_mybatis_plus.entity.User;
import club.xcreeper.springboot_mybatis_plus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @GetMapping(value = "/{id}")
public User getOne(@PathVariable long id){
return userService.getOne(id);
} @GetMapping(value = "/getUser",params = {"name","age"})
public User findByNameAndAge(String name,Integer age){
return userService.findByNameAndAge(name,age);
}
}

9. 需要在启动类上加@ScanMapper注解

package club.xcreeper.springboot_mybatis_plus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("club.xcreeper.springboot_mybatis_plus.dao")
public class SpringbootMybatisPlusApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
} }

10. 启动项目,并使用postman进行接口测试

  测试getOne接口

  测试findByNameAndAge接口

javaweb各种框架组合案例(八):springboot+mybatis-plus+restful的更多相关文章

  1. javaweb各种框架组合案例(五):springboot+mybatis+generator

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  2. javaweb各种框架组合案例(七):springboot+jdbcTemplete+通用dao+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  3. javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  4. javaweb各种框架组合案例(九):springboot+tk.mybatis+通用service

    一.项目结构 二.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  5. javaweb各种框架组合案例(二):maven+spring+springMVC+mybatis

    1.mybatis是比较新的半自动orm框架,效率也比较高,优点是sql语句的定制,管理与维护,包括优化,缺点是对开发人员的sql功底要求较高,如果比较复杂的查询,表与表之间的关系映射到对象与对象之间 ...

  6. javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

    1.hibernate译为"越冬",指的是给java程序员带来春天,因为java程序员无需再关心各种sql了: 2.hibernate通过java类生成数据库表,通过操作对象来映射 ...

  7. javaweb各种框架组合案例(四):maven+spring+springMVC+spring data jpa(hibernate)【失败案例】

    一.失败案例 1. 控制台报错信息 严重: Exception sending context initialized event to listener instance of class org. ...

  8. javaweb各种框架组合案例(一):maven+spring+springMVC+jdbcTemplate

    为了体现spring jdbc对于单表操作的优势,我专门对dao层做了一个抽离,使得抽离出的核心dao具有通用性.主要技术难点是对于泛型的反射.注意:单表操作中,数据库表的字段要和实体类的属性名保持高 ...

  9. Springboot & Mybatis 构建restful 服务五

    Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...

随机推荐

  1. 洛谷 P4570 BZOJ 2460 [BJWC2011]元素

    Time limit 20000 ms Memory limit 131072 kB OS Linux 解题思路 看题解可知 我们将矿石按照魔法值降序排序,然后依次将矿石编号放入线性基(突然想起线代里 ...

  2. Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity

    在一个大的Android项目中,由于客户端来不及更新和实现,经常会内嵌一些网页(在一些大型的互联网公司,PC的产品总是跑在客户端的前面),比如活动页面,通常可以内嵌用html5实现的页面,可以适配手机 ...

  3. [CSP-S模拟测试]:string(线段树)

    题目描述 给定一个由小写字母组成的字符串$s$. 有$m$次操作,每次操作给定$3$个参数$l,r,x$. 如果$x=1$,将$s[l]~s[r]$升序排序: 如果$x=0$,将$s[l]~s[r]$ ...

  4. MySQL高可用架构之MySQL5.7.19 PXC

    CentOS7.3下Percona-XtraDB-Cluster-5.7.19集群部署PXC三节点安装:node1:10.10.10.11 node2:10.10.10.12node3:10.10.1 ...

  5. DAY 3 上午

    状压DP 状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总量很少时,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是:都存在 ...

  6. 线性代数之——SVD 分解

    SVD 分解是线性代数的一大亮点. 1. SVD 分解 \(A\) 是任意的 \(m×n\) 矩阵,它的秩为 \(r\),我们要对其进行对角化,但不是通过 \(S^{-1}A S\).\(S\) 中的 ...

  7. mysql_Qcahce

    .cpu mem disk 如果是固态硬盘ssd那就是高速公路 火箭 高铁 普通公路 mysql 配置文件:windows 下 mysql.ini linux:my.cnf lamp路径:/opt/l ...

  8. Windows Server 2008 R2 为用户“IIS APPPOOL\DefaultAppPool”授予的权限不足,无法执行此操作

    报表开发与部署好后,也嵌入到aspx页面中了,使用VS自带的Web服务器组件,一切正常,当部署到IIS中的时,出现了如下错误: 为用户“IIS APPPOOL\DefaultAppPool”授予的权限 ...

  9. BIN转换成HEX格式及HEX转换成BIN的两个函数接口

    unsigned char HEX2BYTE(unsigned char hex_ch) { ') { '; } if (hex_ch >= 'a' && hex_ch < ...

  10. oracle--本地网络配置tnsnames.ora和监听器listener.ora

    文件tnsnames.ora 是给orcl客户端使用 配置本地网络服务:(客户端) 第一种使用暴力方式直接操作: 修改:C:\app\Administrator\product\11.2.0\dbho ...