av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Springboot mybatis-plus配置及用法詳解

瀏覽:2日期:2023-04-19 14:50:29

本節(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è)試訪問

Springboot mybatis-plus配置及用法詳解

小結(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)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久久国产精品一区 | 日韩欧美网 | 日本成人免费网站 | 久久99精品久久久久久秒播九色 | 国产剧情一区 | 午夜在线精品偷拍 | 精品国产99 | 日本超碰 | av无遮挡| 91成人精品| 国产1区2区 | 亚洲欧美日韩精品 | 日本三级网址 | 天天插日日操 | 国产一区二区在线播放 | 99免费在线观看视频 | 成人在线视频免费观看 | 中文字幕日韩欧美一区二区三区 | 国产成人久久精品一区二区三区 | 91视频免费 | 国产一二区视频 | 亚洲精品在线观 | 国产中文字幕在线 | 国产精品久久久久久久久久 | 黄色免费网站在线看 | 精品二区 | 亚洲专区在线 | 国产91精品网站 | av一区二区三区在线观看 | 日本一级淫片免费啪啪3 | 我要看免费一级毛片 | 中文字幕免费视频 | 欧美精品一区二区三区蜜桃视频 | 久久999 | 久久国产欧美日韩精品 | 日韩欧美一区二区在线播放 | 精品久久久久久亚洲国产800 | 亚洲一区| 久久久噜噜噜www成人网 | 国产精品国产三级国产aⅴ原创 | 亚洲一区二区视频 |