<?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.xxx</groupId>
<artifactId>stu-ssm-base02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--spring环境-->
<!--spring环境-->
<!--spring环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--mybatis环境-->
<!--mybatis环境-->
<!--mybatis环境-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql环境-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring整合jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--分页插件坐标-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--springMVC环境-->
<!--springMVC环境-->
<!--springMVC环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--jackson相关坐标3个-->
<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-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!--servlet环境-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--其他组件-->
<!--其他组件-->
<!--其他组件-->
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<!--设置插件-->
<plugins>
<!--具体的插件配置-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<uriEncoding>utf-8</uriEncoding>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?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.xxx.dao.UserDao">
<!--添加-->
<insert id="save" parameterType="user">
insert into user(userName,password,realName,gender,birthday)values(#{userName},#{password},#{realName},#{gender},#{birthday})
</insert>
<!--删除-->
<delete id="delete" parameterType="int">
delete from user where uuid = #{uuid}
</delete>
<!--修改-->
<update id="update" parameterType="user">
update user set userName=#{userName},password=#{password},realName=#{realName},gender=#{gender},birthday=#{birthday} where uuid=#{uuid}
</update>
<!--查询单个-->
<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>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启bean注解扫描-->
<context:component-scan base-package="com.xxx"/>
<!--Spring整合MyBatis-->
<!--Spring整合MyBatis-->
<!--Spring整合MyBatis-->
<!--开启注解式事务驱动-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--加载jdbc配置文件-->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!--加载数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<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="com.xxx.domain"/>
<!--配置分页插件-->
<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>
<!--Mybatis Mapper 映射扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<!--配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启SpringMVC注解驱动-->
<mvc:annotation-driven/>
<!--配置SpringMVC注解扫描包路径-->
<context:component-scan base-package="com.xxx.controller"/>
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
package com.xxx.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* 用户实体类
*
* @author : lcj
* @date : Created in 2020/8/30 8:58
*/
public class User {
private Integer uuid;
private String userName;
private String password;
private String realName;
private Integer gender;
@DateTimeFormat(pattern = "yyyy/MM/dd")
@JsonFormat(pattern = "yyyy/MM/dd")
private Date birthday;
public User() {
}
public User(Integer uuid, String userName, String password, String realName, Integer gender, Date birthday) {
this.uuid = uuid;
this.userName = userName;
this.password = password;
this.realName = realName;
this.gender = gender;
this.birthday = birthday;
}
public Integer getUuid() {
return uuid;
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"uuid=" + uuid +
", userName=‘" + userName + ‘\‘‘ +
", password=‘" + password + ‘\‘‘ +
", realName=‘" + realName + ‘\‘‘ +
", gender=" + gender +
", birthday=" + birthday +
‘}‘;
}
}
package com.xxx.dao;
import com.xxx.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 用户数据持久层
*
* @author : lcj
* @date : Created in 2020/8/30 8:58
*/
public interface UserDao {
/**
* 保存用户
*
* @param user 用户对象
* @return 返回保存结果, true-保存成功, false-保存失败
*/
boolean save(User user);
/**
* 修改用户
*
* @param user 用户对象
* @return 返回修改结果, true-修改成功, false-修改失败
*/
boolean update(User user);
/**
* 删除用户
*
* @param id 用户id
* @return 返回删除结果, true-保存成功, false-删除失败
*/
boolean delete(Integer id);
/**
* 根据id查询用户
*
* @param id 用户id‘
* @return 返回查询的用户信息
*/
User get(Integer id);
/**
* 查询所有的用户
*
* @return 返回查询的用户集合信息
*/
List<User> getAll();
/**
* 用户登录查询
* @param userName 用户登录名
* @param password 用户登录密码
* @return 用户信息
*/
User getByUserNameAndPassword(@Param("userName") String userName, @Param("password") String password);
}
package com.xxx.service;
import com.github.pagehelper.PageInfo;
import com.xxx.domain.User;
/**
* 用户逻辑层
*
* @author : lcj
* @date : Created in 2020/8/30 8:59
*/
public interface UserService {
/**
* 保存用户
*
* @param user 用户对象
* @return 返回保存结果, true-保存成功, false-保存失败
*/
boolean save(User user);
/**
* 修改用户
*
* @param user 用户对象
* @return 返回修改结果, true-修改成功, false-修改失败
*/
boolean update(User user);
/**
* 删除用户
*
* @param id 用户id
* @return 返回删除结果, true-保存成功, false-删除失败
*/
boolean delete(Integer id);
/**
* 根据id查询用户
*
* @param id 用户id‘
* @return 返回查询的用户信息
*/
User get(Integer id);
/**
* 分页查询所有的用户
*
* @param page 查询第几页
* @param size 每页显示的条数
* @return 返回查询的用户集合信息
*/
PageInfo<User> getAll(int page, int size);
/**
* 用户登录
* @param userName 用户登录名
* @param password 用户登录密码
* @return 用户信息
*/
User login(String userName, String password);
}
package com.xxx.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.dao.UserDao;
import com.xxx.domain.User;
import com.xxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author : lcj
* @date : Created in 2020/8/30 8:59
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/**
* 保存用户
*
* @param user 用户对象
* @return 返回保存结果, true-保存成功, false-保存失败
*/
@Override
public boolean save(User user) {
return userDao.save(user);
}
/**
* 修改用户
*
* @param user 用户对象
* @return 返回修改结果, true-修改成功, false-修改失败
*/
@Override
public boolean update(User user) {
return userDao.update(user);
}
/**
* 删除用户
*
* @param id 用户id
* @return 返回删除结果, true-保存成功, false-删除失败
*/
@Override
public boolean delete(Integer id) {
return userDao.delete(id);
}
/**
* 根据id查询用户
*
* @param id 用户id‘
* @return 返回查询的用户信息
*/
@Override
public User get(Integer id) {
return userDao.get(id);
}
/**
* 分页查询所有的用户
*
* @param page 查询第几页
* @param size 每页显示的条数
* @return 返回查询的用户集合信息
*/
@Override
public PageInfo<User> getAll(int page, int size) {
PageHelper.startPage(page, size);
List<User> all = userDao.getAll();
return new PageInfo<>(all);
}
/**
* 用户登录
*
* @param userName 用户登录名
* @param password 用户登录密码
* @return 用户信息
*/
@Override
public User login(String userName, String password) {
return userDao.getByUserNameAndPassword(userName, password);
}
}
package com.xxx.controller;
import com.github.pagehelper.PageInfo;
import com.xxx.controller.results.Code;
import com.xxx.controller.results.Result;
import com.xxx.domain.User;
import com.xxx.service.UserService;
import com.xxx.system.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 用户表现层
*
* @author : lcj
* @date : Created in 2020/8/30 8:59
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 保存用户信息
*/
@PostMapping
public Result save(User user) {
boolean saveFlag = userService.save(user);
return new Result(saveFlag ? Code.SAVE_OK : Code.SAVE_ERROR, saveFlag);
}
/**
* 修改用户信息
*/
@PutMapping("/f")
public Result updateForm(User user) {
System.out.println(user);
boolean updateFlag = userService.update(user);
return new Result(updateFlag ? Code.UPDATE_OK : Code.UPDATE_ERROR, updateFlag);
}
@PutMapping("/j")
public Result updateJson(@RequestBody User user) {
System.out.println("异步请求" + user);
boolean updateFlag = userService.update(user);
return new Result(updateFlag ? Code.UPDATE_OK : Code.UPDATE_ERROR, updateFlag);
}
/**
* 根据id删除用户信息
*/
@DeleteMapping("/{uuid}")
public Result delete(@PathVariable int uuid) {
boolean deleteFlag = userService.delete(uuid);
return new Result(deleteFlag ? Code.DELETE_OK : Code.DELETE_ERROR, deleteFlag);
}
/**
* 用户登录
*/
@GetMapping("/login")
public Result login(String userName, String password) {
User login = userService.login(userName, password);
return new Result(login != null ? Code.GET_OK : Code.GET_ERROR, login != null, login);
}
/**
* 根据id查询用户信息
*/
@GetMapping("/{uuid}")
public Result get(@PathVariable Integer uuid) {
User user = userService.get(uuid);
//模拟出现异常,使用条件控制,便于测试结果
if (uuid == 10) {
throw new BusinessException("查询出错啦,请重试!", Code.GET_ERROR);
}
return new Result(user == null ? Code.GET_OK : Code.GET_ERROR, user == null, user);
}
/**
* 分页查询所有用户信息
*/
@GetMapping("/{page}/{size}")
public Result getAll(@PathVariable int page, @PathVariable int size) {
PageInfo<User> all = userService.getAll(page, size);
return new Result(all.getList() != null ? Code.GET_OK : Code.GET_ERROR, all.getList() != null, all);
}
}
package com.xxx.controller.results;
/**
* 表现层数据封装类
*
* @author : lcj
* @date : Created in 2020/8/30 15:42
*/
public class Result {
/**
* 操作结果编码
*/
private Integer code;
/**
* 操作结果成功标志
*/
private boolean flag;
/**
* 操作数据结果
*/
private Object data;
/**
* 消息 -返回结果信息, 主要用于页面提示信息
*/
private String message;
public Result(Integer code, boolean flag) {
this.code = code;
this.flag = flag;
}
public Result(Integer code, boolean flag, Object data) {
this.code = code;
this.flag = flag;
this.data = data;
}
public Result(Integer code, boolean flag, String message) {
this.code = code;
this.flag = flag;
this.message = message;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", data=" + data +
", message=‘" + message + ‘\‘‘ +
‘}‘;
}
}
package com.xxx.controller.results;
/**
* 表现层数据封装编码类 - 状态码常量可以根据自己的业务需求设定
*
* @author : lcj
* @date : Created in 2020/8/30 15:43
*/
public class Code {
// 操作结果编码
/**
* 保存成功
*/
public static final Integer SAVE_OK = 20011;
/**
* 修改成功
*/
public static final Integer UPDATE_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 UPDATE_ERROR = 20020;
/**
* 删除失败
*/
public static final Integer DELETE_ERROR = 20030;
/**
* 获取失败
*/
public static final Integer GET_ERROR = 20040;
// 系统错误编码
// 操作权限编码
// 校验结果编码
}
package com.xxx.system.exception;
/**
* 业务异常类
*
* @author : lcj
* @date : Created in 2020/8/30 16:18
*/
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code) {
super();
this.code = code;
}
public BusinessException(String message, Integer code) {
super(message);
this.code = code;
}
public BusinessException(String message, Throwable cause, Integer code) {
super(message, cause);
this.code = code;
}
public BusinessException(Throwable cause, Integer code) {
super(cause);
this.code = code;
}
protected BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) {
super(message, cause, enableSuppression, writableStackTrace);
this.code = code;
}
}
package com.xxx.system.exception;
/**
* @author : lcj
* @date : Created in 2020/8/30 16:18
*/
public class SystemException {
}
package com.xxx.controller.interceptor;
import com.xxx.controller.results.Result;
import com.xxx.system.exception.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 项目异常拦截器类
*
* @author : lcj
* @date : Created in 2020/8/30 16:18
*/
@Component
@ControllerAdvice
public class ProjectExceptionAdvice {
/**
* 对出现异常的情况进行拦截,并将其处理成统一的页面数据结果格式
*
* @param e 业务异常对象
* @return 数据封装异常信息
*/
@ExceptionHandler(BusinessException.class)
@ResponseBody
public Result doBusinessException(BusinessException e) {
return new Result(e.getCode(), false, e.getMessage());
}
}
原文:https://www.cnblogs.com/bklcj/p/13590421.html