上一節《spring boot開發web應用-更為常用的mybatis》中我們簡單介紹了mybatis持久化框架的使用,本節內容擴展介紹下針對mybatis的增強工具mybatis-plus,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生,
二話不多說,我們先寫撰寫個簡單的例子,讓大家先初步的了解下mybatis-plus,
1.mybatis-plus初步實體
(1)創建一個spring boot web工程(具體創建程序就不再演示了,還不會的同學去看看spring boot專題第一節內容)
(2)引入依賴
<!--web專案依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--validation表單校驗--> <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驅動--> <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)物體類User
package com.kinglead.demo.domain; ? import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; ? @Data @TableName(value = "t_user") //指明資料庫表名 public class User { private Long id; private String name; private Integer age; private String email; }
(5)創建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)創建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)創建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="https://www.cnblogs.com/css/common.css" />--> <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>序號</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)啟動類
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)測驗訪問

小結
我們能看到mybatis-plus通過對mybatis的加強,能在不寫mapper.xml檔案的情況下,完成簡單的CRUD的操作,看到這里,有的小伙伴可能會想,這和JPA有什么不一樣,直接用JPA不就好啦,
下面我們來對比下他們倆的區別:要說mybatis-plus,得先說mybatis,mybatis比較接近原生sql,要想使用的好,需要很好的sql基礎,所有的資料庫操作都必須寫sql陳述句;JPA是對hibernate的封裝,提取了很多CRUD的公共方法,可以在不寫sql的情況下完成不復雜的CRUD;mybatis-plus是mybatis的增強工具,天生擁有mybatis的優勢,也具備像JPA一樣擁有很多CRUD的公共方法,簡單的sql直接呼叫,不要撰寫陳述句,
那我們該如何選擇他們呢?沒有哪個框架是最優的,主要還是要根據實際專案情況而定,如果專案不復雜,涉及不到很多復雜的資料處理,那么建議考慮JPA,如果專案復雜,涉及到很多復雜的資料處理,那么建議考慮mybatis或mybatis-plus,在這基礎上,如果想簡化mybatis,可以考慮mybatis-plus,
本節內容只是展示了mybatis-plus的冰山一角,它還有擁有很多特色功能,如支持主鍵欄位生成、內置分頁插件等,
原始碼地址:https://github.com/kinglead2012/myblog
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/97445.html
標籤:Java
上一篇:BDE連接ORACLE報錯
