# 创建数据库
CREATE DATABASE db_20211028;
# 使用数据库
USE db_20211028;
# 1.1 创建用户表
CREATE TABLE `user` (
`uid` VARCHAR(32) NOT NULL,
`username` VARCHAR(20) DEFAULT NULL, #用户名
`password` VARCHAR(32) DEFAULT NULL, #密码
`name` VARCHAR(20) DEFAULT NULL, #昵称
`email` VARCHAR(30) DEFAULT NULL, #电子邮箱
`telephone` VARCHAR(20) DEFAULT NULL, #电话
`birthday` DATE DEFAULT NULL, #生日
`sex` VARCHAR(10) DEFAULT NULL, #性别
`state` INT(11) DEFAULT 0, #状态:0=未激活,1=已激活
`code` VARCHAR(64) DEFAULT NULL, #激活码
PRIMARY KEY (`uid`)
) ;
# 1.2 初始化用户默认数据
INSERT INTO `user` VALUES ('u001','jack','1234','杰克','[email protected]','13612345678','2015-11-04','男',0,NULL);
INSERT INTO `user` VALUES ('u002','rose','1234','肉丝','[email protected]','13612345679','2015-11-05','女',0,NULL);
INSERT INTO `user` VALUES('373eb242933b4f5ca3bd43503c34668b','ccc','ccc','aaa','[email protected]','15723689921','2015-11-04','男',0,'9782f3e837ff422b9aee8b6381ccf927bdd9d2ced10d48f4ba4b9f187edf7738'),('3ca76a75e4f64db2bacd0974acc7c897','bb','bb','张三','[email protected]','15723689921','1990-02-01','男',0,'1258e96181a9457987928954825189000bae305094a042d6bd9d2d35674684e6'),('62145f6e66ea4f5cbe7b6f6b954917d3','cc','cc','张三','[email protected]','15723689921','2015-11-03','男',0,'19f100aa81184c03951c4b840a725b6a98097aa1106a4a38ba1c29f1a496c231'),('c95b15a864334adab3d5bb6604c6e1fc','bbb','bbb','老王','[email protected]','15712344823','2000-02-01','男',0,'71a3a933353347a4bcacff699e6baa9c950a02f6b84e4f6fb8404ca06febfd6f'),('f55b7d3a352a4f0782c910b2c70f1ea4','aaa','aaa','小王','[email protected]','15712344823','2000-02-01','男',1,NULL);
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<failOnMissingWebXml>falsefailOnMissingWebXml>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.24version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapperartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>3.7.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.5version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.10.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.10version>
dependency>
dependencies>
public class MyBatisConfiguration {
/**
* 配置session工厂
* @param dataSource
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
//1 创建 factoryBean
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
//2 设置数据
// 2.1 数据源
factoryBean.setDataSource(dataSource);
// 2.2 驼峰命名
Configuration configuration = new Configuration();
configuration.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(configuration);
// 2.3 分页插件
Properties props = new Properties();
// 设置方言
props.setProperty("dialect", "mysql");
// 分页的同时进行count查询
props.setProperty("rowBoundsWithCount", "true");
// 分页合理化参数,pageNum<=0 时会查询第一页,pageNum>pages (超过总数时),会查询最后一页
props.setProperty("reasonable", "true");
// PageInterceptor pageInterceptor = new PageInterceptor();
// pageInterceptor.setProperties(props);
PageHelper pageHelper = new PageHelper();
pageHelper.setProperties(props);
factoryBean.setPlugins(new Interceptor[] {pageHelper});
//3 通过factorybean获得对应
return factoryBean.getObject();
}
/**
* 映射扫描器
* @return
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
//1 创建
MapperScannerConfigurer mapperScanner = new MapperScannerConfigurer();
//2设置包
mapperScanner.setBasePackage("com.czxy.ssm.mapper");
return mapperScanner;
}
}
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db2
jdbc.username=root
jdbc.password=1234
@ComponentScan(basePackages="com.czxy.ssm.service")
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
public class SpringConfiguration {
/**
* 获得properties文件中内容,并注入对应变量
*/
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 配置数据源
* @return
*/
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
/**
* 事务管理器
* @param dataSource
* @return
*/
@Bean
public DataSourceTransactionManager txManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
@Configuration
@ComponentScan(basePackages={"com.czxy.ssm.controller","com.czxy.ssm.resolver"})
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
/**
* 视图解析器
* @return
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/pages/",".jsp");
}
}
public class WebInitializer3 implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//1 配置spring工厂
AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext();
// 注册所有的配置类
application.register(MyBatisConfiguration.class);
application.register(SpringConfiguration.class);
application.register(MvcConfiguration.class);
//2 post中文乱码
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8"));
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
//3 核心控制器
ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application));
mvcServlet.addMapping("*.action"); //普通项目
//mvcServlet.addMapping("/"); //RESTFul项目
mvcServlet.setLoadOnStartup(2); //tomcat启动时,执行servlet的初始化方法
}
}
public class CustomException extends RuntimeException {
public CustomException() {
super();
}
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
public CustomException(Throwable cause) {
super(cause);
}
protected CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
@ControllerAdvice
public class GlobalExceptionResolver {
/**
* 自定义异常处理器
*
* @param ce
* @param model
* @return
*/
@ExceptionHandler(CustomException.class)
public String custom(CustomException ce, Model model) {
model.addAttribute("error", ce.getMessage());
return "forward:/pages/error.jsp";
}
/**
* 其他异常处理器
*
* @param e
* @param model
* @return
*/
@ExceptionHandler(Exception.class)
public String other(Exception e, Model model) {
model.addAttribute("error",e.getMessage());
return "forward:/pages/error.jsp";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
错误页面
${error}
package com.czxy.ssm.domain;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* @Author: 小新
* @Date: 2021-12-14 19:54
*/
@Table(name = "user")
public class User {
@Id
private String uid;
private String username;
private String password;
private String name;
private String email;
private String telephone;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
private String sex;
private Integer state;
private String code;
//getter、setter...
package com.czxy.ssm.vo;
/**
* @Author: 小新
* @Date: 2021-12-16 14:46
*/
public class UserVo {
private String name;
private String start;
private String end;
private Integer pageNum = 1;
private Integer pageSize = 2;
//getteer、setter...
}
package com.czxy.ssm.mapper;
import com.czxy.ssm.domain.User;
import tk.mybatis.mapper.common.Mapper;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserMapper extends Mapper<User> {
}
package com.czxy.ssm.service;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserService {
/**
* 查询所有
* @return userVo
*/
PageInfo<User> selectAll(UserVo userVo);
}
package com.czxy.ssm.service.impl;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 小新
* @Date: 2021-12-14 19:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public PageInfo<User> selectAll(UserVo userVo) {
//1、拼凑条件
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
//2、查询条件
//2.1 昵称
if (userVo.getName() != null && !"".equals(userVo.getName())){
criteria.andLike("name","%"+userVo.getName()+"%");
}
//2.2 开始生日时间
if (userVo.getStart() != null && !"".equals(userVo.getStart())){
criteria.andGreaterThanOrEqualTo("birthday",userVo.getStart() + " 00:00:00");
}
//2.3 结束生日时间
if (userVo.getEnd() != null && !"".equals(userVo.getEnd())){
criteria.andLessThanOrEqualTo("birthday",userVo.getEnd() + " 23:59:59");
}
//分页条件
PageHelper.startPage(userVo.getPageNum(),userVo.getPageSize());
//3、查询
List<User> list = userMapper.selectByExample(example);
//4、关联查询
//5、封装分页数据
PageInfo<User> pageInfo = new PageInfo<>(list);
return pageInfo;
}
}
package com.czxy.ssm.controller;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
/**
* @Author: 小新
* @Date: 2021-12-14 19:59
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 查询所有
* @param model
* @return
*/
@RequestMapping("/selectAll")
public String selectAll(UserVo userVo , Model model){
// 查询所有
PageInfo<User> pageInfo = userService.selectAll(userVo);
// 将查询结果 、 查询条件 存放request --> 模型
model.addAttribute("pageInfo",pageInfo);
model.addAttribute("userVo",userVo);
// 设置视图名
return "list";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
列表页面
${error}
添加
编号
登录名
密码
姓名
邮件
手机号
生日
性别
状态
规范
操作
男
女
删除
修改
当前第${pageInfo.pageNum}页,每页显示
条,
首页
上一页
${num}
下一页
尾页
添加
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加
${error}
package com.czxy.ssm.controller;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
/**
* @Author: 小新
* @Date: 2021-12-14 19:59
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 添加
* @param user
* @param model
* @return
*/
@RequestMapping("/add")
public String add(User user, Model model){
Boolean result = userService.insert(user);
if (result){
//添加成功
return "redirect:/user/selectAll.action";
}else{
//添加失败
model.addAttribute("error","添加失败");
return "add";
}
}
package com.czxy.ssm.service;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserService {
/**
* 添加
* @param user
* @return
*/
Boolean insert(User user);
}
package com.czxy.ssm.service.impl;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 小新
* @Date: 2021-12-14 19:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public Boolean insert(User user) {
int result = userMapper.insert(user);
return result == 1;
}
}
修改
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
编辑
${error}
package com.czxy.ssm.controller;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* @Author: 小新
* @Date: 2021-12-14 19:59
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 根据id查询详情
* 编辑前
* @param uid
* @param model
* @return
*/
@RequestMapping("/selectById")
public String selectById(String uid , Model model){
User user = userService.selectById(uid);
if (user == null){
return "list";
}else{
model.addAttribute("user",user);
return "update";
}
}
}
package com.czxy.ssm.service;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserService {
/**
* 根据id查询详情
* 更新前回显
* @param uid
* @return
*/
User selectById(String uid);
}
package com.czxy.ssm.service.impl;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 小新
* @Date: 2021-12-14 19:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public User selectById(String uid) {
return userMapper.selectByPrimaryKey(uid);
}
}
package com.czxy.ssm.controller;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* @Author: 小新
* @Date: 2021-12-14 19:59
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 修改
* @param user
* @param model
* @return
*/
@RequestMapping("/update")
public String update(User user , Model model){
Boolean result = userService.update(user);
if (result){
//修改成功
return "redirect:/user/selectAll.action";
}else{
//修改失败
model.addAttribute("error","修改失败");
return "update";
}
}
}
package com.czxy.ssm.service;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserService {
/**
* 更新
* @param user
* @return
*/
Boolean update(User user);
}
package com.czxy.ssm.service.impl;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 小新
* @Date: 2021-12-14 19:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public Boolean update(User user) {
int update = userMapper.updateByPrimaryKey(user);
return update == 1;
}
}
删除
package com.czxy.ssm.controller;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* @Author: 小新
* @Date: 2021-12-14 19:59
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
* 删除
* @param uid
* @return
*/
@RequestMapping("/delete")
public String delete(String uid, Model model){
Boolean result = userService.delete(uid);
if (result){
//删除成功
return "redirect:/user/selectAll.action";
}else{
//删除失败
model.addAttribute("error","删除失败");
return "list";
}
}
}
package com.czxy.ssm.service;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageInfo;
/**
* @Author: 小新
* @Date: 2021-12-14 19:57
*/
public interface UserService {
/**
* 删除
* @param uid
* @return
*/
Boolean delete(String uid);
}
package com.czxy.ssm.service.impl;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
import com.czxy.ssm.service.UserService;
import com.czxy.ssm.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 小新
* @Date: 2021-12-14 19:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public Boolean delete(String uid) {
int result = userMapper.deleteByPrimaryKey(uid);
return result == 1;
}
}
ps:小编把项目放在了百度网盘,有需要的可以拿走
链接:https://pan.baidu.com/s/1uaIN_mqygZCY3_efwmL59w
提取码:7264