❤【爆肝万字】手把手教你SpringBoot+MyBatis+jQuery+HTML5从0开始写网页一学就会!(内附源码)❤

x33g5p2x  于2021-11-22 转载在 HTML5  
字(9.3k)|赞(0)|评价(0)|浏览(735)

今天带给大家的是SpringBoot+MyBatis+jQuery+HTML5+CSS简单实现前后端交互,保证干货满满,看完你就可以动手写你自己的程序!

首先得需要你创建一个SpringBoot项目,具体怎么创建这里久不多说啦。

其次,你的创建一张这样一张表用于连接测试功能,很简单的一张表,当然只是用于功能测试,实际业务中可没有这么简单的表哟。

创建完成以后你pom.xm文件中的中应该有如下引入依赖:

  1. <!--springWeb-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!--本地测试-->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-test</artifactId>
  10. <scope>test</scope>
  11. </dependency>
  12. <!--mybatis-->
  13. <dependency>
  14. <groupId>org.mybatis.spring.boot</groupId>
  15. <artifactId>mybatis-spring-boot-starter</artifactId>
  16. <version>2.1.3</version>
  17. </dependency>
  18. <!--mysql-->
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <scope>5.1.38</scope>
  23. </dependency>
  24. <!--热部署-->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-devtools</artifactId>
  28. <optional>true</optional>
  29. </dependency>
  30. <!--thymeleaf-->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  34. </dependency>
  35. <!--jquery依赖-->
  36. <dependency>
  37. <groupId>org.webjars</groupId>
  38. <artifactId>jquery</artifactId>
  39. <version>3.5.1</version>
  40. </dependency>

依赖写入以后,记得在点击此刻出现在你IDEA界面右上角的刷新按钮哟。

注入依赖以后,在你的项目中src目录下的resources目录下建立application.yml文件,当然你也可以使用application.properties来进行配置,但是这里我们更加推荐通过.yml文件对其进行配置,因为它的属性结构可以让参数看起来更加简洁清晰。配置如下:

  1. spring:
  2. #thymeleaf页面缓存关闭
  3. thymeleaf:
  4. cache: false
  5. datasource:
  6. username: root
  7. password: root
  8. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
  9. driver-class-name: com.mysql.cj.jdbc.Driver
  10. # 开发环境配置
  11. server:
  12. # 服务器的HTTP端口,默认为80
  13. port: 1717
  14. servlet:
  15. # 应用的访问路径
  16. context-path: /
  17. tomcat:
  18. # tomcat的URI编码
  19. uri-encoding: UTF-8
  20. # tomcat最大线程数,默认为200
  21. max-threads: 800
  22. # Tomcat启动初始化的线程数,默认值25
  23. min-spare-threads: 30
  24. # 日志配置
  25. logging:
  26. level:
  27. com.gantiexia: debug
  28. org.springframework: warn
  29. # MyBatis
  30. mybatis:
  31. # 搜索指定包别名
  32. typeAliasesPackage: com.gantiexia.**.entity
  33. # 配置mapper的扫描,找到所有的mapper.xml映射文件
  34. mapperLocations: classpath*:mapper/**/*.xml

注意! 上面spring的配置中,datasource要写入你自己数据库的连接参数,这里博主用到的MYSQL的连接方式。同样,最后MyBatis的路径配置决定了你的项目能否找到对应的承接数据库的文件。

连接数据库

在java包下创建层级目录,com/gantiexia/springboot_test,然后在springboot_test包下创建entity包,在这个包下创建实体集。

  1. package com.gantiexia.springboot_test.entity;
  2. /** * @author GanTieXia * @date 2021/8/21 4:20 */
  3. public class User {
  4. /** 用户名*/
  5. private String userName;
  6. /** 账号*/
  7. private String idNumber;
  8. /** 密码*/
  9. private String passWord;
  10. /** 年龄*/
  11. private String age;
  12. /** 性别*/
  13. private String sex;
  14. public String getUserName() {
  15. return userName;
  16. }
  17. public void setUserName(String userName) {
  18. this.userName = userName;
  19. }
  20. public String getIdNumber() {
  21. return idNumber;
  22. }
  23. public void setIdNumber(String idNumber) {
  24. this.idNumber = idNumber;
  25. }
  26. public String getPassWord() {
  27. return passWord;
  28. }
  29. public void setPassWord(String passWord) {
  30. this.passWord = passWord;
  31. }
  32. public String getAge() {
  33. return age;
  34. }
  35. public void setAge(String age) {
  36. this.age = age;
  37. }
  38. public String getSex() {
  39. return sex;
  40. }
  41. public void setSex(String sex) {
  42. this.sex = sex;
  43. }
  44. @Override
  45. public String toString() {
  46. return "User{" +
  47. "userName='" + userName + '\'' +
  48. ", idNumber='" + idNumber + '\'' +
  49. ", passWord='" + passWord + '\'' +
  50. ", age='" + age + '\'' +
  51. ", sex='" + sex + '\'' +
  52. '}';
  53. }
  54. }

实体集写好以后,紧接着我们在springboot_test包下创建层级目录:mapper/user,然后再在user包下创建命为UserMapper的接口:

  1. package com.gantiexia.springboot_test.mapper.user;
  2. import com.gantiexia.springboot_test.entity.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. /** * @author GanTieXia * @date 2021/8/21 4:30 */
  6. @Mapper
  7. public interface UserMapper {
  8. /** * 获取信息 * * @return */
  9. List<User> getMessage();
  10. }

然后在resources目录下,创建层级目录mapper/user,在user包下面创建UserMapper.xml用于写我们的数据库查询语句。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.gantiexia.springboot_test.mapper.user.UserMapper">
  4. <select id="getMessage" resultType="com.gantiexia.springboot_test.entity.User">
  5. select userName,idNumber,passWord,age,sex from t_user;
  6. </select>
  7. </mapper>

注意! 此处的namespace对应的是你的mapper接口所在的路径,各个namespace之间相互独立不影响。

好了,到此我们就得写个测试类看这个dao接口(又称mapper)能否连接数据库,接下来在test包下,按如下路径创建测试类,创建如下路径下的文件:

编写测试类:

  1. package com.gantiexia.springboot_test;
  2. import com.gantiexia.springboot_test.entity.User;
  3. import com.gantiexia.springboot_test.service.UserService;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import java.util.List;
  8. @SpringBootTest
  9. class SpringbootUserCtrlApplicationTests {
  10. @Autowired
  11. private UserService userService;
  12. @Test
  13. public void getAllMessage(){
  14. List<User> list = userService.getMessage();
  15. for(User user : list){
  16. System.out.println(user);
  17. }
  18. }
  19. }

运行结果如下:

至此成功由代码获取到数据库中的数据。

编写业务功能模块代码

按照如下路径创建文件,注意,UserService接口在service目录下。

编写service目录下的UserService接口代码:

  1. package com.gantiexia.springboot_test.service;
  2. import com.gantiexia.springboot_test.entity.User;
  3. import java.util.List;
  4. /** * @author GanTieXia * @date 2021/8/21 4:36 */
  5. public interface UserService {
  6. /** * 获取信息 * * @return */
  7. List<User> getMessage();
  8. }

编写service目录下的serviceImpl包下的UserServiceImpl类代码:

  1. package com.gantiexia.springboot_test.service.serviceImpl;
  2. import com.gantiexia.springboot_test.entity.User;
  3. import com.gantiexia.springboot_test.mapper.user.UserMapper;
  4. import com.gantiexia.springboot_test.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import java.util.List;
  9. /** * @author GanTieXia * @date 2021/8/21 4:36 */
  10. @Service
  11. @Transactional
  12. public class UserServiceImpl implements UserService {
  13. @Autowired
  14. private UserMapper userMapper;
  15. /** * 获取所有信息 * * @return */
  16. @Override
  17. public List<User> getMessage() {
  18. return userMapper.getMessage();
  19. }
  20. }

然后创建如下层级路径以及路径下的文件:

编写controller路径下的UserCtrl文件:

  1. package com.gantiexia.springboot_test.controller;
  2. import com.gantiexia.springboot_test.entity.User;
  3. import com.gantiexia.springboot_test.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import java.util.List;
  9. /** * @author GanTieXia * @date 2021/8/20 23:06 */
  10. @Controller
  11. @RequestMapping("firstTest")
  12. public class UserCtrl {
  13. @Autowired
  14. private UserService userService;
  15. @RequestMapping("/getMessage")
  16. @ResponseBody
  17. public List<User> getMessage(){
  18. return userService.getMessage();
  19. }
  20. @RequestMapping("/testMyFirstPage")
  21. public String toPage(){
  22. return "/user/testPage";
  23. }
  24. }

接下来就要去写前端页面了,这里我们只写一个简单的样式,以供参考。

按照如下路径在resources目录下创建你的HTML5页面:

接下来我们编写testPage.html页面:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>SpringBoot</title>
  6. </head>
  7. <style> #picture { width: 50px; height: 50px; border-radius: 50%; background-color: red; position: relative; left: -25px; } #showPicture{ width: 50px; height: 50px; border-radius: 50%; } </style>
  8. <body>
  9. <div>
  10. <div style="width: 100%;background-color: cornflowerblue;height: 50px">
  11. <div id="picture" style="float: right;">
  12. <img id="showPicture" src="/picture/picture.jpg">
  13. </div>
  14. <div style="float: right">
  15. <p style="width: 150px;">用户名:<label id="userName"></label></p>
  16. </div>
  17. <div style="float: right">
  18. <p style="width: 150px;">账号:<label id="idNumber"></label></p>
  19. </div>
  20. </div>
  21. </div>
  22. </body>
  23. <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
  24. <script> // 这里我们随便加载一下数据,大家知道原理就好啦 $.ajax({ type:"post", url: "/firstTest/getMessage", async:false, success:function (data) { for(var i=0;i<data.length;i++){ $("#userName").html(data[0].userName) $("#idNumber").html(data[0].idNumber) } } }) </script>
  25. </html>

注意! 此处的中我们用到了jQuery,它的使用需要你去下载相应的js文件,放在对应的包里,这里为了大家能达到效果,建议大家将这个文件放在和我一样的路径下:

为了展示头像效果,我在static包下创建了一个picture文件夹,用来装待会我们要显示在前端的图片,你们也可以先随便放一张图片在这里,注意图片路径和图片名字得和我一样, 因为前端引入的时候输入它的路径了,你也可以在前端页面中将图片改成你自己的图片路径:

相信大家前面看了那么多遍这个文件,一定好奇这个类是干嘛的吧!

他就是我们springBoot项目中的启动类啦,这个文件我是放在gantiexia这个文件的路径下面的, 因为它能够读取到同级以及同级子目录下的文件。这样无论你是由多少个包像springboot_test、springboot_testOne、springboot_testThree等等等它都能识别到。我们来看看它长什么样子:

  1. package com.gantiexia;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /** * @author GanTieXia * @date 2021/8/20 23:06 */
  5. @SpringBootApplication
  6. public class SpringbootTestApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringbootTestApplication.class, args);
  9. System.out.println("springBoot项目启动成功...");
  10. }
  11. }

当然我自己改动了一下的哈。我们找到这个类,右键运行起这个类:

在访问前端网页的时候,要确保你的数据库能够正常连接。

接下来我们打开网页,输入刚才我们控制器中定义的url地址,端口号为我们application.yml文件中server下定义的port: 1717(可随意更改,只要不冲突就好)。注意这里我们前端页面Ajax中取得是数据库中得第一条数据没加任何得业务逻辑。

输入网址:localhost:1717/firstTest/testMyFirstPage

效果图:

到此,数据库-后端-前端全部打通,如果有不懂的,欢迎大家留言,我会一一解答。

今天的分享就到这里,觉得博主的分享有用的记得一键三连,谢谢大家,我们下期再见~

相关文章

最新文章

更多