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/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://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);@Resourceprivate 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 stubreturn mapper.getUserListPage(beginIndex, pageSize);}public Integer getUserCount() {// TODO Auto-generated method stubreturn 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表对应的数据库表的相关字段

转载于:https://www.cnblogs.com/m97i/p/7613781.html

springMVC+mybatis相关推荐

  1. Java项目:在线水果商城系统(java+JSP+Spring+SpringMVC +MyBatis+html+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能: 区分为管理员用户和普通用户,普通用户:用户注册登录,首页水果展示,商品分类展示,购物车添加,下单,订单查询,个人信息修 ...

  2. Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--My ...

  3. idea springmvc_手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路

    手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis 本文中的图片用了个人服务器存储,网速较慢,各位老司机耐心等待. 文章开始之前,小编准备先来一波福利youhuo ...

  4. SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

    使用IDEA创建Spring + SpringMVC + MyBatis 框架的Maven的项目. 一. 创建maven项目 1. File -> New Module,进入创建项目窗口. 2. ...

  5. (五)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建根项目

    上一篇我们介绍<springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven模块规划>,从今天开始,我们将对代码的每一个构建做详细的记录,能够帮助大家 ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(十一)——SpringMVC架构

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6985816.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十)--My ...

  7. java B2B2C 源码 多级分销springmvc mybatis多租户电子商城系统--配置中心服务化和高可用...

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.电子商务平台源码请加企鹅求求:一零三八七七四六二六. 这样就存在了一个问题,客户端和服务端的耦合性太高,如果server ...

  8. java B2B2C springmvc mybatis电子商务平台源码-Consul服务发现原理...

    Consul 是什么 Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License ...

  9. SpringMVC + MyBatis整合 【转】

    为什么80%的码农都做不了架构师?>>>    环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3 网络上关于这个架构的搭建文章,实在是太 ...

  10. Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7098753.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)--S ...

最新文章

  1. 九大网络安全失误,需要注意
  2. 生物信息行业应该具备哪些基础素养?重点应该放在计算机方面还是生物方面或者说其他?
  3. ABAP中的动态运算函数
  4. tf dense layer两种创建方式的对比和numpy实现
  5. MS SQL入门基础:数据库中的锁
  6. 【解决问题】idea启动本地tomcat访问localhost:8080报404错误
  7. 应用css div进行页面布局设计,利用CSS与DIV进行页面布局.ppt
  8. 读书笔记∣疯狂XML讲义
  9. 创建可扩展性系统-13-2
  10. 演示账号激活的过程:注册——向指定邮箱发送邮件——用户登录邮箱,激活账号
  11. 最有效的清理C盘/win10如何给系统盘瘦身
  12. 图像识别从零写出dnf脚本关键要点
  13. NI CompactRIO9035与elmo电机驱动联合仿真系统搭建教程(二)
  14. Nginx调度器 Nginx常见问题-
  15. 21)C语言之悬空else
  16. 根据经纬度查找附近的人计算公式
  17. 【Git】pull 分支报错 fatal: Need to specify how to reconcile divergent branches...
  18. 乐蜂网八周年店庆有感
  19. 高科技玩具这么无趣,为何还要鼓励孩子玩?
  20. [经验分享] 覃超直播课学习笔记

热门文章

  1. 有了这个列表,程序员不愁没练手的小项目了
  2. 三种方法求最大公约数
  3. LeetCode contest 182 5369. 统计作战单位数
  4. 马斯克的星链计划并不是在免费送网络而是准备收租
  5. html5判断文字超过几行,判断文字数量超过2行 添加展开按钮 未超过两行则不显示按钮 溢出部分显示省略号...
  6. Oracle PL/SQL开发基础(第十五弹:同义词)
  7. AXI4总线协议总结
  8. axi_ddr_top
  9. 磁珠 符号_关于PCB原理图中的FB-FB是磁珠的符号-电子元器件-电路图
  10. 计算机实习生听课记录,舞蹈课实习听课记录