一、介绍

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接口

最新文章

  1. LeetCode Coin Change
  2. Web Api 简介
  3. MathType 公式后的空格问题
  4. 《1Q84》--[日]村上春树
  5. Ajax案例(使用ajax进行加法运算)
  6. border-radius导致overflow:hidden失效问题。
  7. 【转】IT职场人生系列之四:怎样写简历
  8. 算法 - 乞讨n中位数(C++)
  9. delphi 字符串查找替换函数 转
  10. html_栏目下拉
  11. FG面经: Interval问题合集
  12. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
  13. [leetcode]149. Max Points on a Line多点共线
  14. 定时自动从FTP服务器取数据脚本
  15. bzoj千题计划168:bzoj3513: [MUTC2013]idiots
  16. 获取进程ID,父进程ID,进程完整路径
  17. DotNetBar滚动条的疑似BUG
  18. (二)使用CXF开发WebService服务器端接口
  19. 在oracle数据库表中没有添加rowid字段为什么会出现?
  20. Egret打包App Android热更新(4.1.0)

热门文章

  1. oracle 表连接 - nested loop 嵌套循环连接
  2. 《JavaScript 高级程序设计》
  3. 手把手教您在 Windows Server 2019 上使用 Docker
  4. 测开之路一百:jquery引用、语法、事件
  5. astype()函数
  6. UI自动化之特殊处理四(获取元素属性\爬取页面源码\常用断言)
  7. ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/run/mysqld/mysqld.sock&#39;
  8. xml、Json生成cs代码文件
  9. SSM003/构建Maven单模块项目(一)
  10. C#中拼音模糊匹配汉字智能搜索