<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> </dependencies>
导入了mybatis-spring依赖也还是需要mybatis依赖的。
依赖版本如下:
<spring.version>5.2.0.RELEASE</spring.version> <druid.version>1.1.20</druid.version> <mybatis.version>3.5.2</mybatis.version> <junit.version>4.12</junit.version> <servlet.version>3.1.0</servlet.version> <mysql.version>5.1.47</mysql.version> <jstl.version>1.2</jstl.version> <mybatis.spring.version>2.0.2</mybatis.spring.version> <pageHelper.version>5.1.10</pageHelper.version>
创建出各个模块:entity,dao,service,web(按需求增加或减少,不是一定的)
给每个模块的pom.xml文件添加对应需要的依赖。
将需要的实体类编写,添加相应的属性,getter(),setter(),toString()....方法。
package com.dao; import com.entity.Employee; import org.apache.ibatis.annotations.Select; import java.util.List; public interface EmpDao { List<Employee> queryAll(); }
EmpService接口:
package com.service; import com.entity.Employee; import java.util.List; public interface EmpService { List<Employee> queryAll(); }
EmpServiceImpl实现类:
package com.service.impl; import com.dao.EmpDao; import com.entity.Employee; import com.service.EmpService; import java.util.List; public class EmpServiceImpl implements EmpService { private EmpDao empDao; //通过依赖注入empDao public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } @Override public List<Employee> queryAll() { return empDao.queryAll(); } }
applicationContext.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:mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--普通创建dataSource的bean方法--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/empdb"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--创建sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--传入dataSource--> <property name="dataSource" ref="dataSource"/> <!--读取employeeMapper.xml文件--> <property name="mapperLocations" value="classpath:employeeMapper.xml"/> <!--开启日志--> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/> </bean> </property> <!--开启分页--> <property name="plugins"> <list> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> supportMethodsArguments=true resonable=true </value> </property> </bean> </list> </property> </bean> <!--自动生成com.dao包下的所有类的bean--> <mybatis:scan base-package="com.dao"/> <!--自动注入empService需要的bean--> <bean id="empService" class="com.service.impl.EmpServiceImpl" autowire="byType"/> </beans>
使用employeeMapper.xml能够保留mybatis的优点,更加灵活,降低依赖耦合
<?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.dao.EmpDao"> <select id="queryByPageNum" resultType="com.entity.Employee"> select id,username,salary from employee order by id </select> </mapper>
PageEmpServlet类:
package com.controller; import com.entity.Employee; import com.github.pagehelper.PageInfo; import com.service.EmpService; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/pageEmp") public class PageEmpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); Integer pageNum = 1; try { pageNum = Integer.valueOf(req.getParameter("pageNum")); }catch (NullPointerException e){ throw new RuntimeException("未发现指定参数",e); }finally { //得到context对象,通过web.xml绑定的监听类的方法创建,单一 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()); EmpService empService = context.getBean("empService", EmpService.class); List<Employee> employees = empService.queryByPageNum(pageNum, 5); PageInfo<Employee> pageInfo = new PageInfo<>(employees); req.setAttribute("pageInfo",pageInfo); req.getRequestDispatcher("index.jsp").forward(req,resp); } } }
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"> <!--绑定容器配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext2.xml</param-value> </context-param> <!--设置监听器,这个监听器帮我们实现了创建单一的context容器对象--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: 李威 Date: 2019/10/24 Time: 15:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>分页查询</title> </head> <body> <table> <c:forEach items="${pageInfo.list}" var="emp"> <tr> <td>${emp.id}</td> <td>${emp.username}</td> <td>${emp.salary}</td> </tr> </c:forEach> <tr> <td><a href="/pageEmp?pageNum=1">首页</a></td> <td><a href="/pageEmp?pageNum=${pageInfo.prePage}">上一页</a><a href="/pageEmp?pageNum=${pageInfo.nextPage}">下一页</a></td> <td><a href="/pageEmp?pageNum=${pageInfo.navigateLastPage}">尾页</a></td> </tr> </table> </body> </html>
1、保留了employeeMapper.xml文件
2、使用了依赖jar包的监听类ContextLoaderListener
3、使用pagehelper实现分页
4、自动生成dao,自动注入dao到Service中
druid + mybatis-spring 不使用注解方式整合分页查询
原文:https://www.cnblogs.com/liweixml/p/11788931.html