当前位置:   article > 正文

前端后端数据库的交互,前后端如何打通,vue+spring boot +MySQL_后端和数据库的关系

后端和数据库的关系

前端软件:Microsoft VS Code

后端软件:IntelliJ IDEA 

数据库:Navicat Premium

前端git地址:https://github.com/zouwanghua/ZWH_prodect_01.git

后端git地址:https://github.com/zouwanghua/ZWH_prodect_02.git

数据库git地址:https://github.com/zouwanghua/ZWH_prodect_03.git

1、在前端项目 config\index.js 中,找到 proxyTable 位置,修改为以下内容

  1. proxyTable: { // 注意如果不是在最后的位置,大括号外需要添加一个逗号。
  2. '/api': {
  3. target: 'http://localhost:8443',
  4. changeOrigin: true,
  5. pathRewrite: {
  6. '^/api': ''
  7. }
  8. }
  9. }

2、运行项目 npm run dev    注意地址是 localhost:8080/#/login,这边就不写出来了(具体的看git)

3、后端开发

  1. // 在 Login.vue 中,前端发送数据的代码段为
  2. methods: {
  3. login () {
  4. this.$axios
  5. .post('/login', {
  6. username: this.loginForm.username,
  7. password: this.loginForm.password
  8. })
  9. .then(successResponse => {
  10. if (successResponse.data.code === 200) {
  11. // var data = this.loginForm
  12. this.$router.replace({path: '/index'})
  13. }
  14. })
  15. .catch(failResponse => {
  16. })
  17. }
  18. }

4、后端需要接收,建 User类; 建Result 类构造 response响应码;建LoginController类对响应部分进行处理

5、同时运行前端和后端项目,访问 localhost:8080/#/login,输入用户名 admin,密码 123456,进行登录

6、前端后端打通,,接下来就是后端与数据库,引入数据库,使用 Navicat 创建数据库与表,建完记得下面打钩

7、数据库的操作完成,接下来就开始进行交互,后端项目需要配置依赖关系,修改 pom.xml,右下角出现Import Changes,点击

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.evan</groupId>
  4. <artifactId>wj</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>wj</name>
  7. <description>White Jotter - Your Mind Palace</description>
  8. <packaging>war</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.1.1.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15. <dependencies>
  16. <!-- springboot web -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- springboot tomcat 支持 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-tomcat</artifactId>
  25. <scope>provided</scope>
  26. </dependency>
  27. <!-- 热部署 -->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <optional>true</optional>
  32. </dependency>
  33. <!-- jpa-->
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-data-jpa</artifactId>
  37. </dependency>
  38. <!-- redis -->
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-data-redis</artifactId>
  42. </dependency>
  43. <!-- springboot test -->
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. <!-- thymeleaf -->
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  53. </dependency>
  54. <!-- elastic search -->
  55. <dependency>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  58. </dependency>
  59. <!-- 用了 elasticsearch 就要加这么一个,不然要com.sun.jna.Native 错误 -->
  60. <dependency>
  61. <groupId>com.sun.jna</groupId>
  62. <artifactId>jna</artifactId>
  63. <version>3.0.9</version>
  64. </dependency>
  65. <!-- thymeleaf legacyhtml5 模式支持 -->
  66. <dependency>
  67. <groupId>net.sourceforge.nekohtml</groupId>
  68. <artifactId>nekohtml</artifactId>
  69. <version>1.9.22</version>
  70. </dependency>
  71. <!-- 测试支持 -->
  72. <dependency>
  73. <groupId>junit</groupId>
  74. <artifactId>junit</artifactId>
  75. <version>4.12</version>
  76. <scope>test</scope>
  77. </dependency>
  78. <!-- tomcat的支持.-->
  79. <dependency>
  80. <groupId>org.apache.tomcat.embed</groupId>
  81. <artifactId>tomcat-embed-jasper</artifactId>
  82. <version>8.5.23</version>
  83. </dependency>
  84. <!-- mysql-->
  85. <dependency>
  86. <groupId>mysql</groupId>
  87. <artifactId>mysql-connector-java</artifactId>
  88. <version>5.1.21</version>
  89. </dependency>
  90. <!-- junit -->
  91. <dependency>
  92. <groupId>junit</groupId>
  93. <artifactId>junit</artifactId>
  94. <version> 4.12</version>
  95. </dependency>
  96. <!-- commons-lang -->
  97. <dependency>
  98. <groupId>commons-lang</groupId>
  99. <artifactId>commons-lang</artifactId>
  100. <version>2.6</version>
  101. </dependency>
  102. <!-- shiro -->
  103. <dependency>
  104. <groupId>org.apache.shiro</groupId>
  105. <artifactId>shiro-spring</artifactId>
  106. <version>1.3.2</version>
  107. </dependency>
  108. <!-- hsqldb -->
  109. <dependency>
  110. <groupId>org.hsqldb</groupId>
  111. <artifactId>hsqldb</artifactId>
  112. </dependency>
  113. </dependencies>
  114. <properties>
  115. <java.version>1.8</java.version>
  116. </properties>
  117. <build>
  118. <plugins>
  119. <plugin>
  120. <groupId>org.springframework.boot</groupId>
  121. <artifactId>spring-boot-maven-plugin</artifactId>
  122. </plugin>
  123. </plugins>
  124. </build>
  125. </project>

8、配置完依赖后,还需要配置数据库,打开src\main\resources\application.properties,修改内容

  1. server.port=8443
  2. # 注意端口、数据库名、用户名、密码要与你想使用的数据库一致。
  3. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/white_jotter?characterEncoding=UTF-8
  4. spring.datasource.username=root
  5. spring.datasource.password=123456
  6. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  7. spring.jpa.hibernate.ddl-auto = none
  8. spring.jpa.database = MYSQL
  9. spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
  10. pring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  11. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

9、完善登录控制器,修改 User 类代码以建立对数据库的映射;建立数据访问对象DAO用来操作数据库的对象(定义);建立Service对 UserDAO 进行了二次封装(操作);建立Controller登录控制器查询数据库验证登录成功与否(采取三层架构(DAO + Service + Controller))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/547412
推荐阅读
相关标签
  

闽ICP备14008679号