欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot mybatis-plus配置及用法詳解

 更新時(shí)間:2020年09月30日 11:41:00   作者:kinglead  
這篇文章主要介紹了Springboot mybatis-plus配置及用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

本節(jié)內(nèi)容擴(kuò)展介紹下針對(duì)mybatis的增強(qiáng)工具mybatis-plus,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。

二話不多說,我們先寫編寫個(gè)簡(jiǎn)單的例子,讓大家先初步的了解下mybatis-plus。

1.mybatis-plus初步實(shí)例

(1)創(chuàng)建一個(gè)spring boot web工程(具體創(chuàng)建過程就不再演示了,還不會(huì)的同學(xué)去看看spring boot專題第一節(jié)內(nèi)容)

(2)引入依賴

<!--web項(xiàng)目依賴-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!--validation表單校驗(yàn)-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-validation</artifactId>
 </dependency>
 <!--mybatis-plus-->
 <dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.4.0</version>
 </dependency>
 <!--mysql驅(qū)動(dòng)-->
 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <!--thymeleaf依賴包-->
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <!--lombok-->
 <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
 </dependency>

(3)配置文件application.yml

# DataSource Config
 spring:
  datasource:
   username: root
   password: tx@mysql@2020
   url: jdbc:mysql://188.131.233.55:3306/spring_boot_topic
   driver-class-name: com.mysql.cj.jdbc.Driver(4)

(4)實(shí)體類User

package com.kinglead.demo.domain;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 ​
 @Data
 @TableName(value = "t_user") //指明數(shù)據(jù)庫表名
 public class User {
   private Long id;
   private String name;
   private Integer age;
   private String email;
 }

(5)創(chuàng)建Mapper接口

package com.kinglead.demo.mapper;
 ​
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.kinglead.demo.domain.User;
 ​
 //未來使用mybatis-plus的公共接口,必須繼承BaseMapper
 public interface UserMapper extends BaseMapper<User> {
 }

(6)創(chuàng)建Service接口

UserService

package com.kinglead.demo.service;
 import com.kinglead.demo.domain.User;
 import java.util.List;
 public interface UserService {
   List<User> queryUserList();
 }

UserServiceImpl

package com.kinglead.demo.service.impl;
 ​
 import com.kinglead.demo.domain.User;
 import com.kinglead.demo.mapper.UserMapper;
 import com.kinglead.demo.service.UserService;
 import org.springframework.stereotype.Service;
 ​
 import javax.annotation.Resource;
 import java.util.List;
 ​
 @Service
 public class UserServiceImpl implements UserService {
   @Resource
   private UserMapper userMapper;
   @Override
   public List<User> queryUserList() {
     //使用mybatis-plus公共查詢接口完成列表查詢
     return userMapper.selectList(null);
   }
 }

(7)創(chuàng)建controller

package com.kinglead.demo.controller;
 ​
 import com.kinglead.demo.domain.User;
 import com.kinglead.demo.service.UserService;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 ​
 import javax.annotation.Resource;
 import java.util.List;
 ​
 @Controller
 @RequestMapping("/user")
 public class UserController {
 ​
   @Resource
   private UserService userService;
 ​
   @RequestMapping("/userList")
   public ModelAndView queryUserList(ModelAndView modelAndView){
     List<User> userList = userService.queryUserList();
     modelAndView.addObject("userList", userList);
     modelAndView.setViewName("userList");
     return modelAndView;
   }
 ​
 }

(8)用戶列表頁面

<!DOCTYPE html>
 <html xmlns:th="http://www.thymeleaf.org">
   <head>
     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
     <title>用戶信息</title>
     <!--<link rel="stylesheet" type="text/css" href="/css/common.css" rel="external nofollow" />-->
     <style type="text/css">
       table {
         border: 1px solid black;
         text-align: center;
         border-collapse: collapse;
       }
       table thead th {
         border: 1px solid black;
       }
       table tbody td {
         border: 1px solid black;
       }
     </style>
   </head>
   <body>
     <div>
       <h2>用戶列表</h2>
     </div>
     <table cellpadding="0" cellspacing="0">
       <thead>
         <th>序號(hào)</th>
         <th>編碼</th>
         <th>用戶名</th>
       </thead>
       <tbody>
       <tr th:each="entries,stat:${userList}" th:style="' color: rgb(17, 119, 0);">>
         <td th:text="${stat.count}"></td>
         <td th:text="${entries['id']}"></td>
         <td th:text="${entries['name']}"></td>
       </tr>
       </tbody>
     </table>
   </body>
 </html>

(9)啟動(dòng)類

package com.kinglead.demo;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
 @MapperScan("com.kinglead.demo.mapper")
 public class App {
   public static void main(String[] args) {
     SpringApplication.run(App.class, args);
   }
 }

(10)測(cè)試訪問

小結(jié)

我們能看到mybatis-plus通過對(duì)mybatis的加強(qiáng),能在不寫mapper.xml文件的情況下,完成簡(jiǎn)單的CRUD的操作??吹竭@里,有的小伙伴可能會(huì)想,這和JPA有什么不一樣,直接用JPA不就好啦。

下面我們來對(duì)比下他們倆的區(qū)別:要說mybatis-plus,得先說mybatis。mybatis比較接近原生sql,要想使用的好,需要很好的sql基礎(chǔ),所有的數(shù)據(jù)庫操作都必須寫sql語句;JPA是對(duì)hibernate的封裝,提取了很多CRUD的公共方法,可以在不寫sql的情況下完成不復(fù)雜的CRUD;mybatis-plus是mybatis的增強(qiáng)工具,天生擁有mybatis的優(yōu)勢(shì),也具備像JPA一樣擁有很多CRUD的公共方法,簡(jiǎn)單的sql直接調(diào)用,不要編寫語句。

那我們?cè)撊绾芜x擇他們呢?沒有哪個(gè)框架是最優(yōu)的,主要還是要根據(jù)實(shí)際項(xiàng)目情況而定。如果項(xiàng)目不復(fù)雜,涉及不到很多復(fù)雜的數(shù)據(jù)處理,那么建議考慮JPA。如果項(xiàng)目復(fù)雜,涉及到很多復(fù)雜的數(shù)據(jù)處理,那么建議考慮mybatis或mybatis-plus,在這基礎(chǔ)上,如果想簡(jiǎn)化mybatis,可以考慮mybatis-plus。

本節(jié)內(nèi)容只是展示了mybatis-plus的冰山一角,它還有擁有很多特色功能,如支持主鍵字段生成、內(nèi)置分頁插件等。

源碼地址:https://github.com/kinglead2012/myblog

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java開發(fā)使用StringUtils.split避坑詳解

    java開發(fā)使用StringUtils.split避坑詳解

    這篇文章主要為大家介紹了java開發(fā)使用StringUtils.split避坑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java實(shí)現(xiàn)浪漫流星表白的示例代碼

    Java實(shí)現(xiàn)浪漫流星表白的示例代碼

    本文將利用Java語言實(shí)現(xiàn)浪漫流星表白,可以實(shí)現(xiàn)這些功能:播放音樂、自定義流星數(shù)量、飛行速度、光暈大小、流星大小,自定義表白話語,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • Java使用JMeter進(jìn)行高并發(fā)測(cè)試

    Java使用JMeter進(jìn)行高并發(fā)測(cè)試

    軟件的壓力測(cè)試是一種保證軟件質(zhì)量的行為,本文主要介紹了Java使用JMeter進(jìn)行高并發(fā)測(cè)試,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java SimpleDateFormat線程安全問題原理詳解

    Java SimpleDateFormat線程安全問題原理詳解

    這篇文章主要介紹了Java SimpleDateFormat線程安全問題原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • idea 配置checkstyle詳細(xì)步驟

    idea 配置checkstyle詳細(xì)步驟

    checkstyle是提高代碼質(zhì)量,檢查代碼規(guī)范的很好用的一款工具,本文簡(jiǎn)單介紹一下集成的步驟,并提供一份完整的checkstyle的代碼規(guī)范格式文件,以及常見的格式問題的解決方法,需要的朋友可以參考下
    2023-11-11
  • 關(guān)于@ComponentScan?TypeFilter自定義指定掃描bean的規(guī)則

    關(guān)于@ComponentScan?TypeFilter自定義指定掃描bean的規(guī)則

    這篇文章主要介紹了關(guān)于@ComponentScan?TypeFilter自定義指定掃描bean的規(guī)則,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    Spring Boot提供了一種方便的方式來實(shí)現(xiàn)定時(shí)任務(wù),即使用Spring的@Scheduled注解,通過在方法上添加@Scheduled注解,我們可以指定方法在何時(shí)執(zhí)行,本文我們就給大家介紹一下SpringBoot如何使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù),需要的朋友可以參考下
    2023-08-08
  • Java模擬實(shí)現(xiàn)斗地主的洗牌和發(fā)牌

    Java模擬實(shí)現(xiàn)斗地主的洗牌和發(fā)牌

    這篇文章主要為大家詳細(xì)介紹了Java模擬實(shí)現(xiàn)斗地主的洗牌和發(fā)牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java中RocketMQ使用方法詳解

    Java中RocketMQ使用方法詳解

    這篇文章主要介紹了RocketMQ和Kafka在SpringBoot中的使用方法,以及如何保證消息隊(duì)列的順序性、可靠性以及冪等性,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • Java 通過位運(yùn)算求一個(gè)集合的所有子集方法

    Java 通過位運(yùn)算求一個(gè)集合的所有子集方法

    下面小編就為大家?guī)硪黄狫ava 通過位運(yùn)算求一個(gè)集合的所有子集方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03

最新評(píng)論