一、SSM整合的步骤

0)建库,建表

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : zar
  4. Source Server Type : MySQL
  5. Source Server Version : 80022
  6. Source Host : localhost:3306
  7. Source Schema : ssmusers
  8. Target Server Type : MySQL
  9. Target Server Version : 80022
  10. File Encoding : 65001
  11. Date: 26/06/2021 16:26:14
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. CREATE DATABASE IF NOT EXISTS `ssmuser` DEFAULT CHARACTER SET utf8;
  16. USE `ssmuser`;
  17. -- ----------------------------
  18. -- Table structure for user
  19. -- ----------------------------
  20. DROP TABLE IF EXISTS `user`;
  21. CREATE TABLE `user` (
  22. `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
  23. `card_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  24. `card_no` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  25. `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  26. `user_sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  27. `user_age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  28. `user_role` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  29. PRIMARY KEY (`user_id`) USING BTREE
  30. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;
  31. -- ----------------------------
  32. -- Records of user
  33. -- ----------------------------
  34. INSERT INTO `user` VALUES ('15968162087363060', '身份证', '114264195202156467', '张三', '男', '30', '办事人员和有关人员');
  35. INSERT INTO `user` VALUES ('15968162346981977', '护照', 'A32532654', '李四', '男', '29', '不便分类的其他从业人员');
  36. INSERT INTO `user` VALUES ('15968162893439470', '身份证', '112344198709094532', '王五', '男', '31', '农、林、牧、渔、水利业生产人员');
  37. INSERT INTO `user` VALUES ('15968163245457143', '身份证', '453234199909094532', '赵六', '男', '34', '未知');
  38. INSERT INTO `user` VALUES ('15968163514764733', '军官证', '军7657868', '钱七', '女', '23', '不便分类的其他从业人员');
  39. INSERT INTO `user` VALUES ('15968165113694372', '台湾往来大陆通行证', '43256786', '周八', '女', '48', '生产、运输设备操作人员及有关人员');
  40. INSERT INTO `user` VALUES ('15968165371931786', '港澳居民通行证', 'C98767665', '吴九', '女', '35', '不便分类的其他从业人员');
  41. INSERT INTO `user` VALUES ('15968941217553030', '身份证', '343546199801018768', '郑十', '男', '22', '军人');
  42. INSERT INTO `user` VALUES ('15968943937844616', '身份证', '445453199603025756', '冯十一', '女', '31', '不便分类的其他从业人员');
  43. INSERT INTO `user` VALUES ('15968944123869023', '护照', 'B54322654', '陈十二', '女', '39', '农、林、牧、渔、水利业生产人员');
  44. INSERT INTO `user` VALUES ('15968953962316864', '身份证', '110232199505056789', '朱十三', '女', '33', '商业、服务业人员');
  45. INSERT INTO `user` VALUES ('15968954638794962', '身份证', '110654196604079098', '孔十四', '女', '29', '生产、运输设备操作人员及有关人员');
  46. SET FOREIGN_KEY_CHECKS = 1;

1)新建Maven项目,选择webapp模板

2)修改目录

image.png

3)修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.bjpowernode</groupId>
  6. <artifactId>springmvc_006_ssm</artifactId>
  7. <version>1.0</version>
  8. <packaging>war</packaging>
  9. <!-- 集中定义依赖版本号 -->
  10. <properties>
  11. <!--单元测试的依赖-->
  12. <junit.version>4.12</junit.version>
  13. <!--spring的相关依赖-->
  14. <spring.version>5.2.5.RELEASE</spring.version>
  15. <!--注意这个依赖要与mysql版本一致>
  16. <mysql.version>8.0.27</mysql.version>
  17. <!--mybatis的相关依赖-->
  18. <mybatis.version>3.5.1</mybatis.version>
  19. <!--mybaits与spring整合的依赖-->
  20. <mybatis.spring.version>1.3.1</mybatis.spring.version>
  21. <!--mybatis支持的分页插件的依赖-->
  22. <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
  23. <!--mysql的依赖-->
  24. <mysql.version>5.1.32</mysql.version>
  25. <!--slf4j日志依赖-->
  26. <slf4j.version>1.6.4</slf4j.version>
  27. <!--阿里的数据库连接池-->
  28. <druid.version>1.1.12</druid.version>
  29. <!--分页插件的依赖-->
  30. <pagehelper.version>5.1.2</pagehelper.version>
  31. <!--JSTL的依赖(jsp的标准标签库)-->
  32. <jstl.version>1.2</jstl.version>
  33. <!--servlet的依赖-->
  34. <servlet-api.version>3.0.1</servlet-api.version>
  35. <!--jsp的依赖-->
  36. <jsp-api.version>2.0</jsp-api.version>
  37. <!--jackson的依赖,springmvc框架默认进行JSON转换的依赖工具-->
  38. <jackson.version>2.9.6</jackson.version>
  39. </properties>
  40. <dependencies>
  41. <!-- spring -->
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-context</artifactId>
  45. <version>${spring.version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-beans</artifactId>
  50. <version>${spring.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-webmvc</artifactId>
  55. <version>${spring.version}</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-jdbc</artifactId>
  60. <version>${spring.version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework</groupId>
  64. <artifactId>spring-aspects</artifactId>
  65. <version>${spring.version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-jms</artifactId>
  70. <version>${spring.version}</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.springframework</groupId>
  74. <artifactId>spring-context-support</artifactId>
  75. <version>${spring.version}</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework</groupId>
  79. <artifactId>spring-test</artifactId>
  80. <version>5.2.5.RELEASE</version>
  81. </dependency>
  82. <!-- Mybatis -->
  83. <dependency>
  84. <groupId>org.mybatis</groupId>
  85. <artifactId>mybatis</artifactId>
  86. <version>${mybatis.version}</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.mybatis</groupId>
  90. <artifactId>mybatis-spring</artifactId>
  91. <version>${mybatis.spring.version}</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>com.github.miemiedev</groupId>
  95. <artifactId>mybatis-paginator</artifactId>
  96. <version>${mybatis.paginator.version}</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>com.github.pagehelper</groupId>
  100. <artifactId>pagehelper</artifactId>
  101. <version>${pagehelper.version}</version>
  102. </dependency>
  103. <!-- MySql -->
  104. <dependency>
  105. <groupId>mysql</groupId>
  106. <artifactId>mysql-connector-java</artifactId>
  107. <version>${mysql.version}</version>
  108. </dependency>
  109. <!-- 连接池 -->
  110. <dependency>
  111. <groupId>com.alibaba</groupId>
  112. <artifactId>druid</artifactId>
  113. <version>${druid.version}</version>
  114. </dependency>
  115. <!-- junit -->
  116. <dependency>
  117. <groupId>junit</groupId>
  118. <artifactId>junit</artifactId>
  119. <version>${junit.version}</version>
  120. <scope>test</scope>
  121. </dependency>
  122. <!-- JSP相关 -->
  123. <dependency>
  124. <groupId>jstl</groupId>
  125. <artifactId>jstl</artifactId>
  126. <version>${jstl.version}</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>javax.servlet</groupId>
  130. <artifactId>javax.servlet-api</artifactId>
  131. <version>3.0.1</version>
  132. <scope>provided</scope>
  133. </dependency>
  134. <dependency>
  135. <groupId>javax.servlet</groupId>
  136. <artifactId>jsp-api</artifactId>
  137. <scope>provided</scope>
  138. <version>${jsp-api.version}</version>
  139. </dependency>
  140. <!-- Jackson Json处理工具包 -->
  141. <dependency>
  142. <groupId>com.fasterxml.jackson.core</groupId>
  143. <artifactId>jackson-databind</artifactId>
  144. <version>${jackson.version}</version>
  145. </dependency>
  146. </dependencies>
  147. <!-- 插件配置 -->
  148. <build>
  149. <plugins>
  150. <plugin>
  151. <groupId>org.apache.maven.plugins</groupId>
  152. <artifactId>maven-compiler-plugin</artifactId>
  153. <configuration>
  154. <source>1.8</source>
  155. <target>1.8</target>
  156. <encoding>UTF-8</encoding>
  157. </configuration>
  158. </plugin>
  159. </plugins>
  160. <!--识别所有的配置文件-->
  161. <resources>
  162. <resource>
  163. <directory>src/main/java</directory>
  164. <includes>
  165. <include>**/*.properties</include>
  166. <include>**/*.xml</include>
  167. </includes>
  168. <filtering>false</filtering>
  169. </resource>
  170. <resource>
  171. <directory>src/main/resources</directory>
  172. <includes>
  173. <include>**/*.properties</include>
  174. <include>**/*.xml</include>
  175. </includes>
  176. <filtering>false</filtering>
  177. </resource>
  178. </resources>
  179. </build>
  180. </project>

4)添加jdbc.properties属性文件

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
  3. jdbc.username=root
  4. jdbc.password=123456
  1. jdbc.driverClassName=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  3. jdbc.username=root
  4. jdbc.password=123456

5)添加SqlMapConfig.xml文件(使用模板)

  1. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  2. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!--设置日志输出语句,显示相应操作的sql语名-->
  5. <settings>
  6. <setting name="logImpl" value="STDOUT_LOGGING"/>
  7. </settings>
  8. </configuration>

6)添加applicationContext_mapper.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 https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--读取属性文件-->
  7. <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  8. <!--配置数据源-->
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  10. <property name="driverClassName" value="${jdbc.driverClassName}"></property>
  11. <property name="url" value="${jdbc.url}"></property>
  12. <property name="username" value="${jdbc.username}"></property>
  13. <property name="password" value="${jdbc.password}"></property>
  14. </bean>
  15. <!--配置SqlSessionFactoryBean-->
  16. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  17. <!--配置数据源-->
  18. <property name="dataSource" ref="dataSource"></property>
  19. <!--配置SqlMapConfig.xml核心配置-->
  20. <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
  21. <!--注册实体类-->
  22. <property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
  23. </bean>
  24. <!--注册mapper.xml文件-->
  25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <property name="basePackage" value="com.bjpowernode.mapper"></property>
  27. </bean>
  28. </beans>

7)添加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" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7. <!--添加包扫描-->
  8. <context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
  9. <!--添加事务管理器-->
  10. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  11. <!--切记切记:配置数据源-->
  12. <property name="dataSource" ref="dataSource"></property>
  13. </bean>
  14. <!--配置事务切面-->
  15. <tx:advice id="myadvice" transaction-manager="transactionManager">
  16. <tx:attributes>
  17. <tx:method name="*select*" read-only="true"/>
  18. <tx:method name="*find*" read-only="true"/>
  19. <tx:method name="*serach*" read-only="true"/>
  20. <tx:method name="*get*" read-only="true"/>
  21. <tx:method name="*insert*" propagation="REQUIRED"/>
  22. <tx:method name="*add*" propagation="REQUIRED"/>
  23. <tx:method name="*save*" propagation="REQUIRED"/>
  24. <tx:method name="*set*" propagation="REQUIRED"/>
  25. <tx:method name="*update*" propagation="REQUIRED"/>
  26. <tx:method name="*change*" propagation="REQUIRED"/>
  27. <tx:method name="*modify*" propagation="REQUIRED"/>
  28. <tx:method name="*delete*" propagation="REQUIRED"/>
  29. <tx:method name="*drop*" propagation="REQUIRED"/>
  30. <tx:method name="*remove*" propagation="REQUIRED"/>
  31. <tx:method name="*clear*" propagation="REQUIRED"/>
  32. <tx:method name="*" propagation="SUPPORTS"/>
  33. </tx:attributes>
  34. </tx:advice>
  35. <!--配置切入点+绑定-->
  36. <aop:config>
  37. <aop:pointcut id="mycut" expression="execution(* com.bjpowernode.service.impl.*.*(..))"></aop:pointcut>
  38. <aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>
  39. </aop:config>
  40. </beans>

8)添加spirngmvc.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <!--添加包扫描-->
  8. <context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
  9. <!--添加注解驱动-->
  10. <mvc:annotation-driven></mvc:annotation-driven>
  11. <!--因为本项目全部是ajax请求,不需要配置视图解析器-->
  12. </beans>

image.png

9)删除web.xml文件,新建,改名,设置中文编码,并注册spirngmvc框架,并注册Spring框架

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--添加中文编码过滤器
  7. private String encoding;
  8. private boolean forceRequestEncoding;
  9. private boolean forceResponseEncoding;
  10. -->
  11. <filter>
  12. <filter-name>encode</filter-name>
  13. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  14. <init-param>
  15. <param-name>encoding</param-name>
  16. <param-value>UTF-8</param-value>
  17. </init-param>
  18. <init-param>
  19. <param-name>forceRequestEncoding</param-name>
  20. <param-value>true</param-value>
  21. </init-param>
  22. <init-param>
  23. <param-name>forceResponseEncoding</param-name>
  24. <param-value>true</param-value>
  25. </init-param>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>encode</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>
  31. <!--注册SpringMVC框架-->
  32. <servlet>
  33. <servlet-name>springmvc</servlet-name>
  34. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  35. <init-param>
  36. <param-name>contextConfigLocation</param-name>
  37. <param-value>classpath:springmvc.xml</param-value>
  38. </init-param>
  39. </servlet>
  40. <servlet-mapping>
  41. <servlet-name>springmvc</servlet-name>
  42. <url-pattern>/</url-pattern>
  43. </servlet-mapping>
  44. <!--注册Spring框架,目的就是启动spring容器-->
  45. <listener>
  46. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  47. </listener>
  48. <context-param>
  49. <param-name>contextConfigLocation</param-name>
  50. <param-value>classpath:applicationContext_*.xml</param-value>
  51. </context-param>
  52. </web-app>

10)新建实体类user

  1. package com.bjpowernode.pojo;
  2. /**
  3. *
  4. */
  5. public class User {
  6. private String userId;
  7. private String cardType;
  8. private String cardNo;
  9. private String userName;
  10. private String userSex;
  11. private String userAge;
  12. private String userRole;
  13. @Override
  14. public String toString() {
  15. return "User{" +
  16. "userId='" + userId + '\'' +
  17. ", cardType='" + cardType + '\'' +
  18. ", cardNo='" + cardNo + '\'' +
  19. ", userName='" + userName + '\'' +
  20. ", userSex='" + userSex + '\'' +
  21. ", userAge='" + userAge + '\'' +
  22. ", userRole='" + userRole + '\'' +
  23. '}';
  24. }
  25. public String getUserId() {
  26. return userId;
  27. }
  28. public void setUserId(String userId) {
  29. this.userId = userId;
  30. }
  31. public String getCardType() {
  32. return cardType;
  33. }
  34. public void setCardType(String cardType) {
  35. this.cardType = cardType;
  36. }
  37. public String getCardNo() {
  38. return cardNo;
  39. }
  40. public void setCardNo(String cardNo) {
  41. this.cardNo = cardNo;
  42. }
  43. public String getUserName() {
  44. return userName;
  45. }
  46. public void setUserName(String userName) {
  47. this.userName = userName;
  48. }
  49. public String getUserSex() {
  50. return userSex;
  51. }
  52. public void setUserSex(String userSex) {
  53. this.userSex = userSex;
  54. }
  55. public String getUserAge() {
  56. return userAge;
  57. }
  58. public void setUserAge(String userAge) {
  59. this.userAge = userAge;
  60. }
  61. public String getUserRole() {
  62. return userRole;
  63. }
  64. public void setUserRole(String userRole) {
  65. this.userRole = userRole;
  66. }
  67. public User(String userId, String cardType, String cardNo, String userName, String userSex, String userAge, String userRole) {
  68. this.userId = userId;
  69. this.cardType = cardType;
  70. this.cardNo = cardNo;
  71. this.userName = userName;
  72. this.userSex = userSex;
  73. this.userAge = userAge;
  74. this.userRole = userRole;
  75. }
  76. public User() {
  77. }
  78. }

11)新建UserMapper.java接口

  1. package com.bjpowernode.mapper;
  2. import com.bjpowernode.pojo.User;
  3. import org.apache.ibatis.annotations.Param;
  4. import java.util.List;
  5. /**
  6. *
  7. */
  8. public interface UserMapper {
  9. /**
  10. * url /user/selectUserPage?userName=z&userSex=男&page=null
  11. * 参数
  12. * userName:表单中用户名称
  13. * userSex:表单中用户性别
  14. * page:提交的页码(第一次访问为null)
  15. * 结果 有数据时:
  16. * [{
  17. * "userId":"15968954638794962",
  18. * "cardType":"身份证","
  19. * cardNo":"343343554654",
  20. * "userName":"撒撒旦",
  21. * "userSex":"女",
  22. * "userAge":"29",
  23. * "userRole":"生产、运输设备操作人员及有关人员"},
  24. * {….}
  25. * ]
  26. * 无数据时:
  27. * []
  28. *
  29. * select * from user
  30. * #limit (当前页码-1)*每页条数,每页条数
  31. * limit 10,5;
  32. */
  33. List<User> selectUserPage(
  34. @Param("userName")
  35. String userName,
  36. @Param("userSex")
  37. String userSex,
  38. @Param("startRow") //算好的起始行的值
  39. int startRow);
  40. /**
  41. * url /user/createUser(参数见下面)
  42. * 参数
  43. * cardType: this.ruleForm.cardType,//证件类型
  44. * cardNo: this.ruleForm.cardNo,//证件号码
  45. * userName: this.ruleForm.userName,//用户姓名
  46. * userSex: this.ruleForm.userSex,//用户性别
  47. * userAge: this.ruleForm.userAge,//用户年龄
  48. * userRole: this.ruleForm.userRole,//用户角色
  49. * 结果 增加成功时:
  50. * 1
  51. * 增加失败时:
  52. * 0
  53. */
  54. int createUser(User user);
  55. /**
  56. * url /user/ deleteUserById?userId= 15968162087363060
  57. * 参数
  58. * userId:删除用户的id
  59. * 结果
  60. * 删除成功时:
  61. * 1
  62. * 删除失败时:
  63. * 0
  64. */
  65. int deleteUserById(String userId);
  66. /**
  67. * url /user/getRowCount?userName=z&userSex=男
  68. * 参数
  69. * userName:表单中用户名称
  70. * userSex:表单中用户性别
  71. * 结果 有数据时:
  72. * 12
  73. * 无数据时:
  74. * 0
  75. */
  76. int getRowCount(
  77. @Param("userName")
  78. String userName,
  79. @Param("userSex")
  80. String userSex);
  81. }

12)新建UserMapper.xml实现增删查所有功能,没有更新

  1. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.bjpowernode.mapper.UserMapper">
  4. <!--完成实体类与表中列名的映射
  5. private String userId;
  6. private String cardType;
  7. private String cardNo;
  8. private String userName;
  9. private String userSex;
  10. private String userAge;
  11. private String userRole;
  12. -->
  13. <resultMap id="usermap" type="user">
  14. <id property="userId" column="user_id"></id>
  15. <result property="cardType" column="card_type"></result>
  16. <result property="cardNo" column="card_no"></result>
  17. <result property="userName" column="user_name"></result>
  18. <result property="userSex" column="user_sex"></result>
  19. <result property="userAge" column="user_age"></result>
  20. <result property="userRole" column="user_role"></result>
  21. </resultMap>
  22. <!--定义全部列名-->
  23. <sql id="allColumns">
  24. user_id,card_type,card_no,user_name,user_sex,user_age,user_role
  25. </sql>
  26. <!--
  27. List<User> selectUserPage(
  28. @Param("userName")
  29. String userName,
  30. @Param("userSex")
  31. String userSex,
  32. @Param("startRow") //算好的起始行的值
  33. int startRow);
  34. -->
  35. <select id="selectUserPage" resultMap="usermap">
  36. select <include refid="allColumns"></include>
  37. from user
  38. <where>
  39. <if test="userName != null and userName != ''">
  40. and user_name like concat('%',#{userName},'%')
  41. </if>
  42. <if test="userSex != null and userSex != ''">
  43. and user_sex = #{userSex}
  44. </if>
  45. </where>
  46. limit #{startRow},5
  47. </select>
  48. <!--
  49. int createUser(User user);
  50. -->
  51. <insert id="createUser" parameterType="user">
  52. insert into user values(#{userId},#{cardType},#{cardNo},#{userName},#{userSex},#{userAge},#{userRole})
  53. </insert>
  54. <!--
  55. int deleteUserById(String userId);
  56. -->
  57. <delete id="deleteUserById" parameterType="string">
  58. delete from user where user_id = #{userId}
  59. </delete>
  60. <!--
  61. int getRowCount(
  62. @Param("userName")
  63. String userName,
  64. @Param("userSex")
  65. String userSex);
  66. -->
  67. <select id="getRowCount" resultType="int">
  68. select count(*)
  69. from user
  70. <where>
  71. <if test="userName != null and userName != ''">
  72. and user_name like concat('%',#{userName},'%')
  73. </if>
  74. <if test="userSex != null and userSex != ''">
  75. and user_sex = #{userSex}
  76. </if>
  77. </where>
  78. </select>
  79. </mapper>

13)新建service接口和实现类

  1. package com.bjpowernode.service;
  2. import com.bjpowernode.pojo.User;
  3. import java.util.List;
  4. /**
  5. *
  6. */
  7. public interface UserService {
  8. /**
  9. * url /user/selectUserPage?userName=z&userSex=男&page=null
  10. */
  11. List<User> selectUserPage(String userName,String userSex,int startRow);
  12. /**
  13. * /user/createUser(参数见下面)
  14. */
  15. int createUser(User user);
  16. /**
  17. * user/ deleteUserById?userId= 15968162087363060
  18. */
  19. int deleteUserById(String userId);
  20. /**
  21. * /user/getRowCount?userName=z&userSex=男
  22. */
  23. int getRowCount(String userName,String userSex);
  24. }
  1. package com.bjpowernode.service.impl;
  2. import com.bjpowernode.mapper.UserMapper;
  3. import com.bjpowernode.pojo.User;
  4. import com.bjpowernode.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. /**
  9. *
  10. */
  11. @Service
  12. public class UserServiceImpl implements UserService {
  13. //切记切记:一定会有数据访问层的对象
  14. @Autowired
  15. UserMapper userMapper;
  16. @Override
  17. public List<User> selectUserPage(String userName, String userSex, int startRow) {
  18. return userMapper.selectUserPage(userName,userSex,startRow);
  19. }
  20. @Override
  21. public int createUser(User user) {
  22. return userMapper.createUser(user);
  23. }
  24. @Override
  25. public int deleteUserById(String userId) {
  26. return userMapper.deleteUserById(userId);
  27. }
  28. @Override
  29. public int getRowCount(String userName, String userSex) {
  30. return userMapper.getRowCount(userName,userSex);
  31. }
  32. }

14)新建测试类,完成所有功能的测试

  1. package test;
  2. import com.bjpowernode.pojo.User;
  3. import com.bjpowernode.service.UserService;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import java.util.List;
  10. /**
  11. *
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class) //启动spring容器
  14. @ContextConfiguration(locations = {"classpath:applicationContext_mapper.xml","classpath:applicationContext_service.xml"})
  15. public class MyTest {
  16. @Autowired
  17. UserService userService;
  18. @Test
  19. public void testSelectUserPage(){
  20. List<User> list = userService.selectUserPage("三","男",0);
  21. list.forEach(user -> System.out.println(user));
  22. }
  23. @Test
  24. public void testDeleteUserById(){
  25. int num = userService.deleteUserById("15968162087363060");
  26. System.out.println(num);
  27. }
  28. @Test
  29. public void testGetRowCount(){
  30. int num = userService.getRowCount(null,"男");
  31. System.out.println(num);
  32. }
  33. @Test
  34. public void testCreateUser(){
  35. User u = new User("125412145214547846","身份证","121451245784","哈哈","男","23","工人");
  36. int num = userService.createUser(u);
  37. System.out.println("-----"+num);
  38. }
  39. }

15)新建控制器,完成所有功能

  1. package com.bjpowernode.controller;
  2. import com.bjpowernode.pojo.User;
  3. import com.bjpowernode.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.CrossOrigin;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.util.List;
  11. /**
  12. *
  13. */
  14. @CrossOrigin //在服务器端支持跨域访问
  15. @RestController //如果本类中全部都是ajax请求,则使用此注解,方法上的@ResponseBody可不写
  16. @RequestMapping("/user")
  17. public class UserController {
  18. //切记切记:一定会有业务逻辑层的对象
  19. @Autowired
  20. UserService userService;
  21. public static final int PAGE_SIZE = 5;
  22. //user/selectUserPage?userName=z&userSex=男&page=null
  23. @RequestMapping("/selectUserPage")
  24. public List<User> selectUserPage(String userName,String userSex,Integer page){
  25. //根据页码计算起始行
  26. int startRow = 0;
  27. if(page != null){
  28. startRow = (page-1) * PAGE_SIZE;
  29. }
  30. return userService.selectUserPage(userName,userSex,startRow);
  31. }
  32. ///user/getRowCount?userName=z&userSex=男
  33. @RequestMapping("/getRowCount")
  34. public int getRowCount(String userName,String userSex){
  35. return userService.getRowCount(userName,userSex);
  36. }
  37. ///user/deleteUserById?userId= 15968162087363060
  38. @RequestMapping("/deleteUserById")
  39. public int deleteUserById(String userId){
  40. return userService.deleteUserById(userId);
  41. }
  42. ///user/createUser(参数见下面)
  43. @RequestMapping("/createUser")
  44. public int createUser(User user){
  45. String userId = System.currentTimeMillis()+"";
  46. user.setUserId(userId);
  47. return userService.createUser(user);
  48. }
  49. }

16)浏览器测试功能