1. 程式人生 > 實用技巧 >springboot+mybatis實現增刪改查

springboot+mybatis實現增刪改查

開發工具IDEA

一.建立springboot專案(可以百度或者點選檢視

二.新增依賴pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4
<modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.3.1.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10
</parent> 11 <groupId>com.jzdsh</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18
<java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <!-- postgresql資料庫驅動--> 23 <dependency> 24 <groupId>org.postgresql</groupId> 25 <artifactId>postgresql</artifactId> 26 <version>42.2.5</version> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-jdbc</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-thymeleaf</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.projectlombok</groupId> 38 <artifactId>lombok</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-web</artifactId> 43 </dependency> 44 <dependency> 45 <groupId>org.mybatis.spring.boot</groupId> 46 <artifactId>mybatis-spring-boot-starter</artifactId> 47 <version>2.1.3</version> 48 </dependency> 49 <dependency> 50 <groupId>mysql</groupId> 51 <artifactId>mysql-connector-java</artifactId> 52 <scope>runtime</scope> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-starter-test</artifactId> 57 <scope>test</scope> 58 <exclusions> 59 <exclusion> 60 <groupId>org.junit.vintage</groupId> 61 <artifactId>junit-vintage-engine</artifactId> 62 </exclusion> 63 </exclusions> 64 </dependency> 65 </dependencies> 66 67 <build> 68 <plugins> 69 <plugin> 70 <groupId>org.springframework.boot</groupId> 71 <artifactId>spring-boot-maven-plugin</artifactId> 72 </plugin> 73 </plugins> 74 </build> 75 76 </project>
View Code

三.配置檔案application.properties 點選檢視更多配置

 1 server.port=8080
 2 server.servlet.context-path=/wuzhiqiang#專案名
 3 
 4 
 5 spring.datasource.url=jdbc:postgresql://10.100.55.64:5432/ruleengine#連線路徑
 6 spring.datasource.username=#使用者名稱
 7 spring.datasource.password=#密碼
 8 spring.datasource.driverClassName=org.postgresql.Driver#驅動
 9 
10 mybatis.mapperLocations=classpath:templates/mapper/**/*.xml
View Code

四.專案結構

五.上程式碼

1.controller StudentController

 1 package com.jzdsh.demo.controller;
 2 
 3 import com.jzdsh.demo.model.Student;
 4 import com.jzdsh.demo.service.StudentService;
 5 import com.jzdsh.demo.util.AjaxMessage;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 import java.util.List;
11 
12 /**
13  * @author ex_wuzhiqiang.
14  * @date 2020/7/13.
15  * @time 15:36.
16  */
17 @RestController
18 @RequestMapping("/index")
19 public class StudentController {
20     @Autowired(required = false)
21     private StudentService studentService;
22 
23     @RequestMapping("/select")
24     public AjaxMessage selectAll(){
25         AjaxMessage<Object> objectAjaxMessage = new AjaxMessage<>();
26         List<Student> list =  studentService.selectAll();
27         if (list != null){
28             objectAjaxMessage.setData(list);
29             objectAjaxMessage.setIs(true);
30             objectAjaxMessage.setMsg("success");
31         }else {
32             objectAjaxMessage.setMsg("fail");
33             objectAjaxMessage.setIs(false);
34         }
35         return objectAjaxMessage;
36     }
37 }
View Code

2.service StudentService

 1 package com.jzdsh.demo.service;
 2 
 3 import com.jzdsh.demo.model.Student;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * @author ex_wuzhiqiang.
 9  * @date 2020/7/13.
10  * @time 15:38.
11  */
12 public interface StudentService  {
13     List<Student> selectAll();
14 }
View Code

3.service impl StudentServiceImpl

 1 package com.jzdsh.demo.service.impl;
 2 
 3 import com.jzdsh.demo.dao.StudentDao;
 4 import com.jzdsh.demo.model.Student;
 5 import com.jzdsh.demo.service.StudentService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 
11 /**
12  * @author ex_wuzhiqiang.
13  * @date 2020/7/13.
14  * @time 15:38.
15  */
16 @Service
17 public class StudentServiceImpl implements StudentService {
18     @Autowired
19     private StudentDao studentDao;
20     @Override
21     public List<Student> selectAll() {
22         try {
23             return studentDao.selectAll();
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27         return null;
28     }
29 }
View Code

4.dao StudentDao

 1 package com.jzdsh.demo.dao;
 2 
 3 import com.jzdsh.demo.model.Student;
 4 import org.apache.ibatis.annotations.Mapper;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * @author ex_wuzhiqiang.
10  * @date 2020/7/13.
11  * @time 16:16.
12  */
13 @Mapper
14 public interface StudentDao {
15     List<Student> selectAll();
16 }
View Code

5.util AjaxMessage

 1 package com.jzdsh.demo.util;
 2 
 3 import lombok.Data;
 4 
 5 /**
 6  * @author ex_wuzhiqiang.
 7  * @date 2020/7/13.
 8  * @time 15:39.
 9  */
10 @Data
11 public class AjaxMessage<T> {
12     private String msg;
13     private T data;
14     private boolean is;
15     public AjaxMessage() {
16         this.msg="成功";
17         this.is=true;
18     }
19 
20     public AjaxMessage(String code, String msg, boolean is) {
21         this.msg = msg;
22         this.is = is;
23     }
24 
25     public AjaxMessage(String code, String msg, T data, boolean is) {
26         this.msg = msg;
27         this.data = data;
28         this.is = is;
29     }
30 }
View Code

6.templates/mapper/student StudentMapper.xml

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
4 <mapper namespace="com.jzdsh.demo.dao.StudentDao">
5 
6     <select id="selectAll" resultType="com.jzdsh.demo.model.Student">
7        select * from t_test_marketcount order by id asc
8     </select>
9 </mapper>
View Code

7.頁面resource/index.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4 
 5     <meta charset="UTF-8">
 6     <title>Title</title>
 7 </head>
 8 <body>
 9 <button onclick="selectButton()">
10     查詢
11 </button>
12 <table>
13     <thead>
14     <tr>
15         <th>id</th>
16         <th>姓名</th>
17         <th>年齡</th>
18     </tr>
19     </thead>
20     <tbody>
21 
22     </tbody>
23 </table>
24 <script type="text/javascript" src="./js/jquery-2.1.1.min.js"></script>
25 <script>
26     function selectButton() {
27 
28         $.ajax({
29               type:"post",
30               url:"/wuzhiqiang/index/select",
31               dataType:"json",
32               success:function (data) {
33                console.log(JSON.stringify(data));
34 
35                console.log(data.data.length
36                )
37                    var str = "";
38                   for (var i=0;i<data.data.length;i++){
39                      str +="<tr>" +
40                          "<td>" +
41                              data.data[i].id+
42                          "</td>" +
43                          "<td>" +
44                          data.data[i].name+
45                       "</td>" +
46                       "<td>" +
47                       data.data[i].age+
48                       "</td>" +
49                          "</tr>";
50                   }
51                   $("table tbody").append(str);
52 
53               }
54         })
55     }
56 </script>
57 </body>
58 </html>
View Code

六.資料庫

七.效果展示

--------------------到此查詢簡單的做完了,增刪改就差不多了,在此就不寫了。。。。。。。。。。。---------------------------------------------------

八.問題總結

1.html頁面引入了jquery,但是js報unresolved function or method $();

解決:

在idea中選擇file-settings開啟設定視窗,找到Libraries,在Language&Frameworks下面的JavaScript中

點選add,彈出視窗

點選“+”選擇要新增的jquery檔案,注意要選擇不帶min的jquery

2.application.properties問題

主要主要application.properties和application.yml的書寫格式

再一個就是對mapper.xml檔案路徑配置時,要注意路徑(直接寫在resources下直接寫檔名;如果寫在templates包下則要加包名。

如本專案路徑為templates/mapper/studetent/StudentMapper.xml:

配置為:mybatis.mapperLocations=classpath:templates/mapper/**/*.xml)

3.idea中的lombok外掛已經下載,並且在另一個專案上可以使用,但在此專案上不能使用解決辦法:

settings -> Build,Execution,Deployment -> Annotation Processors -> 把Enable annotation processing勾選

4.啟動類

 報錯:required a bean of type 'XXX.XXX.XXX' that could not be found

把主啟動類上的exclude= {DataSourceAutoConfiguration.class} 去掉,然後就可以了。

原來為什麼加這個引數?因為原來沒有在application.yml或者application.properties中配置spring.datasource.url這個屬性,所以啟動會報錯。

但是後來我配置了資料來源相關的屬性,應該把exclude= {DataSourceAutoConfiguration.class}去掉,而且spring-data-jpa是操作資料庫相關的框架,可能exculde資料來源配置導致spring不會自動掃描repository。