导入依赖:
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>org.example</groupId>
<artifactId>ssm_tk_smbms</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.14.RELEASE</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!--验证码功能-->
<dependency>
<groupId>cn.dsna.util.image</groupId>
<artifactId>ValidateCode</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
</project>
结构目录:
spring核心配置文件
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--加载数据库配置文件-->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!-- config params as the following -->
<value>
param1=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--mappe接口-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="org.qf.mapper"></property>
</bean>
<!--配置注解管理-->
<context:annotation-config></context:annotation-config>
<!--配置扫描包-->
<context:component-scan base-package="org.qf"></context:component-scan>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务注解驱动-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf-8
username=root
password=root
<?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/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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用mvc注解管理-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--扫描包-->
<context:component-scan base-package="org.qf.web"></context:component-scan>
<!--使用默认的处理器映射器-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--thymeleaf模板解析器-->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".html"></property>
<property name="templateMode" value="HTML5"></property>
<property name="cacheable" value="false"></property>
<property name="characterEncoding" value="utf-8"></property>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"></property>
</bean>
<!--视图解析器-->
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"></property>
<property name="characterEncoding" value="utf-8"></property>
</bean>
<!--文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
<property name="maxInMemorySize" value="102400"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
<?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">
<!--欢迎页-->
<welcome-file-list>
<welcome-file>/WEB-INF/templates/login.html</welcome-file>
</welcome-file-list>
<!--配置-->
<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:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--加载spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--编码过滤器-->
<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Calendar;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "smbms_user")
public class SmbmsUser {
@Id@KeySql(useGeneratedKeys = true)
private Integer id;
private String usercode;
private String username;
private String userpassword;
private Integer gender;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
private String phone;
private String address;
private Long userrole;
private Long createdby;
private Date creationdate;
private Long modifyby;
private Date modifydate;
//当知道出生年月日,能不能得到年龄 1998 2022
@Transient //忽略映射关系
private Integer age; //表示表中没有这个字段
public Integer getAge() {
//计算年龄
Calendar newDate=Calendar.getInstance();
Calendar birth=Calendar.getInstance();
birth.setTime(birthday);
return newDate.get(Calendar.YEAR)-birth.get(Calendar.YEAR);
}
}
mapper接口:
继承Mapper
import org.qf.pojo.SmbmsUser;
import tk.mybatis.mapper.common.Mapper;
public interface SmbmsUserMapper extends Mapper<SmbmsUser> {
}
业务层:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.qf.mapper.SmbmsUserMapper;
import org.qf.pojo.SmbmsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
@Service
public class SmbmsUserService {
@Resource
private SmbmsUserMapper smbmsUserMapper;
//登录功能
public SmbmsUser login(String username,String password){
SmbmsUser smbmsUser=new SmbmsUser();
smbmsUser.setUsername(username);
smbmsUser.setUserpassword(password);
return smbmsUserMapper.selectOne(smbmsUser);
}
/**
* 查询所有信息并分页 模糊查询 模糊查询并分页
* @return
*/
public PageInfo<SmbmsUser> showList(int pageNum,String username){
//设置当前页和页面容量
PageHelper.startPage(pageNum,3);
Example example=new Example(SmbmsUser.class);
Example.Criteria criteria=example.createCriteria();
//判断当用户名不为空时
if(!StringUtils.isEmpty(username)){
criteria.andLike("username",'%'+username+'%');
}
List<SmbmsUser> smbmsUsers = smbmsUserMapper.selectByExample(example);
PageInfo<SmbmsUser> pageInfo=new PageInfo<SmbmsUser>(smbmsUsers);
return pageInfo;
}
}
控制层:
import cn.dsna.util.images.ValidateCode;
import com.github.pagehelper.PageInfo;
import org.qf.pojo.SmbmsUser;
import org.qf.service.SmbmsUserService;
import org.springframework.http.HttpRequest;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@Controller
public class SmbmsUserController {
@Resource
private SmbmsUserService smbmsUserService;
@RequestMapping("/login")
public String login(String username, String password, String imgCode,Model model,HttpServletRequest request){
SmbmsUser login = smbmsUserService.login(username, password);
//从session中获取code
HttpSession session = request.getSession();
String codeName = (String)session.getAttribute("code");
//先进行验证码判断 equals 区分大小写 equalsIgnoreCase 不区分大小写
if(imgCode.equalsIgnoreCase(codeName)){
if(login != null){
//登录功能
session.setAttribute("name",username); //model的作用域跟request一样,
return "welcome";
}else{
//登录失败
return "login";
}
}else{
return "login";
}
}
//验证码
@RequestMapping("/imgCode")
public void imgCode(HttpServletResponse response, HttpServletRequest request){
// 四个参数: 第一个参数:宽度 第二个参数:高度 第三个参数:验证码的字符个数 第四个参数:线条数
ValidateCode validateCode=new ValidateCode(120,40,4,10);
//获取验证码
String code=validateCode.getCode();
// 把获取的验证码存储起来 session作用域,一次会话,把数据存储在session中,每个页面都可以共享。
HttpSession session = request.getSession();
session.setAttribute("code",code);
try {
validateCode.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
//查询并分页
@RequestMapping("/userList")
public String showUser(Model model, @RequestParam(defaultValue = "1") int pageNum,String username){
System.out.println("=============="+username+"=================");
//调用业务层代码
PageInfo<SmbmsUser> pageInfo = smbmsUserService.showList(pageNum,username);
System.out.println("========="+pageInfo+"===================");
//把list集合存储起来
model.addAttribute("username",username);
model.addAttribute("page",pageInfo);
return "userList";
}
}
前端页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8">
<title>系统登录 - 超市账单管理系统</title>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body class="login_bg">
<section class="loginBox">
<header class="loginHeader">
<h1>超市账单管理系统</h1>
</header>
<section class="loginCont">
<form class="loginForm" action="/login">
<div class="inputbox">
<label for="user">用户名:</label>
<input id="user" type="text" name="username" placeholder="请输入用户名" required/>
</div>
<div class="inputbox">
<label for="mima">密码:</label>
<input id="mima" type="password" name="password" placeholder="请输入密码" required/>
</div>
<div class="inputbox">
<label>验证码:</label>
<img src="/imgCode" id="img_code"/>
<input type="text" name="imgCode" placeholder="请输入验证码"/>
</div>
<div class="subBtn">
<input type="submit" value="登录" />
<input type="reset" value="重置"/>
</div>
</form>
</section>
</section>
//点击刷新验证码
</body>
<script src="/js/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("#img_code").click(function () {
$("#img_code").attr("src","/imgCode?time="+new Date().getTime());
});
});
</script>
</html>
快速开发用的是
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
减少sql语句的编写,接口的编写。
验证码使用的是:
<dependency>
<groupId>cn.dsna.util.image</groupId>
<artifactId>ValidateCode</artifactId>
<version>1.0.0</version>
</dependency>