Springboot mybatis-plus配置及用法詳解
本節(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='http://m.4tl426be.cn/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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)2. python 寫函數(shù)在一定條件下需要調(diào)用自身時(shí)的寫法說明3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表6. 利用CSS制作3D動(dòng)畫7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼9. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)10. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程
