VueJS ajax综合案例

- 日理万妓 2022-01-22 03:39 356阅读 0赞

一、目录结构和注意事项

1.目录结构

在这里插入图片描述

2.注意事项

(1)data.html里面的Vue

在这里插入图片描述

(2)箭头函数

在这里插入图片描述

二、引入依赖

在这里插入图片描述

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.william</groupId>
  6. <artifactId>vue_demo_01</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>vue_demo_01 Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. <!--提取版本号-->
  17. <spring.version>5.0.2.RELEASE</spring.version>
  18. <spring.security.version>5.0.1.RELEASE</spring.security.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-context</artifactId>
  25. <version>${spring.version}</version>
  26. </dependency>
  27. </dependencies>
  28. </dependencyManagement>
  29. <dependencies>
  30. <!-- spring相关的jar包 -->
  31. <!-- 容器 -->
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-context</artifactId>
  35. </dependency>
  36. <!-- 事务 -->
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-tx</artifactId>
  40. <version>${spring.version}</version>
  41. </dependency>
  42. <!-- JDBC -->
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-jdbc</artifactId>
  46. <version>${spring.version}</version>
  47. </dependency>
  48. <!-- 测试 -->
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-test</artifactId>
  52. <version>${spring.version}</version>
  53. </dependency>
  54. <!-- springMVC -->
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-webmvc</artifactId>
  58. <version>${spring.version}</version>
  59. </dependency>
  60. <!-- mybatis -->
  61. <dependency>
  62. <groupId>org.mybatis</groupId>
  63. <artifactId>mybatis</artifactId>
  64. <version>3.4.5</version>
  65. </dependency>
  66. <!-- mybatis与Spring整合 -->
  67. <dependency>
  68. <groupId>org.mybatis</groupId>
  69. <artifactId>mybatis-spring</artifactId>
  70. <version>1.3.1</version>
  71. </dependency>
  72. <!-- AOP切面 -->
  73. <dependency>
  74. <groupId>org.aspectj</groupId>
  75. <artifactId>aspectjweaver</artifactId>
  76. <version>1.8.7</version>
  77. </dependency>
  78. <!-- 数据源 -->
  79. <dependency>
  80. <groupId>com.alibaba</groupId>
  81. <artifactId>druid</artifactId>
  82. <version>1.1.9</version>
  83. </dependency>
  84. <!-- 单元测试 -->
  85. <dependency>
  86. <groupId>junit</groupId>
  87. <artifactId>junit</artifactId>
  88. <version>4.12</version>
  89. <scope>test</scope>
  90. </dependency>
  91. <!-- servletAPI -->
  92. <!-- JSP应用 -->
  93. <dependency>
  94. <groupId>javax.servlet</groupId>
  95. <artifactId>jsp-api</artifactId>
  96. <version>2.0</version>
  97. <scope>provided</scope>
  98. </dependency>
  99. <!-- servlet应用 -->
  100. <dependency>
  101. <groupId>javax.servlet</groupId>
  102. <artifactId>servlet-api</artifactId>
  103. <version>2.5</version>
  104. <scope>provided</scope>
  105. </dependency>
  106. <!-- 日志记录工具 -->
  107. <dependency>
  108. <groupId>log4j</groupId>
  109. <artifactId>log4j</artifactId>
  110. <version>1.2.17</version>
  111. </dependency>
  112. <dependency>
  113. <groupId>org.apache.logging.log4j</groupId>
  114. <artifactId>log4j-api</artifactId>
  115. <version>2.10.0</version>
  116. </dependency>
  117. <dependency>
  118. <groupId>org.apache.logging.log4j</groupId>
  119. <artifactId>log4j-core</artifactId>
  120. <version>2.10.0</version>
  121. </dependency>
  122. <dependency>
  123. <groupId>org.apache.logging.log4j</groupId>
  124. <artifactId>log4j-web</artifactId>
  125. <version>2.9.1</version>
  126. </dependency>
  127. <dependency>
  128. <groupId>org.slf4j</groupId>
  129. <artifactId>slf4j-api</artifactId>
  130. <version>1.7.25</version>
  131. </dependency>
  132. <dependency>
  133. <groupId>org.apache.logging.log4j</groupId>
  134. <artifactId>log4j-slf4j-impl</artifactId>
  135. <version>2.9.1</version>
  136. </dependency>
  137. <dependency>
  138. <groupId>org.apache.logging.log4j</groupId>
  139. <artifactId>log4j-jcl</artifactId>
  140. <version>2.9.1</version>
  141. </dependency>
  142. <!-- mysql -->
  143. <dependency>
  144. <groupId>mysql</groupId>
  145. <artifactId>mysql-connector-java</artifactId>
  146. <version>5.1.6</version>
  147. </dependency>
  148. <!-- JSTL -->
  149. <dependency>
  150. <groupId>jstl</groupId>
  151. <artifactId>jstl</artifactId>
  152. <version>1.2</version>
  153. </dependency>
  154. <dependency>
  155. <groupId>taglibs</groupId>
  156. <artifactId>standard</artifactId>
  157. <version>1.1.1</version>
  158. </dependency>
  159. <!-- 文件上传 -->
  160. <dependency>
  161. <groupId>commons-fileupload</groupId>
  162. <artifactId>commons-fileupload</artifactId>
  163. <version>1.3.1</version>
  164. </dependency>
  165. <dependency>
  166. <groupId>commons-io</groupId>
  167. <artifactId>commons-io</artifactId>
  168. <version>2.5</version>
  169. </dependency>
  170. <dependency>
  171. <groupId>c3p0</groupId>
  172. <artifactId>c3p0</artifactId>
  173. <version>0.9.1.2</version>
  174. </dependency>
  175. <!--引入json的依赖-->
  176. <dependency>
  177. <groupId>com.fasterxml.jackson.core</groupId>
  178. <artifactId>jackson-core</artifactId>
  179. <version>2.9.0</version>
  180. </dependency>
  181. <dependency>
  182. <groupId>com.fasterxml.jackson.core</groupId>
  183. <artifactId>jackson-databind</artifactId>
  184. <version>2.9.0</version>
  185. </dependency>
  186. <dependency>
  187. <groupId>com.fasterxml.jackson.core</groupId>
  188. <artifactId>jackson-annotations</artifactId>
  189. <version>2.9.0</version>
  190. </dependency>
  191. </dependencies>
  192. <build>
  193. <finalName>vue_demo_01</finalName>
  194. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  195. <plugins>
  196. <plugin>
  197. <artifactId>maven-clean-plugin</artifactId>
  198. <version>3.1.0</version>
  199. </plugin>
  200. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  201. <plugin>
  202. <artifactId>maven-resources-plugin</artifactId>
  203. <version>3.0.2</version>
  204. </plugin>
  205. <plugin>
  206. <artifactId>maven-compiler-plugin</artifactId>
  207. <version>3.8.0</version>
  208. </plugin>
  209. <plugin>
  210. <artifactId>maven-surefire-plugin</artifactId>
  211. <version>2.22.1</version>
  212. </plugin>
  213. <plugin>
  214. <artifactId>maven-war-plugin</artifactId>
  215. <version>3.2.2</version>
  216. </plugin>
  217. <plugin>
  218. <artifactId>maven-install-plugin</artifactId>
  219. <version>2.5.2</version>
  220. </plugin>
  221. <plugin>
  222. <artifactId>maven-deploy-plugin</artifactId>
  223. <version>2.8.2</version>
  224. </plugin>
  225. </plugins>
  226. </pluginManagement>
  227. </build>
  228. </project>

三、Account类

  1. package com.william.domain;
  2. /**
  3. * @author :lijunxuan
  4. * @date :Created in 2019/6/4 21:15
  5. * @description :
  6. * @version: 1.0
  7. */
  8. public class Account {
  9. private Integer id;
  10. private String name;
  11. private Float money;
  12. @Override
  13. public String toString() {
  14. return "Account{" +
  15. "id=" + id +
  16. ", name='" + name + '\'' +
  17. ", money=" + money +
  18. '}';
  19. }
  20. public Integer getId() {
  21. return id;
  22. }
  23. public void setId(Integer id) {
  24. this.id = id;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public Float getMoney() {
  33. return money;
  34. }
  35. public void setMoney(Float money) {
  36. this.money = money;
  37. }
  38. }

四、AccountDao接口

  1. package com.william.dao;
  2. import com.william.domain.Account;
  3. import java.util.List;
  4. public interface AccountDao {
  5. /**
  6. * 查询所用用户信息
  7. * @return
  8. */
  9. public List<Account> findAll();
  10. /**
  11. *根据用户ID查询
  12. * @return
  13. */
  14. Account findById(Integer id);
  15. /**
  16. * 更新用户数据
  17. * @param account
  18. */
  19. void update(Account account);
  20. /**
  21. * 添加用户数据
  22. * @param account
  23. */
  24. void insert(Account account);
  25. /**
  26. * 删除用户信息
  27. * @param id
  28. */
  29. void delete(Integer id);
  30. }

五、service

1.accountService接口

  1. package com.william.service;
  2. import com.william.domain.Account;
  3. import org.springframework.stereotype.Service;
  4. import java.util.List;
  5. public interface AccountService {
  6. /**
  7. * 查询所用用户信息
  8. * @return
  9. */
  10. public List<Account> findAll();
  11. /**
  12. * 根据ID查询用户信息
  13. * @return
  14. */
  15. Account findById(Integer id);
  16. void update(Account account);
  17. void insert(Account account);
  18. void delete(Integer id);
  19. }

2.AccountServiceImpl实现类

  1. package com.william.service.Impl;
  2. import com.william.dao.AccountDao;
  3. import com.william.domain.Account;
  4. import com.william.service.AccountService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. /**
  9. * @author :lijunxuan
  10. * @date :Created in 2019/6/4 21:21
  11. * @description :
  12. * @version: 1.0
  13. */
  14. @Service
  15. public class AccountServiceImpl implements AccountService {
  16. @Autowired
  17. AccountDao accountDao;
  18. @Override
  19. public List<Account> findAll() {
  20. return accountDao.findAll();
  21. }
  22. @Override
  23. public Account findById(Integer id) {
  24. return accountDao.findById(id);
  25. }
  26. @Override
  27. public void update(Account account) {
  28. accountDao.update(account);
  29. }
  30. @Override
  31. public void insert(Account account) {
  32. accountDao.insert(account);
  33. }
  34. @Override
  35. public void delete(Integer id) {
  36. accountDao.delete(id);
  37. }
  38. }

六、AccountDao.xml

在这里插入图片描述

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.william.dao.AccountDao">
  6. <select id="findAll" resultType="account">
  7. select * from account
  8. </select>
  9. <select id="findById" parameterType="int" resultType="account">
  10. select * from account where id =#{id}
  11. </select>
  12. <update id="update" parameterType="account">
  13. update account set name=#{name},money=#{money} where id =#{id}
  14. </update>
  15. <insert id="insert" parameterType="account">
  16. insert into account values(null,#{name},#{money})
  17. </insert>
  18. <delete id="delete" parameterType="int">
  19. delete from account where id =#{id}
  20. </delete>
  21. </mapper>

七、spring里的配置文件

目录结构

在这里插入图片描述

注意的细节

在这里插入图片描述

1.applicationContext-dao.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--引入属性文件-->
  7. <context:property-placeholder location="classpath:dp.properties"></context:property-placeholder>
  8. <!--配置数据源-->
  9. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  10. <property name="driverClassName" value="${jdbc.driver}"></property>
  11. <property name="url" value="${jdbc.url}"></property>
  12. <property name="username" value="${jdbc.user}"></property>
  13. <property name="password" value="${jdbc.password}"></property>
  14. </bean>
  15. <!--创建sqlSessionFactory对象-->
  16. <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  17. <!--引入数据源-->
  18. <property name="dataSource" ref="dataSource"></property>
  19. <!--配置别名-->
  20. <property name="typeAliasesPackage" value="com.william.domain"></property>
  21. </bean>
  22. <!--创建dao接口的动态代理对象-->
  23. <bean id="Mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  24. <property name="basePackage" value="com.william.dao"></property>
  25. </bean>
  26. </beans>

2.applicationContext-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. ">
  16. <!--开启注解扫描-->
  17. <context:component-scan base-package="com.william.service"></context:component-scan>
  18. <!--事务管理器-->
  19. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  20. <property name="dataSource" ref="dataSource"></property>
  21. </bean>
  22. <!--事务增强-->
  23. <tx:advice id="txadvice" transaction-manager="transactionManager">
  24. <tx:attributes>
  25. <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
  26. <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
  27. <tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
  28. <tx:method name="select*" read-only="true" propagation="SUPPORTS"/>
  29. <tx:method name="*" />
  30. </tx:attributes>
  31. </tx:advice>
  32. <!--配置aop切面-->
  33. <aop:config>
  34. <aop:advisor advice-ref="txadvice" pointcut="execution(* com.william.service.Impl.*.*(..))"></aop:advisor>
  35. </aop:config>
  36. </beans>

八、dp.properties

在这里插入图片描述

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/web04
  3. jdbc.user=root
  4. jdbc.password=root

九、Spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <!--开启注解扫描-->
  8. <context:component-scan base-package="com.william.controller"></context:component-scan>
  9. <!--mvc注解驱动-->
  10. <mvc:annotation-driven></mvc:annotation-driven>
  11. </beans>

十、web.xml

在这里插入图片描述

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <!--配置全局-->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring/*</param-value>
  10. </context-param>
  11. <!--配置过滤器-->
  12. <filter>
  13. <filter-name>CharacterEncodingFilter</filter-name>
  14. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  15. <init-param>
  16. <param-name>encoding</param-name>
  17. <param-value>utf-8</param-value>
  18. </init-param>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>CharacterEncodingFilter</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. <!--监听器-->
  25. <listener>
  26. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  27. </listener>
  28. <!--配置前端控制器-->
  29. <servlet>
  30. <servlet-name>DispatcherServlet</servlet-name>
  31. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  32. <init-param>
  33. <param-name>contextConfigLocation</param-name>
  34. <param-value>classpath:Spring-mvc.xml</param-value>
  35. </init-param>
  36. <load-on-startup>1</load-on-startup>
  37. </servlet>
  38. <servlet-mapping>
  39. <servlet-name>DispatcherServlet</servlet-name>
  40. <url-pattern>*.do</url-pattern>
  41. </servlet-mapping>
  42. </web-app>

十一、AccountController

目录结构

在这里插入图片描述

@RequestBody注解

在这里插入图片描述

小细节

在这里插入图片描述

  1. package com.william.controller;
  2. import com.william.domain.Account;
  3. import com.william.service.AccountService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import java.util.List;
  10. /**
  11. * @author :lijunxuan
  12. * @date :Created in 2019/6/4 21:58
  13. * @description :
  14. * @version: 1.0
  15. */
  16. @RestController
  17. @RequestMapping("/account")
  18. public class AccountController {
  19. @Autowired
  20. AccountService accountService;
  21. @RequestMapping("/findAll")
  22. public List<Account> findAll(){
  23. List<Account> accountList = accountService.findAll();
  24. return accountList;
  25. }
  26. @RequestMapping("/findById")
  27. public Account findById(Integer id){
  28. Account account =accountService.findById(id);
  29. return account;
  30. }
  31. @RequestMapping("/update")
  32. public String update(@RequestBody Account account){
  33. System.out.println(account);
  34. accountService.update(account);
  35. return "ok" ;
  36. }
  37. @RequestMapping("/insert")
  38. public String insert(@RequestBody Account account){
  39. System.out.println(account);
  40. accountService.insert(account);
  41. return "insert ok";
  42. }
  43. @RequestMapping("/delete")
  44. public String delete(Integer id){
  45. System.out.println(id);
  46. accountService.delete(id);
  47. return "delete success";
  48. }
  49. }

十二、前端页面

1.需要引入的js

在这里插入图片描述

2.data.html页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <title>AdminLTE 2 | Data Tables</title>
  7. <!-- Tell the browser to be responsive to screen width -->
  8. <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  9. <link rel="stylesheet" href="../css/bootstrap.min.css">
  10. <script src="../js/jquery-1.9.1.js"></script>
  11. <script src="../js/bootstrap.min.js"></script>
  12. </head>
  13. <body class="hold-transition skin-blue sidebar-mini">
  14. <!-- Content Wrapper. Contains page content -->
  15. <div class="content-wrapper">
  16. <!-- Main content -->
  17. <section class="content">
  18. <div class="row">
  19. <div class="col-xs-12">
  20. <div class="box" id="app">
  21. <div class="box-header">
  22. <button type="button" class="btn btn-success" @click="insertModelShow()">添加</button>
  23. </div>
  24. <div class="box-body">
  25. <!--数据表格-->
  26. <table id="data_table" class="table table-bordered table-hover">
  27. <thead>
  28. <tr>
  29. <th>id</th>
  30. <th>用户名</th>
  31. <th>余额</th>
  32. <th>操作</th>
  33. </tr>
  34. </thead>
  35. <tbody>
  36. <tr v-for="account in accountList">
  37. <td>{
  38. {account.id}}</td>
  39. <td>{
  40. {account.name}}</td>
  41. <td>{
  42. {account.money}}</td>
  43. <td>
  44. <button type="button" class="btn btn-info" @Click="updateModelShow(account.id)">修改</button>
  45. <button type="button" class="btn btn-danger" @Click="delete1(account.id)">删除</button></td>
  46. </tr>
  47. </tbody>
  48. </table>
  49. </div>
  50. <!--修改 模块-->
  51. <div id="update_modal" class="modal fade" role="dialog">
  52. <div class="modal-dialog modal-lg">
  53. <div class="modal-content">
  54. <div class="modal-header">
  55. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  56. <span aria-hidden="true">×</span></button>
  57. <h4 class="modal-title">修改--用户信息</h4>
  58. </div>
  59. <div class="modal-body">
  60. <input type="hidden" class="form-control" >
  61. <div class="box-body">
  62. <div class="box-body">
  63. <div class="form-group">
  64. <label for="inputEmail3" class="col-sm-2 control-label">账户名</label>
  65. <div class="col-sm-10">
  66. <input type="text" class="form-control" v-model="account.name" placeholder="name">
  67. </div>
  68. </div>
  69. <div class="form-group">
  70. <label for="inputPassword3" class="col-sm-2 control-label">余额</label>
  71. <div class="col-sm-10">
  72. <input type="text" class="form-control" v-model="account.money" placeholder="money">
  73. </div>
  74. </div>
  75. </div>
  76. <div class="box-footer">
  77. <button type="button" class="btn btn-outline" data-dismiss="modal">关闭</button>
  78. <button type="button" class="btn btn-outline" @click="update" data-dismiss="modal">修改</button>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. <!-- /.modal-content -->
  84. </div>
  85. <!-- /.modal-dialog -->
  86. </div>
  87. <!--添加 模块-->
  88. <div id="add_modal" class="modal fade" role="dialog">
  89. <div class="modal-dialog modal-lg">
  90. <div class="modal-content">
  91. <div class="modal-header">
  92. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  93. <span aria-hidden="true">×</span></button>
  94. <h4 class="modal-title">添加--用户信息</h4>
  95. </div>
  96. <div class="modal-body">
  97. <div class="box-body">
  98. <form class="form-horizontal">
  99. <div class="box-body">
  100. <div class="form-group">
  101. <label for="inputEmail3" class="col-sm-2 control-label">账户名称</label>
  102. <div class="col-sm-10">
  103. <input type="text" class="form-control" id="inputEmail3" placeholder="name" v-model="account.name">
  104. </div>
  105. </div>
  106. <div class="form-group">
  107. <label for="inputPassword3" class="col-sm-2 control-label">余额</label>
  108. <div class="col-sm-10">
  109. <input type="text" class="form-control" id="inputPassword3" placeholder="money" v-model="account.money">
  110. </div>
  111. </div>
  112. </div>
  113. <!-- /.box-body -->
  114. <div class="box-footer">
  115. <button type="button" class="btn btn-outline" data-dismiss="modal">关闭</button>
  116. <button type="button" class="btn btn-outline" @click="insert" data-dismiss="modal">添加</button>
  117. </div>
  118. <!-- /.box-footer -->
  119. </form>
  120. </div>
  121. </div>
  122. </div>
  123. <!-- /.modal-content -->
  124. </div>
  125. <!-- /.modal-dialog -->
  126. </div>
  127. <!-- /.box-body -->
  128. </div>
  129. <!-- /.box -->
  130. </div>
  131. <!-- /.col -->
  132. </div>
  133. <!-- /.row -->
  134. </section>
  135. <!-- /.content -->
  136. </div>
  137. </div>
  138. </body>
  139. <script src="../js/vue-v2.6.10.js"></script>
  140. <script src="../js/axios-v0.19.0.js"></script>
  141. <script type="text/javascript">
  142. new Vue({
  143. el:"#app",
  144. data:{
  145. accountList:[],
  146. account:{}
  147. },
  148. methods:{
  149. /**
  150. * 查找所有用户信息
  151. */
  152. findAll:function () {
  153. var _this=this;
  154. axios.get("/account/findAll.do").then(function (response){
  155. console.log(response.data);
  156. //this 不是vue this是axios
  157. _this.accountList=response.data;
  158. }).catch(function (err) {
  159. console.log(err);
  160. });
  161. },
  162. /**
  163. * 通过id查询用户数据
  164. * 使用v-modal进行双向绑定
  165. * @param id
  166. */
  167. updateModelShow:function(id){
  168. axios.get("/account/findById.do?id="+id).then( (res)=>{
  169. console.log(res.data);
  170. this.account=res.data;
  171. }).catch((r)=> {
  172. console.log(r);
  173. });
  174. $("#update_modal").modal("show");
  175. },
  176. /**
  177. * 更新用户数据
  178. */
  179. update:function () {
  180. axios.post("/account/update.do",this.account).then((res)=>{
  181. //console.log(res.data);
  182. this.findAll();
  183. }).catch((r)=>{
  184. console.log(r);
  185. });
  186. },
  187. /**
  188. * 通过视图层修改用户信息
  189. * 视图层再调用insert方法,添加用户信息
  190. *
  191. *
  192. */
  193. insertModelShow:function(){
  194. $("#add_modal").modal("show");
  195. this.account={};
  196. },
  197. /**
  198. * 插入用户信息
  199. */
  200. insert:function(){
  201. axios.post("/account/insert.do",this.account).then((res)=>{
  202. this.findAll();
  203. }).catch((r)=>{
  204. console.log(r);
  205. });
  206. },
  207. /**
  208. * 删除用户信息
  209. * @param id
  210. */
  211. delete1:function (id) {
  212. axios.get("/account/delete.do?id="+id).then((res)=>{
  213. this.findAll();
  214. }).catch((r)=>{
  215. console.log(r);
  216. });
  217. }
  218. },
  219. /**
  220. * 钩子函数
  221. */
  222. created:function(){
  223. this.findAll();
  224. }
  225. });
  226. </script>
  227. </html>

十三、功能实现步骤分解

1进入界面首先会加载页面查询所有用户信息

(1)在页面输入请求后进入data.html页面

在这里插入图片描述

(2)调用findAll向后端发送请求

在这里插入图片描述

(3) AccountController接收前端发送过来的请求

在这里插入图片描述

(4) 调用AccountServiceImpl实现类

在这里插入图片描述

(5)调用AccountDao

在这里插入图片描述

(6)AccountDao.xml映射文件查询数据

在这里插入图片描述

(7) 最后给前端相应数据

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,356人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HTML综合案例练习

    一、展示简历内容 可以首先看一下我们的效果,之后再思考怎么实现 ![\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6a8XAHrq-1