Spring Boot 鏁村悎 Mybatis-Plus
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 鏁村悎 Mybatis-Plus相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/config' title='config'>config 娣诲姞鐢ㄦ埛 username log rip web ext jdb div
1.Spring 鏁村悎 Mybatis-Plus
銆€銆€a.閾炬帴锛歨ttps://www.cnblogs.com/vettel0329/p/11990721.html
2.鍚庣鎼缓锛?/p>
銆€銆€a.鍦ㄦ暟鎹簱鍒涘缓 tb_user 鐢ㄦ埛琛?/p>
-- 鐢ㄦ埛琛?/span> CREATE TABLE `tb_user` ( `id` varchar(255) NOT NULL, `user_name` varchar(255) NOT NULL, `user_code` int(11) NOT NULL, PRIMARY KEY (`id`) );
銆€銆€b.鍒涘缓SpringBoot宸ョ▼锛岄€夋嫨渚濊禆 Web銆乀hymeleaf銆丮ysql Driver锛屽苟鎵嬪姩娣诲姞 Mybatis-Plus 鐨凷pringBoot渚濊禆
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> ...... <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ...... </dependencies>
銆€銆€c.缂栬緫 application.yml 閰嶇疆鏂囦欢
# 鏈嶅姟绔彛 server: port: 8081 spring: # 鏈嶅姟鍚嶇О application: name: springboot-mybatis-plus # 鏁版嵁婧愰厤缃?/span> datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password: 123456 # thymeleaf鐑洿鏂?/span> thymeleaf: cache: false # mybatis-plus鐩稿叧閰嶇疆 mybatis-plus: # entity鎵弿 type-aliases-package: com.wode.springbootmybatisplus.entity global-config: db-config: # AUTO -> 鏁版嵁搴揑D鑷 # INPUT -> 鐢ㄦ埛杈撳叆ID # ID_WORKER -> 鍏ㄥ眬鍞竴ID锛堟暟瀛楃被鍨嬪敮涓€ID锛?/span> # UUID -> 鍏ㄥ眬鍞竴ID锛圲UID锛?/span> id-type: UUID # 鍏ㄥ眬琛ㄥ墠缂€ table-prefix: tb_
銆€銆€d.鍒涘缓 Mybatis-Plus 閰嶇疆绫?/p>
@Configuration @EnableTransactionManagement //寮€鍚簨鍔?/span> @MapperScan("com.wode.springbootmybatisplus.dao") //mapper鎵弿 public class MybatisPlusConfig { //鍒嗛〉鎻掍欢 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
銆€銆€e.鍒涘缓entity瀹炰綋绫?/p>
@TableName public class User { @TableId private String id; private String userName; private int userCode; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getUserCode() { return userCode; } public void setUserCode(int userCode) { this.userCode = userCode; } @Override public String toString() { return "User(id[" + id + "], userName[" + userName + "], userCode[" + userCode + "])"; } }
銆€銆€f.鍒涘缓Dao
public interface UserMapper extends BaseMapper<User> { }
銆€銆€g.鍒涘缓Service
@Service public class UserService { @Resource private UserMapper userMapper; public boolean addUser(User user){ if(userMapper.insert(user) > 0){ return true; } return false; } public boolean updateUser(User user){ if(userMapper.updateById(user) > 0){ return true; } return false; } public User getUser(String id){ return userMapper.selectById(id); } public IPage<User> getUserList(int currentPage, int pageSize){ Page<User> page = new Page<>(currentPage, pageSize); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); return userMapper.selectPage(page, queryWrapper); } public boolean deleteUser(String id){ if(userMapper.deleteById(id) > 0){ return true; } return false; } }
銆€銆€h.鍒涘缓Controller
@RestController @RequestMapping("/user") public class UserController { @Resource private UserService userService; @PostMapping("/add") public String addUser(User user) { if(userService.addUser(user)){ return "Success"; } return "Failure"; } @PostMapping("/update") public String updateUser(User user) { if(userService.updateUser(user)){ return "Success"; } return "Failure"; } @RequestMapping("/get/{id}") public User getUser(@PathVariable("id") String id) { return userService.getUser(id); } @RequestMapping("/list") public IPage<User> getUserList(int currentPage, int pageSize){ return userService.getUserList(currentPage, pageSize); } @RequestMapping("/delete/{id}") public String deleteUser(@PathVariable("id") String id){ if(userService.deleteUser(id)){ return "Success"; } return "Failure"; } }
@Controller public class PageController { //榛樿椤甸潰 @RequestMapping("/") public String index() { return "home"; } //璺宠浆鍒伴椤?/span> @RequestMapping("/home") public String home() { return "home"; } //璺宠浆鍒拌鎯?/span> @RequestMapping("/detail") public String detail() { return "detail"; } }
3.鍓嶇娴嬭瘯
銆€銆€a.鍦?resources/templates 涓嬪垱寤?home.html 鍜?detail.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>棣栭〉</title> <link rel="stylesheet" href="css/common.css"/> </head> <body> <div> <h1>棣栭〉</h1> <div class="row"> <table id="list-table" border="1"> <tr> <th>ID</th> <th>璐﹀彿</th> <th>缂栧彿</th> <th>鎿嶄綔</th> </tr> </table> </div> <div class="row"> <div class="col"> <button id="list-btn">鐢ㄦ埛鍒楄〃</button> </div> <div class="col"> <button id="add-btn">娣诲姞鐢ㄦ埛</button> </div> </div> </div> </body> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/home.js"></script> </html>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>淇濆瓨</title> <link rel="stylesheet" href="css/common.css"/> </head> <body> <div> <h1>淇濆瓨</h1> <div class="row"> <div class="col"> <label>鐢ㄦ埛鍚嶏細</label><input id="username-input" type="text"/> </div> </div> <div class="row"> <div class="col"> <label>缂栧彿锛?span style="color: #0000ff;"></label><input id="usercode-input" type="text"/> </div> </div> <div class="row"> <div class="col"> <button id="save-btn">淇濆瓨</button> </div> <div class="col"> <button id="close-btn">鍏抽棴</button> </div> </div> <input id="id" type="hidden" th:value="${#request.getParameter(鈥榠d鈥?}" /> </div> </body> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="js/detail.js"></script> </html>
銆€銆€b.鍦?resources/static/css 涓嬪垱寤?nbsp;common.css
.row{ margin:10px 0px; } .col{ display: inline-block; margin:0px 5px; }
銆€銆€c.鍦?nbsp;resources/static/css 涓嬪垱寤?home.js 鍜?detail.js
$(function() { //鑾峰彇鐢ㄦ埛鍒楄〃 $("#list-btn").on("click", getUserListFunc); //鐢ㄦ埛璇︽儏 $(document).on("click", ".detail-btn",function(){ let id = $(this).attr(鈥榙ata-id鈥?span style="color: #000000;">); location.href = "detail?id=" + id; }); //鍒犻櫎鐢ㄦ埛 $(document).on("click", ".delete-btn",function(){ let id = $(this).attr(鈥榙ata-id鈥?span style="color: #000000;">); $.ajax({ url: "user/delete/" + id, success: function(data){ alert("鍒犻櫎鎴愬姛锛?); getUserListFunc(); }, error: function(e){ alert("绯荤粺閿欒锛?); }, }) }); //娣诲姞鐢ㄦ埛 $("#add-btn").on("click",function(){ location.href = "detail"; }); }); //鍑芥暟锛氳幏鍙栫敤鎴峰垪琛?/span> let getUserListFunc = function() { $.ajax({ url: "user/list", data: { currentPage: 1, pageSize: 100 }, success: function (data) { if (data) { $("#list-table").empty(); $("#list-table").html("<tr><th>ID</th><th>璐﹀彿</th><th>缂栧彿</th><th>鎿嶄綔</th></tr>"); let userArray = data.records; for (let i in userArray) { let user = userArray[i]; let id = user.id; let userName = user.userName; let userCode = user.userCode let trTemplate = `<tr> <th>${id}</th> <th>${userName}</th> <th>${userCode}</th> <th> <button class="detail-btn" data-id="${id}">璇︽儏</button> <button class="delete-btn" data-id="${id}">鍒犻櫎</button> </th> </tr>`; $("#list-table").append(trTemplate); } } }, error: function (e) { console.log("绯荤粺閿欒锛?); }, }) }
$(function() { //鍔犺浇 let id = $("#id").val(); if(id){ $.ajax({ url: "user/get/" + id, success: function(data){ if(data){ let userName = data.userName; let userCode = data.userCode; $("#username-input").val(userName); $("#usercode-input").val(userCode); }else{ alert("绯荤粺閿欒锛?); } }, error: function(e){ alert("绯荤粺閿欒锛?); }, }) } //鑾峰彇鐢ㄦ埛鍒楄〃 $("#save-btn").on("click",function(){ let userName = $("#username-input").val(); if(! userName){ alert("鐢ㄦ埛鍚嶄笉鑳戒负绌?); return; } let userCode = $("#usercode-input").val(); if(! userCode){ alert("缂栧彿涓嶈兘涓虹┖"); return; } let user; let url; //淇敼 if(id){ url = "user/update"; user = { userName: userName, userCode: userCode, id: id }; //娣诲姞 }else{ url = "user/add"; user = { userName: userName, userCode: userCode }; } $.ajax({ url: url, type: "POST", data: user, success: function(data){ alert("淇濆瓨鎴愬姛锛?); }, error: function(e){ alert("绯荤粺閿欒锛?); }, }) }); //娣诲姞鐢ㄦ埛 $("#close-btn").on("click",function(){ location.href = "home"; }); });
銆€銆€d.璁块棶 http://localhost:8080 杩涜娴嬭瘯
以上是关于Spring Boot 鏁村悎 Mybatis-Plus的主要内容,如果未能解决你的问题,请参考以下文章