你的位置:首页 > ASP.net教程

[ASP.net教程]SSM整合配置文件(Springmvc+Spring+Mybatis)


/WEB-INF/web.

 

<?

<web-app version="2.4" 

    

    xsi:schemaLocation="http://java.sun.com/

http://java.sun.com/

    <!-- 配置 Spring -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring/applicationContext.

    </context-param>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <!-- 配置springmvc -->

    <servlet>

        <servlet-name>springmvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:springmvc/springmvc-servlet.

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <url-pattern>*.action</url-pattern>

    </servlet-mapping>

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

 

classpath:spring/applicationContext.

 

<?

<beans 

    

    

    

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

            http://www.springframework.org/schema/mvc 

            http://www.springframework.org/schema/mvc/spring-mvc-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/aop 

            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

            http://www.springframework.org/schema/tx 

            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    <!-- 扫描service、dao组件 -->

    <context:component-scan base-package="us.xuhang.project.service,us.xuhang.project.dao" />

    <!-- 分解配置 jdbc.properites -->

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

    <!-- 数据源c3p0 -->

    <bean id="dataSource" >

        <property name="driverClass" value="${jdbc.driverClassName}" />

        <property name="jdbcUrl" value="${jdbc.url}" />

        <property name="user" value="${jdbc.username}" />

        <property name="password" value="${jdbc.password}" />

        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />

        <property name="minPoolSize" value="${c3p0.pool.size.min}" />

        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />

        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />

    </bean>

    <!-- sessionFactory 将spring和mybatis整合 -->

    <bean id="sqlSessionFactory" >

        <property name="dataSource" ref="dataSource" />

        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.

        <property name="mapperLocations" value="classpath:us/xuhang/project/mapper/*.

    </bean>

    <!-- 事务 -->

    <bean id="txManager" >

        <property name="dataSource" ref="dataSource" />

    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">

        <tx:attributes>

            <tx:method name="insert*" propagation="REQUIRED" />

            <tx:method name="update*" propagation="REQUIRED" />

            <tx:method name="delete*" propagation="REQUIRED" />

            <tx:method name="find" read-only="true" />

            <tx:method name="get" read-only="true" />

            <tx:method name="*" propagation="REQUIRED" />

        </tx:attributes>

    </tx:advice>

    <aop:config>

        <aop:pointcut expression="execution(* us.xuhang.project.service.*.*(..))"

            id="pointCut" />

        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />

    </aop:config>

</beans>            

 

classpath:springmvc/springmvc-servlet.

<?

<beans 

    

    

    

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

    http://www.springframework.org/schema/mvc 

    http://www.springframework.org/schema/mvc/spring-mvc-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/aop 

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

    http://www.springframework.org/schema/tx 

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    <!-- 注解方式 -->

    <mvc:annotation-driven />

    <!-- 自动扫描Controller -->

    <context:component-scan base-package="us.xuhang.project.controller" />

    <!-- 配置一个springmvc框架的视图解析器 -->

    <bean id="viewResolver" >

        <!-- 通过setter方法注入前缀 /WEB-INF/jsps/showMsg.jsp-->

        <property name="prefix" value="/WEB-INF/jsps/" />

        <!-- 通过setter方法注入后缀 -->

        <property name="suffix" value=".jsp" />

    </bean>

    

    <!-- 支持文件上传 -->

    <bean id="multipartResolver" >

    </bean>

</beans>

classpath:mybatis/sqlMapConfig.

<?

<!DOCTYPE configuration

    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

    <!-- 定义别名 -->

    <typeAliases>

        <typeAlias type="us.xuhang.project.domain.Person" alias="Person"/>

    </typeAliases>

    <!-- 这里不需要再注册mybatis的SQL映射文件 ,因为在Spring的配置文件中配置sqlSessionFactory时注册了mapper的路径-->

</configuration>     

 

jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/eclipseweb?characterEncoding=utf-8

jdbc.username=root

jdbc.password=123456

c3p0.pool.size.max=20

c3p0.pool.size.min=5

c3p0.pool.size.ini=3

c3p0.pool.size.increment=2

 

log4j.properties:

log4j.rootLogger=info, stdout

#Console

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%-5p - %m%n

#LogFile

log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=../logs/jklog.log

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

#Daily LogFile

log4j.appender.dayfile=org.apache.log4j.DailyRollingFileAppender 

log4j.appender.dayfile.File=../logs/jklog.log

log4j.appender.dayfile.DatePattern='.'yyyy-MM-dd'.log' 

log4j.appender.dayfile.Append=true 

log4j.appender.dayfile.layout=org.apache.log4j.PatternLayout

log4j.appender.dayfile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# LOGIN_LOG_P db config ###

log4j.appender.logindb=org.apache.log4j.jdbc.JDBCAppender

log4j.appender.logindb.BufferSize=1

log4j.appender.logindb.driver=com.mysql.jdbc.Driver

log4j.appender.logindb.URL=jdbc:mysql://localhost:3306/jkdb?characterEncoding=utf-8

log4j.appender.logindb.user=root

log4j.appender.logindb.password=root

##    MySQL

#log4j.appender.logindb.sql=INSERT INTO LOGIN_LOG_P(LOGIN_LOG_ID,LOGIN_NAME,IP_ADDRESS,LOGIN_TIME) VALUES(uuid(),'%X{userId}|%X{userName}','%X{ip}','%d{yyyy-MM-dd HH:mm:ss}')

log4j.appender.logindb.sql=INSERT INTO LOGIN_LOG_P(LOGIN_LOG_ID,LOGIN_NAME,IP_ADDRESS,LOGIN_TIME) VALUES(uuid(),'','',NOW())

log4j.appender.logindb.layout=org.apache.log4j.PatternLayout

log4j.logger.org.apache=INFO

log4j.logger.cn.itcast.jk=DEBUG

 

 

  us.xuhang.project.controller.PersonController:

 

package  us.xuhang.project.controller; 
import  java.util.List; 
import  java.util.UUID; 
import  javax.servlet.http.HttpServletRequest; 
import  javax.servlet.http.HttpServletResponse; 
import  org.springframework.beans.factory.annotation.Autowired; 
import  org.springframework.stereotype.Controller; 
import  org.springframework.ui.Model; 
import  org.springframework.web.bind.annotation.RequestMapping; 
import  org.springframework.web.servlet.ModelAndView; 
import  org.springframework.web.servlet.mvc.AbstractController; 
import  org.springframework.web.servlet.mvc.support.RedirectAttributes; 
import  us.xuhang.project.domain.Person; 
import  us.xuhang.project.service.PersonService; 
@Controller( "personController") 
@RequestMapping( "/person") 
public   class  PersonController{ 
    @Autowired 
    PersonService personService;      
    @RequestMapping( "/list.action") 
      public  String listPerson(Person person, Model model){ 
        List personList   =  (List)   this.personService.listPerson(person); 
        model.addAttribute( "personList", personList); 
          return   "listperson"; 
    } 
    @RequestMapping( "/save.action") 
      public  String savePerson(Person person){ 
        person.setPid(UUID.randomUUID().toString()); 
          this.personService.savePerson(person); 
          return   "listperson"; 
    } 
    @RequestMapping( "/view.action") 
      public  String viewPerson(String pid, Model model){ 
        Person p   =   this.personService.findPersonById(pid); 
        model.addAttribute( "p", p); 
          return   "viewperson"; 
    } 
    @RequestMapping( "/updatePerson.action") 
      public  String updatePerson(Person person, RedirectAttributes att){ 
          this.personService.updatePerson(person); 
          //控制器之间带参数的重定向跳转 
        att.addAttribute( "name", person.getName()); 
          return   "redirect:list.action"; 
    } 

 

pom.

<project 

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>us.xuhang</groupId>

    <artifactId>c</artifactId>

    <packaging>war</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <name>c Maven Webapp</name>

    <url>http://maven.apache.org</url>

    <properties>

        <org.springframework.version>3.2.2.RELEASE</org.springframework.version>

    </properties>

    

    <dependencies>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-orm</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-test</artifactId>

            <version>3.1.3.RELEASE</version>

            <type>jar</type>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjweaver</artifactId>

            <version>1.7.2</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.2.2</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis-spring</artifactId>

            <version>1.2.0</version>

        </dependency>

        <dependency>

            <groupId>c3p0</groupId>

            <artifactId>c3p0</artifactId>

            <version>0.9.1.2</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.10</version>

        </dependency>

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>1.2.13</version>

        </dependency>

        <dependency>

            <groupId>org.apache.poi</groupId>

            <artifactId>poi</artifactId>

            <version>3.9</version>

        </dependency>

        <dependency>

            <groupId>org.apache.poi</groupId>

            <artifactId>poi-oo

            <version>3.9</version>

        </dependency>

        <dependency>

            <groupId>jfree</groupId>

            <artifactId>jfreechart</artifactId>

            <version>1.0.13</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.9</version>

        </dependency>

        <dependency>

            <groupId>commons-fileupload</groupId>

            <artifactId>commons-fileupload</artifactId>

            <version>1.2.2</version>

        </dependency>

        <dependency>

            <groupId>commons-io</groupId>

            <artifactId>commons-io</artifactId>

            <version>2.0.1</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet.jsp.jstl</groupId>

            <artifactId>jstl-api</artifactId>

            <version>1.2</version>

        </dependency>

        <dependency>

            <groupId>org.glassfish.web</groupId>

            <artifactId>jstl-impl</artifactId>

            <version>1.2</version>

        </dependency>

        <dependency>

            <groupId>jexcelapi</groupId>

            <artifactId>jxl</artifactId>

            <version>2.4.2</version>

        </dependency>

        <dependency>

            <groupId>org.apache.tomcat</groupId>

            <artifactId>servlet-api</artifactId>

            <version>6.0.33</version>

            <scope>provided</scope>

        </dependency>

        <!-- 防止和tomcat中的jar冲突 -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>2.5</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>javax.servlet.jsp</groupId>

            <artifactId>jsp-api</artifactId>

            <version>2.1</version>

            <scope>provided</scope>

        </dependency> 

    </dependencies>

    <build>

        <finalName>c</finalName>

    </build>

</project>

 整理的比较粗略,将代码分享给大家,【源码地址获取】