1:创建maven项目
idea->file->new project->maven 创建maven项目
点击加好添加一个name=archerypeVersion,value=RELEASE可以加快maven的项目的构建点击next完成
刚创建完的目录:
添加java文件夹,设置根目录
创建项目目录:
2:编写pom.xml文件引入jar包
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lmy.test</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--版本号-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- spring版本号 -->
<spring.version>4.3.6.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.8</mybatis.version>
<!-- mysql驱动版本号 -->
<mysql.version>5.1.35</mysql.version>
<!-- log4j日志包版本号 -->
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--导入jar包,springmvc ,spring ,mybatis,数据库链接-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 添加mybatis/spring整合包依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!--ssm jar包整合完毕-->
<!--数据库的jar-->
<!-- 添加mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 添加数据库连接池依赖 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--添加日志和json jar包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<!--文件上传jar-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--loombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
<!--quartz-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
下面的可以不要
<build>
<finalName>ssm</finalName>
<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>
3:创建配置文件
springmvc.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 http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 开启注解-->
<mvc:annotation-driven/>
<!-- 扫描包配置-->
<context:component-scan base-package="mytest.controller.**">
</context:component-scan>
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源,和web.xml文件配合使用,来拦截请求,识别静态资源-->
<mvc:default-servlet-handler/>
</beans>
application.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="mytest.dao.**"/>
<context:component-scan base-package="mytest.service.**"/>
<!-- 加载redis的配置文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redisconfig.properties"/>
</bean>
<!-- 第一种方式:加载一个properties文件 -->
<!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClasss}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!– 初始化连接大小 –>
<property name="initialSize" value="${initialSize}"></property>
<!– 连接池最大数量 –>
<property name="maxActive" value="${maxActive}"></property>
<!– 连接池最大空闲 –>
<property name="maxIdle" value="${maxIdle}"></property>
<!– 连接池最小空闲 –>
<property name="minIdle" value="${minIdle}"></property>
<!– 获取连接最大等待时间 –>
<property name="maxWait" value="${maxWait}"></property>
</bean>-->
<!--第二种方式-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mytest/dao/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mytest.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 定义一个任务类 -->
<bean id="myJob" class="mytest.job.MyJob"></bean>
<!-- jobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob"></property>
<property name="targetMethod" value="execute"></property>
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- 定义trigger 触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="0/10 * * * * ?"></property>
</bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
<!-- redis config start -->
<!-- 配置JedisPoolConfig实例 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 配置JedisConnectionFactory -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="database" value="${redis.dbIndex}" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 配置RedisCacheManager -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate" />
<property name="defaultExpiration" value="${redis.expiration}" />
</bean>
<!-- 配置RedisCacheConfig -->
<bean id="redisCacheConfig" class="mytest.redis.RedisCacheConfig">
<constructor-arg ref="jedisConnectionFactory" />
<constructor-arg ref="redisTemplate" />
<constructor-arg ref="redisCacheManager" />
</bean>
<!-- redis config end -->
</beans>
redisconfig.properties
# Redis settings
redis.host=127.0.0.1
redis.port=6379
#redis.pass=password
redis.dbIndex=0
redis.expiration=3000
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
jdbc.properties
driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=自己的密码
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
log4j.properties
log4j.rootLogger=INFO,Console,File
#控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#普通文件日志
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
web.xml
<?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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--日志文件的加载-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--spring 配置文件的加载-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--字符集过滤-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器,比如quartz -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--配置springmvc的前端控制器DispatcherServlet。拦截所有请求-->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- session配置 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<!--定义首页列单.-->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4:代码编写
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<script type="text/javascript" src="<%=path%>/js/jquery-3.1.1.min.js"></script>
<head>
<title>Title</title>
</head>
<body>
<form action="<%=path%>/bookConroller/delete.do" method="post" id="fm">
<div style="text-align:center;">
<a href="<%=path%>/bookConroller/toAdd.do?pageIndex=${page.totalPage}&pageSize=${page.pageSize}">添加书籍</a><br/>
<input type="text" name="booksName" id="con">
<select οnchange="chooseType()" id="sel">
<option value="booksName">按书名</option>
<option value="booksType">按类型</option>
<option value="booksPrice">按价格</option>
<option value="library.libraryName">按图书馆</option>
</select>
<input type="button" value="查询" οnclick="find()">
</div>
<input type="submit" value="批量删除" οnclick="return confirm('确认此操作?');">
<table width="100%" border="1px">
<tr>
<th width="5%"><input type="checkbox" name="ck" id="ckb" οnclick="qx()">序号</th>
<th width="15%">姓名</th>
<th width="10%">性别</th>
<th width="10%">年龄</th>
<th width="10%">职业</th>
<th width="20%">地址</th>
<th width="*">操作</th>
</tr>
<c:forEach items="${slist}" var="student" varStatus="vs">
<tr>
<td><input type="checkbox" name="bkId" value="${student.sid}"/>${vs.count}</td>
<td>${student.sname}</td>
<td>${student.ssex}</td>
<td>${student.age}</td>
<td>${student.job}</td>
<td>${student.address}</td>
<td><a href="<%=path%>/bookConroller/toUpdate.do?booksId=${books.booksId}&pageIndex=${page.pageIndex}&pageSize=${page.pageSize}">编辑</a></td>
</tr>
</c:forEach>
</table>
</form>
<script type="text/javascript">
function qx() {
var hb=document.getElementsByName("bkId");
for(var i=0;i<hb.length;i++){
if(hb[i].checked){
hb[i].checked=false;
}else{
hb[i].checked=true;
}
}
}
function find(){
$("#fm").attr("action","<%=path%>/bookConroller/findBooks.do")
$("#fm").submit();
}
function chooseType(){
var condition=$("#sel").val();
$("#con").attr("name",condition);
}
function changSize(v){
window.location="<%=path%>/bookConroller/findBooks.do?pageSize="+v+"&pageIndex=${page.pageIndex}";
}
function toFirst(){
window.location="<%=path%>/bookConroller/findBooks.do?pageIndex=1&pageSize=${page.pageSize}";
}
function toUp() {
window.location="<%=path%>/bookConroller/findBooks.do?pageIndex=${page.pageIndex-1}&pageSize=${page.pageSize}";
}
function toDown() {
window.location="<%=path%>/bookConroller/findBooks.do?pageIndex=${page.pageIndex+1}&pageSize=${page.pageSize}";
}
function toNumber(nbr) {
window.location="<%=path%>/bookConroller/findBooks.do?pageIndex="+nbr+"&pageSize=${page.pageSize}";
}
function toLast() {
window.location="<%=path%>/bookConroller/findBooks.do?pageIndex=${page.totalPage}&pageSize=${page.pageSize}";
}
function jump(){
var number=document.getElementById("number").value;
if(number>0&&number<=${page.totalPage}){
window.location="<%=path%>/bookConroller/find.do?pageIndex="+number+"&pageSize=${page.pageSize}";
}else{
alert("页码不存在");
}
}
$().ready(function(){
if(!${empty book.booksName}){
$("#sel").val("booksName");
$("#con").attr("name","booksName");
$("#con").val("${book.booksName}");
}else if(!${empty book.booksType}){
$("#sel").val("booksType");
$("#con").attr("name","booksType");
$("#con").val("${book.booksType}");
}else if(!${empty book.library.libraryName}){
$("#sel").val("library.libraryName");
$("#con").attr("name","library.libraryName");
$("#con").val("${book.library.libraryName}");
}else if(!${book.booksPrice}==0){
$("#sel").val("booksPrice");
$("#con").attr("name","booksPrice");
$("#con").val("${book.booksPrice}");
}
});
</script>
</body>
</html>
controller
package mytest.controller;
import mytest.entity.Student;
import mytest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
*
* @author 李梦源
* @create 2018-03-13 13:47
**/
@Controller
@RequestMapping("/studentController")
public class StudentController {
@Resource
StudentService studentService;
@RequestMapping("/findstudents")
public String findstudents(Model model){
List<Student> studentlist = studentService.findstudentList();
model.addAttribute("slist",studentlist);
return "show";
}
}
service
package mytest.service;
import mytest.entity.Student;
import java.util.List;
/**
* Created by hasee on 2018/3/13.
*/
public interface StudentService {
List<Student> findstudentList();
}
serviceimpl
package mytest.service;
import mytest.dao.StudentDao;
import mytest.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
* @author 李梦源
* @create 2018-03-13 14:38
**/
@Service
public class StudentServiceImpl implements StudentService {
@Resource StudentDao sdao;
@Cacheable("getAllUser")
public List<Student> findstudentList() {
return sdao.findstudentList();
}
}
dao
package mytest.dao;
import mytest.entity.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by hasee on 2018/3/13.
*/
@Repository
public interface StudentDao {
List<Student> findstudentList();
}
mapper.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="mytest.dao.StudentDao">
<resultMap type="mytest.entity.Student" id="studentsResultMap">
<id property ="sid" column ="sid" />
<result property ="sname" column ="sname" />
<result property ="ssex" column ="ssex" />
<result property ="age" column ="age" />
<result property ="address" column ="address" />
<result property ="job" column ="job" />
<result property ="class_id" column ="class_id" />
</resultMap>
<select id="findstudentList" resultMap="studentsResultMap" parameterType="map">
select * from Student;
</select>
</mapper>
实体类
package mytest.entity;
import lombok.Data;
import java.io.Serializable;
/**
*
* @author 李梦源
* @create 2018-03-13 14:37
**/
@Data
public class Student implements Serializable {
private int sid;
private String sname;
private String ssex;
private double age;
private int class_id;
private String address;
private String job;
}
quarzt 的myjob
package mytest.job;
import mytest.controller.StudentController;
import mytest.entity.Student;
import mytest.service.StudentService;
import org.springframework.ui.Model;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
*
* @author 李梦源
* @create 2018-03-14 11:04
**/
public class MyJob {
@Resource
StudentService studentService;
public void execute(){
List<Student> stu = studentService.findstudentList();
System.out.println("Quartz的任务调度!!!"+stu.size());
}
}
redis的config类
package mytest.redis;
/**
*
* @author 李梦源
* @create 2018-03-19 19:04
**/
import java.lang.reflect.Method;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* 通过spring管理redis缓存配置
*
* @author Administrator
*
*/
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
private volatile JedisConnectionFactory jedisConnectionFactory;
private volatile RedisTemplate<String, String> redisTemplate;
private volatile RedisCacheManager redisCacheManager;
public RedisCacheConfig() {
super();
}
/**
* 带参数的构造方法 初始化所有的成员变量
*
* @param jedisConnectionFactory
* @param redisTemplate
* @param redisCacheManager
*/
public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory, RedisTemplate<String, String> redisTemplate,
RedisCacheManager redisCacheManager) {
this.jedisConnectionFactory = jedisConnectionFactory;
this.redisTemplate = redisTemplate;
this.redisCacheManager = redisCacheManager;
}
public JedisConnectionFactory getJedisConnecionFactory() {
return jedisConnectionFactory;
}
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
public RedisCacheManager getRedisCacheManager() {
return redisCacheManager;
}
@Bean
public KeyGenerator customKeyGenerator() {
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}
5:项目部署测试项目是否可以启动
点击页面成功进入主页面
quarzt:运行成功每隔10秒查询一次后台(idea控制台乱码可以设置下字符集在redis的安装文件)
redis验证:
mysql五条数据,现在在直接操作数据库删除一条
刷新前台看页面展示仍让是5条说明redis起作用了