SSM
此案例
搭建Spring
框架基础
搭建MyBatis
Mysql+druid+pagehelper
Spring整合MyBatis 单元测试
Junit测试业务接口 这里 表示业务层以及完成
搭建SpringMVC
Rest风格(postman测试请求结果)
数据封装(json)回写数据的一种形式 jackson
Spring整合SpringMVC
Controller调用Service
表现层数据封装 、异常状态返回
项目基础结构搭建
实体类对应的数据库样式
项目结构
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>cn.yl</groupId>
<artifactId>springmvc_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- servlet 规范坐标-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- <!– spring的坐标–>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!-- springMvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- springweb坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.4.49.ALL</version>
</dependency>
<!-- 分页插件的坐标-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!-- 对应JSON的坐标-->
<!-- <dependency>-->
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
<!-- <artifactId>jackson-core</artifactId>-->
<!-- <version>2.9.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
<!-- <artifactId>jackson-annotations</artifactId>-->
<!-- <version>2.9.0</version>-->
<!-- </dependency>-->
<!-- 文件上传下载-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 导入校验jsr303规范-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- 导入校验框架实现技术-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring 整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
</dependencies>
<!-- 构建-->
<build>
<!--maven插件-->
<plugins>
<!--jdk编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!--加入对应的maven版本号码-->
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!--端口控制-->
<port>80</port>
<!--项目路径控制意味着http://localhost:8080/abc-->
<path>/</path>
<!--编码-->
<uriEncoding>UTF-8</uriEncoding>
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spring环境配置
<context:component-scan base-package="cn.yl">
Mybatis 映射配置
<!--mapper 核心标签-->
<!--namespace 名称空间-->
<mapper namespace="cn.yl.dao.UserDao">
<insert id="save" parameterType="user">
INSERT INTO user (uuid,username,password,realName,gender,birthday)values (#{uuid},#{username},#{password},#{realName},#{gender},#{birthday})
</insert>
<update id="update" parameterType="user">
UPDATE user SET username=#{username},password=#{password},realName=#{realName},gender=#{gender},birthday=#{birthday} WHERE uuid=#{uuid}
</update>
<delete id="delete" parameterType="int">
DELETE FROM user WHERE uuid=#{uuid}
</delete>
<select id="get" resultType="user" parameterType="int">
SELECT * FROM user where uuid= #{uuid}
</select>
<!-- 分页查询-->
<select id="getAll" resultType="user" >
SELECT * FROM user
</select>
<!-- 登录 -->
<select id="getByUserNameAndPassword" resultType="user">
SELECT * FROM user WHERE username=#{username} and password=#{password}
</select>
</mapper>
将参数进行对应
MyBatis核心配置
<!-- 数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注入数据源 扫描实体类 -->
<!-- 整合mybatis到 spring -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 对应的实体类包名-->
<property name="typeAliasesPackage" value="cn.yl.domain"/>
加入对对应的Pagehelper
分页插件配置与使用
<!-- 分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
MyBtais配置文件 做 sql映射的
MyBtais映射扫描
<!-- 映射扫描 加载UserDao.xml-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.yl.dao"/>
</bean>
事务管理定义
打开注解事务的事务驱动
<!-- 注解驱动-->
<tx:annotation-driven transaction-manager="txManager"/>
事务管理器 开在业务层的接口上
// 事务管理
@Transactional(readOnly = true)
public interface UserService {
@Transactional(readOnly = false)
public boolean save(User user);
@Transactional(readOnly = false)
public boolean update(User user);
@Transactional(readOnly = false)
public boolean delete(Integer uuid);
public User get(Integer uuid);
// public PageInfo getAll(int page, int size);
public PageInfo<User> getAll(int page, int size) ;
// 数据层 起的名称 要与数据层有关
public User login(String username,String password);
}
SpringMVC
Web.xml 加载SpringMVC
Rest风格
数据封装为JSON数据
<!-- 中文过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>enconding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:spring-mvc.xml
DispatcherServlet
/
加载Springmvc需要这两个配置
@RestController
//公共的映射路径
@RequestMapping("/user")
Spring整合SpringMVC
在web环境加载spring上下文
@Autowired
private UserService userService;
表示 spring在启动时 将applciationContext.xml对应的配置文件加载到 Servlet tomcat范围 存在整个 则服务器中就会存在spring环境
<!-- tomcat文件配置文件名-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 在类路径下 寻找对应的 配置文件 -->
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 启动服务器 通过监听器加载Spring运行环境-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
表现层数据封装
前端接收数据表现层返回的数据种类
返回数据状态设计
根据业务不同设计不同的状态码
public class Code {
// 操作结果编码
public static final Integer SAVE_OK =20011;
public static final Integer UODATE_OK =20021;
public static final Integer DELETE_OK =20031;
public static final Integer GET_OK =20041;
// 失败的编码
public static final Integer SAVE_ERROR =20010;
public static final Integer UODATE_ERROR =20020;
public static final Integer DELETE_ERROR =20030;
public static final Integer GET_ERROR =20040;
}
@RestController
//公共的映射路径
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public Result save(User user){
boolean flag = userService.save(user);
return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERROR);
}
@PutMapping
public Result update(User user){
boolean flag = userService.update(user);
return new Result(flag ? Code.UODATE_OK : Code.UODATE_ERROR);
}
// 对应收集参数
@DeleteMapping("/{uuid}")
public Result delete(@PathVariable Integer uuid){
boolean flag = userService.delete(uuid);
return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERROR);
}
@GetMapping("/{uuid}") // 从路径中获取该变量
public Result get(@PathVariable Integer uuid){
User user = userService.get(uuid);
return new Result(null != user ? Code.GET_OK : Code.GET_ERROR,user);
}
@GetMapping("/{page}/{size}") // 从路径中获取该变量
public Result getAll(@PathVariable Integer page, @PathVariable Integer size){
PageInfo<User> all = userService.getAll(page, size);
return new Result(null != all ? Code.GET_OK : Code.GET_ERROR,all);
}
// 地址栏 有密码 故此处不遵循 rest风格
@PostMapping("/login") // 和上面的save进行区分s
public Result login(String username ,String password){
User user = userService.login(username, password);
return new Result(null != user ? Code.GET_OK : Code.GET_ERROR,user);
}
}
对应的Junit测试类
@RunWith(SpringJUnit4ClassRunner.class)
// 对应的配置文件
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSave(){
User user = new User();
user.setUuid(6);
user.setUserName("罗五");
user.setPassword("root");
user.setRealName("123");
user.setGender(1);
user.setBirthday(new Date(21412412412L));
userService.save(user);
}
@Test
public void testDelete(){
User user = new User();
userService.delete(1);
}
@Test
public void testUpdate(){
User user = new User();
user.setUuid(3);
user.setUserName("罗二");
user.setPassword("root1");
user.setRealName("123");
user.setGender(1);
user.setBirthday(new Date(21411232412L));
userService.update(user);
}
@Test
public void testGet(){
User user = userService.get(2);
System.out.println(user);
}
@Test
public void testGetAll(){
PageInfo<User> all= userService.getAll(1,3);
System.out.println(all);
System.out.println(all.getList().get(0));
System.out.println(all.getList().get(1));
System.out.println(all.getList().get(2));
}
@Test
public void testLogin(){
User user = userService.login("罗蛋","root");
System.out.println(user);
}
}
前台用的测试 均是在Postman上进行的