SSM整合
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_4_0.xsd"
version="4.0">
<!-- springMVC编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springMVC restful编码风格,方法辨析-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_MVC.xml</param-value>
</init-param>
<!--将DispatcherServlet 的创建时间提前到tomcat启动时-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- /不进jsp /*进jsp-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
spring_MVC.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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描-->
<context:component-scan base-package="com.ljh.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 如此设置就可以访问其他控制器-->
<mvc:annotation-driven/>
<!-- 处理静态资源-->
<mvc:default-servlet-handler/>
<!-- 不经过后台控制器,直接跳转页面,但是会导致其他控制器无法访问-->
<mvc:view-controller path="/" view-name="welcome"></mvc:view-controller>
</beans>
spring_mybatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 指定扫描文件 ,不包含控制层-->
<context:component-scan base-package="com.ljh">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 加载数据源文件-->
<context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
<!-- 配置数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置MyBatis的SessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置文件-->
<property name="configLocation" value="classpath:Mybatis_config.xml"></property>
<!-- 映射文件-->
<property name="mapperLocations" value="classpath:com/ljh/mapper/*.xml"></property>
</bean>
<mybatis-spring:scan base-package="com.ljh.mapper"></mybatis-spring:scan>
<bean class="org.mybatis.spring.SqlSessionTemplate" name="sessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- Mapper接口组件扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ljh.mapper"></property>
<property name="sqlSessionTemplateBeanName" value="sessionTemplate"></property>
</bean>
<!-- 事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- <tx:annotation-driven></tx:annotation-driven>-->
<!-- 事务配置,注解的话,只需要上面这一行-->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"></tx:method>
<tx:method name="find*"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="point" expression="execution(* com.ljh.service.impl.*.*(..))"/>
<aop:advisor advice-ref="interceptor" pointcut-ref="point"></aop:advisor>
</aop:config>
</beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启驼峰命名,忽略数据库字段名下划线,实体类属性名规则为驼峰法-->
</settings>
<!-- 插件,下面的是分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<!-- 需要什么,添加什么-->
</configuration>
以上为配置,下面是用分页插件写的分页条件查询案例
bean层
实体类,就和数据库列名对应;
mapper层
接口,写入需要的方法
@Repository
public interface EmployeeMapper {
public List<Employee> findAll();
public List<Employee> findAll2(@Param("firstName") String firstName, @Param("min") Integer min, @Param("max") Integer max);
}
映射文件,放置在resourse文件夹下,需要和mapper层的接口文件名、层级保持一致,一次写入多级创建文件夹时需要用" / “,而不能用” . "nampspace 指向接口文件,映射中的方法名也保持一直
<?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.ljh.mapper.EmployeeMapper">
<!-- public List<Employee> findAll();-->
<select id="findAll" resultType="com.ljh.bean.Employee">
select * from employees
</select>
<!-- public List<Employee> findAll2(String firstName,Integer min,Integer max);-->
<sql id="baseSql" >
SELECT * FROM employees
</sql>
<select id="findAll2" resultType="com.ljh.bean.Employee">
<include refid="baseSql"></include>
<trim prefix="where" prefixOverrides="and">
<if test="firstName!=null and firstName!='' ">
<bind name="first_Name" value="'%'+firstName+'%'"/>
AND first_name LIKE #{first_Name}
</if>
<if test="min!=null and min!=0">
AND salary >= #{min}
</if>
<if test="max!=null and max!=''">
AND salary <= #{max}
</if>
</trim>
</select>
</mapper>
service层,写入接口和实体类,调用mapper的方法完成相关操作,事务一般添加在这一层
@Service
public class EmplyeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public PageInfo<Employee> getPageInfo(int pageNo, int pageSize, int nums) {
Page<Employee> page= PageHelper.startPage(pageNo,pageSize);
List<Employee> list= employeeMapper.findAll();;
PageInfo<Employee> pageInfo=new PageInfo<>(list,nums);
return pageInfo;
}
@Override
public PageInfo<Employee> getPageInfo(int pageNo, int pageSize, int nums, String firstName, Integer min, Integer max) {
Page<Employee> page= PageHelper.startPage(pageNo,pageSize);
List<Employee> list=employeeMapper.findAll2(firstName,min,max);
PageInfo<Employee> pageInfo=new PageInfo<>(list,nums);
return pageInfo;
}
}
controller层,接收请求和响应
@Controller
public class EmployeeController {
@Autowired
@Qualifier(value = "emplyeeServiceImpl")
private EmployeeService employeeService;
@GetMapping(value = "/emp")
public String getEmp(Integer pageNo, Map<String,Object> map){
if(pageNo==null){
pageNo=1;
}
PageInfo<Employee> pageInfo= employeeService.getPageInfo(pageNo,10,5);
map.put("pageInfo",pageInfo);
return "emplist";
}
@PostMapping(value = "/emp")
public String getEmp2(String firstName, Integer min, Integer max , Integer pageNo, Map<String,Object> map){
if(pageNo==null){
pageNo=1;
}
PageInfo<Employee> pageInfo= employeeService.getPageInfo(pageNo,10,5,firstName,min,max);
map.put("pageInfo",pageInfo);
return "emplist";
}
}
前端页面(jsp)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021/9/28 0028
Time: 15:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-5.1.0-dist/css/bootstrap.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery/jquery-3.6.0.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-5.1.0-dist/js/bootstrap.js"></script>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>
<h2>员工信息</h2>
<div>
<form action="${pageContext.request.contextPath}/emp" method="post" name="form" style="float:left;margin-left: 20px">
姓名<input type="text" name="firstName" value="${param.firstName}">
工资<input type="text" name="min" value="${param.min}">---
<input type="text" name="max" value="${param.max}">
<input type="hidden" name="pageNo">
<input type="submit" value="查询" class="btn btn-secondary">
</form>
<a style="float: right ;margin-right: 30px" class="btn btn-primary">添加</a>
</div>
<table class="table table-striped">
<tr style="text-align: center">
<td>员工编号</td>
<td>姓名</td>
<td>职位</td>
<td>工资</td>
<td>生日</td>
<td>电话</td>
<td>操作</td>
</tr>
<c:forEach items="${pageInfo.list}" var="emp">
<tr style="text-align: center">
<td>${emp.employeeId}</td>
<td>${emp.firstName}</td>
<td>${emp.jobId}</td>
<td>${emp.salary}</td>
<td>${emp.hiredate}</td>
<td>${emp.employeeId}</td>
<td>${emp.phoneNumber}</td>
<td><a class="btn btn-success">修改</a><a class="btn btn-danger">删除</a></td>
</tr>
</c:forEach>
</table>
</div>
<div style="margin-left:720px">
<%-- <nav aria-label="Page navigation example">--%>
<%-- <ul class="pagination">--%>
<%-- <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/emp?pageNo=${pageInfo.prePage}">上一页</a></li>--%>
<%-- <c:forEach items="${pageInfo.navigatepageNums}" var="num">--%>
<%-- <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/emp?pageNo=${num}">${num}</a></li>--%>
<%-- </c:forEach>--%>
<%-- <li class="page-item"><a class="page-link" href="${pageContext.request.contextPath}/emp?pageNo=${pageInfo.nextPage}">下一页</a></li>--%>
<%-- </ul>--%>
<%-- </nav>--%>
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="javascript:void(0)" onclick="getPageNo(1)">首页</a></li>
<li class="page-item"><a class="page-link" href="javascript:void(0)" onclick="getPageNo(${pageInfo.prePage})">上一页</a></li>
<c:forEach items="${pageInfo.navigatepageNums}" var="num">
<li class="page-item"><a class="page-link" href="javascript:void(0)" onclick="getPageNo(${num})">${num}</a></li>
</c:forEach>
<li class="page-item"><a class="page-link" href="javascript:void(0)" onclick="getPageNo(${pageInfo.nextPage})">下一页</a></li>
<li class="page-item"><a class="page-link" href="javascript:void(0)" onclick="getPageNo(${pageInfo.pages})">尾页</a></li>
</ul>
</nav>
</div>
<div style="margin-left:860px" >
共 ${pageInfo.pages} 页,当前第 ${pageInfo.pageNum} 页
</div>
<script>
function getPageNo(pageNo){
$("[name='pageNo']").val(pageNo);
$("[name='form']").submit();
}
</script>
</body>
</html>
效果图