- 一、SSM整合的步骤
- 0)建库,建表
- 1)新建Maven项目,选择webapp模板
- 2)修改目录
- 3)修改pom.xml文件(使用老师提供)
- 4)添加jdbc.properties属性文件
- 5)添加SqlMapConfig.xml文件(使用模板)
- 6)添加applicationContext_mapper.xml文件(数据访问层的核心配置文件)
- 7)添加applicationContext_service.xml文件(业务逻辑层的核心配置文件)
- 8)添加spirngmvc.xml文件
- 9)删除web.xml文件,新建,改名,设置中文编码,并注册spirngmvc框架,并注册Spring框架
- 10)新建实体类user
- 11)新建UserMapper.java接口
- 12)新建UserMapper.xml实现增删查所有功能,没有更新
- 13)新建service接口和实现类
- 14)新建测试类,完成所有功能的测试
- 15)新建控制器,完成所有功能
- 16)浏览器测试功能
一、SSM整合的步骤
0)建库,建表
/*
Navicat Premium Data Transfer
Source Server : zar
Source Server Type : MySQL
Source Server Version : 80022
Source Host : localhost:3306
Source Schema : ssmusers
Target Server Type : MySQL
Target Server Version : 80022
File Encoding : 65001
Date: 26/06/2021 16:26:14
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
CREATE DATABASE IF NOT EXISTS `ssmuser` DEFAULT CHARACTER SET utf8;
USE `ssmuser`;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`card_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`card_no` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`user_sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`user_age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`user_role` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('15968162087363060', '身份证', '114264195202156467', '张三', '男', '30', '办事人员和有关人员');
INSERT INTO `user` VALUES ('15968162346981977', '护照', 'A32532654', '李四', '男', '29', '不便分类的其他从业人员');
INSERT INTO `user` VALUES ('15968162893439470', '身份证', '112344198709094532', '王五', '男', '31', '农、林、牧、渔、水利业生产人员');
INSERT INTO `user` VALUES ('15968163245457143', '身份证', '453234199909094532', '赵六', '男', '34', '未知');
INSERT INTO `user` VALUES ('15968163514764733', '军官证', '军7657868', '钱七', '女', '23', '不便分类的其他从业人员');
INSERT INTO `user` VALUES ('15968165113694372', '台湾往来大陆通行证', '43256786', '周八', '女', '48', '生产、运输设备操作人员及有关人员');
INSERT INTO `user` VALUES ('15968165371931786', '港澳居民通行证', 'C98767665', '吴九', '女', '35', '不便分类的其他从业人员');
INSERT INTO `user` VALUES ('15968941217553030', '身份证', '343546199801018768', '郑十', '男', '22', '军人');
INSERT INTO `user` VALUES ('15968943937844616', '身份证', '445453199603025756', '冯十一', '女', '31', '不便分类的其他从业人员');
INSERT INTO `user` VALUES ('15968944123869023', '护照', 'B54322654', '陈十二', '女', '39', '农、林、牧、渔、水利业生产人员');
INSERT INTO `user` VALUES ('15968953962316864', '身份证', '110232199505056789', '朱十三', '女', '33', '商业、服务业人员');
INSERT INTO `user` VALUES ('15968954638794962', '身份证', '110654196604079098', '孔十四', '女', '29', '生产、运输设备操作人员及有关人员');
SET FOREIGN_KEY_CHECKS = 1;
1)新建Maven项目,选择webapp模板
2)修改目录
3)修改pom.xml文件(使用老师提供)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjpowernode</groupId>
<artifactId>springmvc_006_ssm</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<!-- 集中定义依赖版本号 -->
<properties>
<!--单元测试的依赖-->
<junit.version>4.12</junit.version>
<!--spring的相关依赖-->
<spring.version>5.2.5.RELEASE</spring.version>
<!--注意这个依赖要与mysql版本一致>
<mysql.version>8.0.27</mysql.version>
<!--mybatis的相关依赖-->
<mybatis.version>3.5.1</mybatis.version>
<!--mybaits与spring整合的依赖-->
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<!--mybatis支持的分页插件的依赖-->
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<!--mysql的依赖-->
<mysql.version>5.1.32</mysql.version>
<!--slf4j日志依赖-->
<slf4j.version>1.6.4</slf4j.version>
<!--阿里的数据库连接池-->
<druid.version>1.1.12</druid.version>
<!--分页插件的依赖-->
<pagehelper.version>5.1.2</pagehelper.version>
<!--JSTL的依赖(jsp的标准标签库)-->
<jstl.version>1.2</jstl.version>
<!--servlet的依赖-->
<servlet-api.version>3.0.1</servlet-api.version>
<!--jsp的依赖-->
<jsp-api.version>2.0</jsp-api.version>
<!--jackson的依赖,springmvc框架默认进行JSON转换的依赖工具-->
<jackson.version>2.9.6</jackson.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
<version>${jsp-api.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<!-- 插件配置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<!--识别所有的配置文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
4)添加jdbc.properties属性文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
5)添加SqlMapConfig.xml文件(使用模板)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--设置日志输出语句,显示相应操作的sql语名-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
6)添加applicationContext_mapper.xml文件(数据访问层的核心配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--读取属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置SqlMapConfig.xml核心配置-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!--注册实体类-->
<property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
</bean>
<!--注册mapper.xml文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjpowernode.mapper"></property>
</bean>
</beans>
7)添加applicationContext_service.xml文件(业务逻辑层的核心配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--添加包扫描-->
<context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
<!--添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--切记切记:配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务切面-->
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*select*" read-only="true"/>
<tx:method name="*find*" read-only="true"/>
<tx:method name="*serach*" read-only="true"/>
<tx:method name="*get*" read-only="true"/>
<tx:method name="*insert*" propagation="REQUIRED"/>
<tx:method name="*add*" propagation="REQUIRED"/>
<tx:method name="*save*" propagation="REQUIRED"/>
<tx:method name="*set*" propagation="REQUIRED"/>
<tx:method name="*update*" propagation="REQUIRED"/>
<tx:method name="*change*" propagation="REQUIRED"/>
<tx:method name="*modify*" propagation="REQUIRED"/>
<tx:method name="*delete*" propagation="REQUIRED"/>
<tx:method name="*drop*" propagation="REQUIRED"/>
<tx:method name="*remove*" propagation="REQUIRED"/>
<tx:method name="*clear*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--配置切入点+绑定-->
<aop:config>
<aop:pointcut id="mycut" expression="execution(* com.bjpowernode.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>
</aop:config>
</beans>
8)添加spirngmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!--添加包扫描-->
<context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
<!--添加注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--因为本项目全部是ajax请求,不需要配置视图解析器-->
</beans>
9)删除web.xml文件,新建,改名,设置中文编码,并注册spirngmvc框架,并注册Spring框架
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--添加中文编码过滤器
private String encoding;
private boolean forceRequestEncoding;
private boolean forceResponseEncoding;
-->
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--注册SpringMVC框架-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--注册Spring框架,目的就是启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_*.xml</param-value>
</context-param>
</web-app>
10)新建实体类user
package com.bjpowernode.pojo;
/**
*
*/
public class User {
private String userId;
private String cardType;
private String cardNo;
private String userName;
private String userSex;
private String userAge;
private String userRole;
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", cardType='" + cardType + '\'' +
", cardNo='" + cardNo + '\'' +
", userName='" + userName + '\'' +
", userSex='" + userSex + '\'' +
", userAge='" + userAge + '\'' +
", userRole='" + userRole + '\'' +
'}';
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public User(String userId, String cardType, String cardNo, String userName, String userSex, String userAge, String userRole) {
this.userId = userId;
this.cardType = cardType;
this.cardNo = cardNo;
this.userName = userName;
this.userSex = userSex;
this.userAge = userAge;
this.userRole = userRole;
}
public User() {
}
}
11)新建UserMapper.java接口
package com.bjpowernode.mapper;
import com.bjpowernode.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
*/
public interface UserMapper {
/**
* url /user/selectUserPage?userName=z&userSex=男&page=null
* 参数
* userName:表单中用户名称
* userSex:表单中用户性别
* page:提交的页码(第一次访问为null)
* 结果 有数据时:
* [{
* "userId":"15968954638794962",
* "cardType":"身份证","
* cardNo":"343343554654",
* "userName":"撒撒旦",
* "userSex":"女",
* "userAge":"29",
* "userRole":"生产、运输设备操作人员及有关人员"},
* {….}
* ]
* 无数据时:
* []
*
* select * from user
* #limit (当前页码-1)*每页条数,每页条数
* limit 10,5;
*/
List<User> selectUserPage(
@Param("userName")
String userName,
@Param("userSex")
String userSex,
@Param("startRow") //算好的起始行的值
int startRow);
/**
* url /user/createUser(参数见下面)
* 参数
* cardType: this.ruleForm.cardType,//证件类型
* cardNo: this.ruleForm.cardNo,//证件号码
* userName: this.ruleForm.userName,//用户姓名
* userSex: this.ruleForm.userSex,//用户性别
* userAge: this.ruleForm.userAge,//用户年龄
* userRole: this.ruleForm.userRole,//用户角色
* 结果 增加成功时:
* 1
* 增加失败时:
* 0
*/
int createUser(User user);
/**
* url /user/ deleteUserById?userId= 15968162087363060
* 参数
* userId:删除用户的id
* 结果
* 删除成功时:
* 1
* 删除失败时:
* 0
*/
int deleteUserById(String userId);
/**
* url /user/getRowCount?userName=z&userSex=男
* 参数
* userName:表单中用户名称
* userSex:表单中用户性别
* 结果 有数据时:
* 12
* 无数据时:
* 0
*/
int getRowCount(
@Param("userName")
String userName,
@Param("userSex")
String userSex);
}
12)新建UserMapper.xml实现增删查所有功能,没有更新
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.mapper.UserMapper">
<!--完成实体类与表中列名的映射
private String userId;
private String cardType;
private String cardNo;
private String userName;
private String userSex;
private String userAge;
private String userRole;
-->
<resultMap id="usermap" type="user">
<id property="userId" column="user_id"></id>
<result property="cardType" column="card_type"></result>
<result property="cardNo" column="card_no"></result>
<result property="userName" column="user_name"></result>
<result property="userSex" column="user_sex"></result>
<result property="userAge" column="user_age"></result>
<result property="userRole" column="user_role"></result>
</resultMap>
<!--定义全部列名-->
<sql id="allColumns">
user_id,card_type,card_no,user_name,user_sex,user_age,user_role
</sql>
<!--
List<User> selectUserPage(
@Param("userName")
String userName,
@Param("userSex")
String userSex,
@Param("startRow") //算好的起始行的值
int startRow);
-->
<select id="selectUserPage" resultMap="usermap">
select <include refid="allColumns"></include>
from user
<where>
<if test="userName != null and userName != ''">
and user_name like concat('%',#{userName},'%')
</if>
<if test="userSex != null and userSex != ''">
and user_sex = #{userSex}
</if>
</where>
limit #{startRow},5
</select>
<!--
int createUser(User user);
-->
<insert id="createUser" parameterType="user">
insert into user values(#{userId},#{cardType},#{cardNo},#{userName},#{userSex},#{userAge},#{userRole})
</insert>
<!--
int deleteUserById(String userId);
-->
<delete id="deleteUserById" parameterType="string">
delete from user where user_id = #{userId}
</delete>
<!--
int getRowCount(
@Param("userName")
String userName,
@Param("userSex")
String userSex);
-->
<select id="getRowCount" resultType="int">
select count(*)
from user
<where>
<if test="userName != null and userName != ''">
and user_name like concat('%',#{userName},'%')
</if>
<if test="userSex != null and userSex != ''">
and user_sex = #{userSex}
</if>
</where>
</select>
</mapper>
13)新建service接口和实现类
package com.bjpowernode.service;
import com.bjpowernode.pojo.User;
import java.util.List;
/**
*
*/
public interface UserService {
/**
* url /user/selectUserPage?userName=z&userSex=男&page=null
*/
List<User> selectUserPage(String userName,String userSex,int startRow);
/**
* /user/createUser(参数见下面)
*/
int createUser(User user);
/**
* user/ deleteUserById?userId= 15968162087363060
*/
int deleteUserById(String userId);
/**
* /user/getRowCount?userName=z&userSex=男
*/
int getRowCount(String userName,String userSex);
}
package com.bjpowernode.service.impl;
import com.bjpowernode.mapper.UserMapper;
import com.bjpowernode.pojo.User;
import com.bjpowernode.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*
*/
@Service
public class UserServiceImpl implements UserService {
//切记切记:一定会有数据访问层的对象
@Autowired
UserMapper userMapper;
@Override
public List<User> selectUserPage(String userName, String userSex, int startRow) {
return userMapper.selectUserPage(userName,userSex,startRow);
}
@Override
public int createUser(User user) {
return userMapper.createUser(user);
}
@Override
public int deleteUserById(String userId) {
return userMapper.deleteUserById(userId);
}
@Override
public int getRowCount(String userName, String userSex) {
return userMapper.getRowCount(userName,userSex);
}
}
14)新建测试类,完成所有功能的测试
package test;
import com.bjpowernode.pojo.User;
import com.bjpowernode.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
*
*/
@RunWith(SpringJUnit4ClassRunner.class) //启动spring容器
@ContextConfiguration(locations = {"classpath:applicationContext_mapper.xml","classpath:applicationContext_service.xml"})
public class MyTest {
@Autowired
UserService userService;
@Test
public void testSelectUserPage(){
List<User> list = userService.selectUserPage("三","男",0);
list.forEach(user -> System.out.println(user));
}
@Test
public void testDeleteUserById(){
int num = userService.deleteUserById("15968162087363060");
System.out.println(num);
}
@Test
public void testGetRowCount(){
int num = userService.getRowCount(null,"男");
System.out.println(num);
}
@Test
public void testCreateUser(){
User u = new User("125412145214547846","身份证","121451245784","哈哈","男","23","工人");
int num = userService.createUser(u);
System.out.println("-----"+num);
}
}
15)新建控制器,完成所有功能
package com.bjpowernode.controller;
import com.bjpowernode.pojo.User;
import com.bjpowernode.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
*
*/
@CrossOrigin //在服务器端支持跨域访问
@RestController //如果本类中全部都是ajax请求,则使用此注解,方法上的@ResponseBody可不写
@RequestMapping("/user")
public class UserController {
//切记切记:一定会有业务逻辑层的对象
@Autowired
UserService userService;
public static final int PAGE_SIZE = 5;
//user/selectUserPage?userName=z&userSex=男&page=null
@RequestMapping("/selectUserPage")
public List<User> selectUserPage(String userName,String userSex,Integer page){
//根据页码计算起始行
int startRow = 0;
if(page != null){
startRow = (page-1) * PAGE_SIZE;
}
return userService.selectUserPage(userName,userSex,startRow);
}
///user/getRowCount?userName=z&userSex=男
@RequestMapping("/getRowCount")
public int getRowCount(String userName,String userSex){
return userService.getRowCount(userName,userSex);
}
///user/deleteUserById?userId= 15968162087363060
@RequestMapping("/deleteUserById")
public int deleteUserById(String userId){
return userService.deleteUserById(userId);
}
///user/createUser(参数见下面)
@RequestMapping("/createUser")
public int createUser(User user){
String userId = System.currentTimeMillis()+"";
user.setUserId(userId);
return userService.createUser(user);
}
}
16)浏览器测试功能