springMVC+mybatis
xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</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:DispatcherServlet-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
applicationContext-mybatis.xml配置文件(spring,mybatis集成的配置文件)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 读取配置文件 两种方法 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}?useUnicode=true&characterEncoding=utf8"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <!-- 扫描事务注解 --> <tx:annotation-driven/> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置 MyBatis sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 生成mapper实例 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao"></property> </bean> </beans>
DispatcherServlet-servlet.xml配置文件(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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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.xsd"> <!-- 扫描注解类 --> <context:component-scan base-package="dao,pojo,service,controller"></context:component-scan> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
mybatis-config.xml 配置文件(mybatis)
<?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> <!-- 定义实体类的别名 --> <typeAliases> <package name="pojo"/> </typeAliases> </configuration>
=====================
@Controller
public class IndexController {
@Resource
private UserService userService;
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(){
return "index";
}
--------------------------------------
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
=============随便写的分页,没注重格式,只是实现功能====================================
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <c:forEach var="user" items="${userlist}"> ${user.id} ${user.userName} ${user.password}<br/> </c:forEach> <a href="<%=pageContext.getServletContext().getContextPath()%>/userListPage/1">首页</a> <a href="<%=pageContext.getServletContext().getContextPath()%>/userListPage/${page.beginIndex-1}">上一页</a> <a href="<%=pageContext.getServletContext().getContextPath()%>/userListPage/${page.beginIndex+1}">下一页</a> <a href="<%=pageContext.getServletContext().getContextPath()%>/userListPage/${page.totalPageCount}">尾页</a> 总共有${page.totalCount}条记录, 当前第${page.beginIndex}页 </body> </html>
Controller
@Controller //类似Struts的Action public class UserController { private Logger loger = Logger.getLogger(UserController.class); @Resource private UserService userService; @RequestMapping(value="/userListPage") public String userListPage(){ return "redirect:/userListPage/1"; } @RequestMapping(value="/userListPage/{beginIndex}",method=RequestMethod.GET) public ModelAndView userListPage(@PathVariable String beginIndex){ ModelAndView modelAndView = new ModelAndView("userList"); Page page=new Page(); page.setTotalCount(userService.getUserCount()); if(Integer.valueOf(beginIndex)==1||Integer.valueOf(beginIndex)<1){ page.setBeginIndex(1); }else if(Integer.valueOf(beginIndex)>page.getTotalPageCount()){ page.setBeginIndex(page.getTotalPageCount()); }else{ page.setBeginIndex(Integer.valueOf(beginIndex)); } List<User> userlist=userService.getUserListPage((page.getBeginIndex()-1)*page.getPageSize(),page.getPageSize()); modelAndView.addObject("userlist", userlist); modelAndView.addObject("page", page); return modelAndView; }
page工具类
public class Page { private Integer totalCount;//总记录数 private Integer totalPageCount;//总页数 private Integer pageSize=3;//页面长度 private Integer beginIndex;//分页开始的位置 public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; setTotalPageCount(); } public Integer getTotalPageCount() { return totalPageCount; } public void setTotalPageCount() { if(totalCount%pageSize>0){ this.totalPageCount = totalCount/pageSize+1; }else{ this.totalPageCount = totalCount/pageSize; } } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getBeginIndex() { return beginIndex; } public void setBeginIndex(Integer beginIndex) { this.beginIndex = beginIndex; } }
serviceImpl
public List<User> getUserListPage(Integer beginIndex, Integer pageSize) { // TODO Auto-generated method stub return mapper.getUserListPage(beginIndex, pageSize); } public Integer getUserCount() { // TODO Auto-generated method stub return mapper.getUserCount(); }
userMapper.java
public interface UserMapper { List<User> getUserListPage(Integer beginIndex,Integer pageSize); Integer getUserCount(); }
userMapper.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="org.project.dao.user.UserMapper"> <select id="getUserListPage" resultType="User" parameterType="java.lang.Integer"> select * from user limit #{0},#{1} </select> <select id="getUserCount" resultType="java.lang.Integer"> select count(*) from user </select> </mapper>
还一个pojo,user表对应的数据库表的相关字段