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。

posted @ 2020-07-14 10:35  地狱小飞车  阅读(490)  评论(0)    收藏  举报