Maven

Eclipse中建立Maven,点击File,选择new一个Maven项目html

勾选Create  a simple ...java

以下,补充完整,mysql

 

Group Id 组织名web

Artifact Id 项目名称spring

Version 版本号sql

packaging 打包方式 , 当项目为Java项目时,打成Jar包;当项目为web项目时,打成war包;当项目为其余...数据库

项目建立完成,在src/main/webapp/WEB-INF 下 ,新建web.xmlexpress

在pom.xml中 Add Denpendency(添加jar包),从本地仓库中获取须要的jar包(图中灰色是由于已经导入)apache

                                                          

 

Maven 中SSM整合spring-mvc

 web.xml以下

<?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <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>
    
<!-- 定义一个过滤器,解决项目中编码问题 --> <filter> <filter-name>characterEncoding</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>
<!--6其余地方关于编码的设置通通失效--> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听器,主要监听spring主配置文件,web文件最初开始执行 --> <context-param>
<!--类org.springframework.web.context.context.ContextLoaderListener中的成员contextConfigLocation,存储须要监听的文件路径--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:mybatis-spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

<!-- springmvc 在filter执行完毕后,执行servlet--> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param>
<!--指在tomcat执行完毕后,servlet才开始执行--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

 

在“src/main/resource”下(这也是web.xml中classpath默认的路径), 新建 spring-mvc.xml,spring-mybatis.xml,srping.xml文件

spring-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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">
     <!-- 启用spring mvc注解 -->
     <context:annotation-config></context:annotation-config>
     <!-- 设置扫描 -->
     <context:component-scan base-package="com.isoft.controller"/>
     <!-- 设置映射器、适配器 -->
     <mvc:annotation-driven></mvc:annotation-driven>
     <!-- 对专项页面的路径解析为:prefix前缀   suffix后缀  -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/" p:suffix=".jsp"></bean>
</beans>

 

org.springframework.web.context.ContextLoaderListener

spring.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: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.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 扫描 -->
     <context:component-scan base-package="com.isoft">
         <context:exclude-filter type="annotation" 
             expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
</beans>     

spring-mabtis.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: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.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd">
     
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"     value="jdbc:mysql://127.0.0.1:3306/Sufeng?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
        <property name="user"        value="root"></property>
        <property name="password"    value="1234"></property>
        <!-- 指定链接数据库链接池的最小链接数 -->
        <property name="minPoolSize" value="10"></property>
        <!-- 指定链接数据库链接池的最大链接数 -->
        <property name="maxPoolSize" value="20"></property>
        <!--初始化时获取三个链接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="15"></property>
        <!-- 指定链接数据库链接池的链接的最大空闲时间 -->
        <property name="maxIdleTime" value="120"></property>
        <!--当链接池中的链接耗尽的时候c3p0一次同时获取的链接数。Default: 3 -->
        <property name="acquireIncrement" value="5"></property>
        <property name="maxStatements" value="100"></property>
        <!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60"></property>
        <property name="automaticTestTable" value="c3p0testtable"></property>
    </bean>


    <!-- Mybatis文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" /> 
        <property name="dataSource" ref="dataSource" />
<!--映射文件路径-->
<!-- <properties name="mapperLocations" value="com/isoft/mapping/*.xml">--> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isoft.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </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="select*" read-only="true" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="start*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="finish*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> </beans>

mybatis-config文件

<?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="com.isoft.model.Person" alias="Person"/>  
    </typeAliases>     
    <mappers>  
        <mapper resource="com/isoft/mapping/PersonMapper.xml" />  
    </mappers>
</configuration> 

 

 

log4j.properties  设置日志

log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
相关文章
相关标签/搜索