javaweb各類框架組合案例(八):springboot+mybatis-plus+restful

1、介紹html

1. springboot是spring項目的總結+整合mysql

  當咱們搭smm,ssh,ssjdbc等組合框架時,各類配置不勝其煩,不只是配置問題,在添加各類依賴時也是讓人頭疼,關鍵有些jar包之間還會出現衝突,讓你的項目出現難以解決的問題。基於這種狀況,springboot橫空出世,在考慮到Struts控制層框架有漏洞,springboot放棄(大多數企業一樣如此)了Struts,轉而代之是springMVC,不過,springboot是自動集成springMVC的,不須要任何配置,不須要任何依賴,直接使用各類控制層註解。springboot是springcloud的基礎,是開啓微服務時代的鑰匙。web

2. mybatis-plusspring

  mybatis-plus是國內大佬基於mybatis基礎上的一個加強版,且只作加強,不作減小,其核心是BaseMapper,這是一個通用版的dao接口,有着比較完善的CRUD操做,使用時只需將本身的dao接口繼承BaseMapper便可,相似於以前【demo】中的核心dao思想。sql

 3.對比mybatis優勢數據庫

  不須要mapper.xml文件,dao直接繼承BaseMapper<T> 便可使用。springboot

 

2、新建springboot工程mybatis

1. 使用idea2019新建project,選擇spring Initializr,nextapp

 

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

 

 

3、項目構建

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

  

相關文章
相關標籤/搜索