实现Spring+SpringMVC+MyBatis的整合

maven依赖配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.tiangroupId>
    <artifactId>P_bulidartifactId>
    <version>1.0version>
    <packaging>pompackaging>
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.4version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.1version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.38version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.2version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        
        <dependency>
            <groupId>taglibsgroupId>
            <artifactId>standardartifactId>
            <version>1.1.2version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxwsartifactId>
            <version>3.2.0version>
        dependency>
        
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-transports-http-jettyartifactId>
            <version>3.2.0version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.5version>
        dependency>
        
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.3version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.39version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>javax.servlet.jsp-apiartifactId>
            <version>2.3.1version>
            <scope>providedscope>
        dependency>
    dependencies>
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.0version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>
project>

web.xml配置


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>DRP_Systemdisplay-name>
  
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
      
     <servlet>
    <servlet-name>cxfservlet-name>
     <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
    servlet>
    <servlet-mapping>
     <servlet-name>cxfservlet-name>
    <url-pattern>/ws/tian/*url-pattern>
    servlet-mapping>
    
    <servlet>
        <servlet-name>springMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMVC.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
     <filter>
        <filter-name>springEncodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter> 
    <filter-mapping>
        <filter-name>springEncodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    <error-page>
        <error-code>404error-code>
        <location>/404.jsplocation>
    error-page>
    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>
web-app>

自定义标签


<taglib
    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-jsptaglibrary_2_1.xsd"
    version="2.1">


<tlib-version>1.0tlib-version>

<short-name>pshort-name>

<uri>/mytags/puri> 
   
 <tag>
 
 <name>pagename>
 
 <tag-class>com.tian.jsp.PageTagtag-class>
 
 <body-content>emptybody-content>
 
 <attribute>
 <description>分页数据的路径description>
 <name>pageUrlname>
 <required>truerequired>
 <rtexprvalue>truertexprvalue>
 attribute>
 
 <attribute>
 <description>当前页数description>
 <name>currentPagename>
 <required>truerequired>
 <rtexprvalue>truertexprvalue>
 attribute>
 
 <attribute>
 <description>总页数description>
 <name>totalPagename>
 <required>truerequired>
 <rtexprvalue>truertexprvalue>
 attribute>
 tag>  
taglib>

自定义标签类

package com.tian.jsp;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 分页JSP
 * @author DreamT
 *
 */
public class PageTag extends SimpleTagSupport{

    private Long currentPage;// 当前页
    private Long totalPage;// 总页数
    private String pageUrl;// 提供分页数据的路径

    public Long getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Long currentPage) {
        this.currentPage = currentPage;
    }

    public Long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    public String getPageUrl() {
        return pageUrl;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }

    @Override
    public void doTag() throws JspException, IOException {
        StringBuffer buffer = new StringBuffer();
        //组合请求的url
        pageUrl += "?pageNo=";
        //拼接js代码,目标为了实现跳转
        buffer.append("");
        //拼接HTML标签
        buffer.append("
"); //拼接首页 buffer.append("首页"); //拼接上一页 if (currentPage <= 1) { currentPage = 1L; buffer.append("上一页"); } else { buffer.append("上一页"); } //拼接当前显示信息 buffer.append("当前 " + currentPage + "/" + totalPage + " 页"); //拼接跳转 buffer.append(""); //拼接下一页 if (currentPage >= totalPage) { currentPage = totalPage; buffer.append("下一页"); } else { buffer.append("下一页"); } //拼接尾页 buffer.append("尾页"); buffer.append("
"
); //输出到页面 getJspContext().getOut().print(buffer.toString()); } }

applicatonContext.xml配置


<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://mybatis.org/schema/mybatis-spring
         http://mybatis.org/schema/mybatis-spring.xsd
         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    
    <jaxws:server serviceName="user" address="/user"
        serviceClass="com.tian.webserver.IUserWebServer">
        
        <jaxws:serviceBean>
            <ref bean="userServer">ref>
        jaxws:serviceBean>
    jaxws:server>
    
    <mybatis:scan base-package="com.tian.mapper" />
    
    <context:component-scan base-package="com.tian">
        
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    context:component-scan>

    
    <context:property-placeholder location="classpath:dbconfig.properties" />

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        
        <property name="filters" value="stat" />

        
        <property name="maxActive" value="20" />
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />

        
        <property name="maxWait" value="60000" />

        
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="20" />
    bean>
    
    <bean id="sessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>

        
        
    bean>
    
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>
    <tx:annotation-driven transaction-manager="txManager" />
    <aop:aspectj-autoproxy>aop:aspectj-autoproxy>

beans>

springMvc.xml配置


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <context:component-scan base-package="com.tian.controller">context:component-scan>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8">constructor-arg>
            bean>
            
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8">property>
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/jsonvalue>
                        <value>text/html;charset=UTF-8value>
                    list>
                property>
                <property name="fastJsonConfig" ref="fastJsonConfig">property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

    
    <bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        
        <property name="charset" value="UTF-8">property>
        
        <property name="dateFormat" value="yyyy-MM-dd">property>
        <property name="serializerFeatures">
            <list>
                <value>WriteNullListAsEmptyvalue>
            list>
        property>
    bean>
    
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        
        <property name="suffix" value=".jsp">property>
    bean>
    
    
    
    <mvc:default-servlet-handler />
    
    
    
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="UTF-8">property>
        
        <property name="maxUploadSize" value="#{10*1024*1024}">property>
    bean>

beans>

你可能感兴趣的:(spring,spring,mvc,mybatis,maven,webservice,笔记而已)