zoukankan      html  css  js  c++  java
  • mybatis学习笔记五(与springMVC的集成)

    与springMVC的集成
    需要配置以下几个方面:
    1 web.xml配置spring dispatchservlet
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>MybatisDemo</display-name>


    <!-- 加载配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
    </context-param>
    <!-- spring的监听器 -->
    <listener>
    <description>spring监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- 防止spring内存溢出监听器 -->
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <!-- 配置 DispatcherServlet -->
    <servlet>
    <servlet-name>MybatisDemo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>MybatisDemo</servlet-name>
    <url-pattern>/</url-pattern>


    </servlet-mapping>
    <!-- 解决中文乱码问题 -->
    <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>
    </filter>
    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>




    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    </web-app>










    2 MybatisDemo-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="cn.yue.mybatis.controller" />
    <mvc:annotation-driven />


    <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
    <mvc:default-servlet-handler />


    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
    <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
    <value>.jsp</value>
    </property>
    </bean>




    </beans>




    3 spring的配置文件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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">






    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
    value="jdbc:mysql://127.0.0.1:3306/mybatis_demo?characterEncoding=utf8" />
    <property name="username" value="root" />
    <property name="password" value="yue4512138" />
    </bean>


    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>




    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--dataSource属性指定要用到的连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!--configLocation属性指定mybatis的核心配置文件 -->
    <property name="configLocation" value="classpath:config/Configuration.xml" />
    <!-- 所有配置的mapper文件 -->
    <property name="mapperLocations" value="classpath*:cn/yue/mybatis/mapper/*.xml" />
    </bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.yue.mybatis.inter" />
    </bean>






    </beans>






    4 编写控制controller
    @Controller
    @RequestMapping("/article")
    public class UserController {
    @Autowired
    IUserOperation userMapper;


    @RequestMapping("/list")
    public ModelAndView listAll(HttpServletRequest request, HttpServletResponse response) {
    List<Article> articles = userMapper.getUserArticles(1);
    ModelAndView mv = new ModelAndView("list");
    mv.addObject("articles", articles);
    return mv;
    }
    }






    5 页面视图
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ page isELIgnored="false"%>
    <!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=ISO-8859-1">
    <title>MybatisDemo</title>
    </head>
    <body>
    <c:forEach items="${articles}" var="item"> ${item.id }--${item.title}--${item.content }
    <br />
    </c:forEach>
    </body>
    </html>




    6 测试

    http://localhost:8089/article/list


    参考:http://blog.csdn.net/techbirds_bao/article/details/9233599/

  • 相关阅读:
    python(内置高阶函数)
    cms 环境搭建
    cookie、session 和 token 区别
    接口用例设计
    python(字符编码与转码)
    从北斗卫星时钟(北斗校时器)发展纵论世界卫星导航新格局
    北斗授时系统(GPS授时设备)错一秒会怎样?京准电子科技
    北斗校时服务器(GPS时钟服务器)在电力调度系统应用
    GPS卫星时钟(北斗授时设备)在监狱管理系统方案
    NTP校时(网络对时服务器)IPC网络摄像机时钟同步
  • 原文地址:https://www.cnblogs.com/retacn-yue/p/6194271.html
Copyright © 2011-2022 走看看