Maven整合ssh

作者: l_sivan | 来源:发表于2017-03-29 09:43 被阅读250次

    说几句:
    这几天在忙春招的东西,看了很多东西,但是都没有总结,感觉很急躁。年轻人,从小听到大的一句话就是戒骄戒躁,然而最近,真的开始有点怀疑自己,有点燥。
    没能做到爱其所司,脚踏实地,才发现这两个这么难,人总会不自觉地希望少走弯路,甚至是直接达到目的。能投机取巧也是聪明,但这不和脚踏实地相悖。
    还有发现之前的选择有错误,学了很多可以看到效果的东西,比如框架,开发辅助工具。但是最有问题的就是,这些东西都太“简单”了。这个简单的意思是想达到可以用来做项目的水平,做到优化的水平,没有什么大的难度,网上的视频,文章一找一大堆。Crtl+C,Crtl+V谁不会。
    而且最急躁的一件事,就是学框架的时候还是通过学习视频学的,没能静下心看官方文档,加上之前学习基础的时候没能总结,没做知识点的梳理。这造成了之前政治上的一个东西:下层基础不好,上层结构出问题。所以现在就是虚于表面,有点难受。好好学习吧,人生就是学到老的。

    背景:之前在团队部署的一个展示性网页要加后台,然后之前开发东西都是直接自己把包放进去的,现在用maven来试下,说干就干。

    环境

    Fedora23 + MySQL5.7+Maven3.3.3+Eclipse

    框架选择

    Spring4.3.7+Hibernate4.1.7+SpringMVC4.3.7

    新建webapp项目

    命令:

    <code>
    mvn archetype:generate -DgroupId=com.ycTime.XXX -DartifactId=XXX -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeCatalog=local
    </code>
    在项目工程目录下执行,然后用eclipse打开maven项目就行了。

    写pom.xml文件

    <code><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>org.SKT</groupId>
    <artifactId>SKT</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SKT Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>




    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.7.RELEASE</version>
    </dependency>

        <!-- spring dao层依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- spring web相关依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- hibernate4 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.7.Final</version>
        </dependency>
        <!-- mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2.1-b03</version>
            <scope>provided</scope>
        </dependency>
        <!-- fastJson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.24</version>
        </dependency>
        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!-- commons-fileUpload -->
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    

    <build>
    <finalName>SKT</finalName>
    </build>
    </project>
    </code>

    写配置文件

    • 在resources目录下新建文件夹configs,放入所有的配置文件
    • db.properties(发现用配置文件写不用做转义处理,比如&不用转成& amp;)

    <code>user=root
    password=root
    driverClass=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql://localhost:3306/XXX?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
    maxPoolSize=20
    minPoolSize=5
    maxStatements=100
    acquireIncrement=2</code>

    • hibernate.cfg.xml

    <code><?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory>
    <property name="dialect">
    org.hibernate.dialect.MySQL5InnoDBDialect
    </property>
    <property name="hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>
    <property name="minPoolSize" >10</property>

    <property name="maxPoolSize" >100</property>

    <property name="maxIdleTime" >1800</property>

    <property name="acquireIncrement" >3</property>
    <property name="maxStatements" >1000</property>
    <property name="initialPoolSize">10</property>

    <property name="idleConnectionTestPeriod" >60</property>

    <property name="acquireRetryAttempts" >30</property>
    <property name="breakAfterAcquireFailure" >true</property>
    <property name="testConnectionOnCheckout" >false</property>

        <property name="hibernate.c3p0.max_size">20</property>
        <!-- 最小连接数 -->
        <property name="hibernate.c3p0.min_size">5</property>
        <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
        <property name="hibernate.c3p0.timeout">120</property>
        <!-- 最大的PreparedStatement的数量 -->
        <property name="hibernate.c3p0.max_statements">100</property>
        <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒 -->
        <property name="hibernate.c3p0.idle_test_period">120</property>
        <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <!-- 每次都验证连接是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>
    </session-factory>
    

    </hibernate-configuration>
    </code>

    • ApplicationContext.xml

    <code><?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <context:property-placeholder location="/WEB-INF/classes/configs/db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}">
        </property>
        <property name="jdbcUrl" value="${jdbcUrl}">
        </property>
        <property name="user" value="${user}">
        </property>
        <property name="password" value="${password}">
        </property>
        <property name="maxPoolSize" value="${maxPoolSize}">
        </property>
        <property name="minPoolSize" value="${minPoolSize}">
        </property>
        <property name="maxStatements" value="${maxStatements}">
        </property>
        <property name="acquireIncrement" value="${acquireIncrement}">
        </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="/WEB-INF/classes/configs/hibernate.cfg.xml"></property>
        <property name="packagesToScan" value="com.ycTime.bean" />
        <property name="hibernateProperties">
            <props>
            <!-- key这句很重要,后面有解释 -->  
                <prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>
                <prop key="hibernate.c3p0.idle_test_period">60</prop>
                <prop key="hibernate.c3p0.acquire_increment">2</prop>
                <prop key="hibernate.c3p0.validate">true</prop>
            </props>
        </property>
    </bean>
    <context:component-scan base-package="com.ycTime.controller"></context:component-scan>
    <context:component-scan base-package="com.ycTime.dao"></context:component-scan>
    <context:component-scan base-package="com.ycTime.service"></context:component-scan>
    <context:component-scan base-package="com.ycTime.util"></context:component-scan>
    <context:component-scan base-package="com.ycTime.bean"></context:component-scan>
        <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <bean id="multipartResolver"    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" />
    

    </beans>
    </code>

    写web.xml

    <code><?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"
    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>SKT</display-name>
    <welcome-file-list>
    <welcome-file>skt.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
    <param-name>contextConfigLocation</param-name>

        <param-value>classpath*:/configs/ApplicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/configs/ApplicationContext.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    </web-app></code>
    上面的contextConfigLocation那里用了一个classpath*,用classpath的时候会报没找到ApplicationContext.xml的错误。

    至此,SpringMVC+Hibernate+Spring的整合已经完成,剩下的就是基于注解的各个模块的开发,也没什么特殊的。

    相关文章

      网友评论

        本文标题:Maven整合ssh

        本文链接:https://www.haomeiwen.com/subject/dnqeottx.html