SSM整合配置文件(Springmvc+Spring+Mybatis)

时间:2023-01-24 16:35:15
/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <!-- 配置 Spring -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/applicationContext.xml</param-value>    </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.xml</param-value>        </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.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:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    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" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <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" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />        <property name="mapperLocations" value="classpath:us/xuhang/project/mapper/*.xml" />    <!-- 加载mapper文件 -->    </bean>    <!-- 事务 -->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <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.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:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    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" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 通过setter方法注入前缀 /WEB-INF/jsps/showMsg.jsp-->        <property name="prefix" value="/WEB-INF/jsps/" />        <!-- 通过setter方法注入后缀 -->        <property name="suffix" value=".jsp" />    </bean>        <!-- 支持文件上传 -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    </bean></beans>classpath:mybatis/sqlMapConfig.xml<?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>        <typeAlias type="us.xuhang.project.domain.Person" alias="Person"/>    </typeAliases>    <!-- 这里不需要再注册mybatis的SQL映射文件 ,因为在Spring的配置文件中配置sqlSessionFactory时注册了mapper的路径--></configuration>     


jdbc.properties:jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/eclipseweb?characterEncoding=utf-8jdbc.username=rootjdbc.password=123456c3p0.pool.size.max=20c3p0.pool.size.min=5c3p0.pool.size.ini=3c3p0.pool.size.increment=2


log4j.properties:log4j.rootLogger=info, stdout#Consolelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%-5p - %m%n#LogFilelog4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=../logs/jklog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n#Daily LogFilelog4j.appender.dayfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.dayfile.File=../logs/jklog.loglog4j.appender.dayfile.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.dayfile.Append=true log4j.appender.dayfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.dayfile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n# LOGIN_LOG_P db config ###log4j.appender.logindb=org.apache.log4j.jdbc.JDBCAppenderlog4j.appender.logindb.BufferSize=1log4j.appender.logindb.driver=com.mysql.jdbc.Driverlog4j.appender.logindb.URL=jdbc:mysql://localhost:3306/jkdb?characterEncoding=utf-8log4j.appender.logindb.user=rootlog4j.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.PatternLayoutlog4j.logger.org.apache=INFOlog4j.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.xml:<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/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>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-ooxml</artifactId>            <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>