ssm+maven+eclipse框架搭建

ssm框架搭建css

 

由於以前沒有獨立搭建過ssm框架,作的項目也都是從視頻上看着照作,沒有本身的想法,也不符合本身的意願,同時對ssm整合的瞭解並無起到太大的做用,因此本身嘗試搭建了一個ssm的框架,如今記錄一下個個配置,以便之後搭建框架出先問題能夠回來查閱。前端

 

 

一:首先建立maven工程(由於之前在學校搭建ssh框架本身找jar包,各類依賴不對應,各類jar包找不到,因此如今很喜歡maven這個依賴管理工具)。java

這中間須要注意的是下面這個圖要選擇webapp這個選項(以前看教程說是這個webapp的版本也有講究,可是我此次配置沒有改也成功了,因此就先無論了,之後遇到這個問題在說吧)mysql

 

 

二:名字什麼的都起好以後,點擊finish,如今的項目結構是這樣的web

 

 

跟我們平時的web項目相比少了一些東西(並且我這裏的maven是修改過conf下的setting文件的,否者他的JRE默認是1.5的,不過咱們都是須要修改的)。而後咱們右鍵項目進入BulidPath中spring

 

 

這裏很明顯少了server(由於我是在修改了maven的jar包地址的同時寫這篇博客,原本項目沒有server環境的話index.jsp是應該報錯的),咱們點擊上圖中的add Librarysql

 

 

把我畫紅線的兩個都配置成本地的(記住是你本地的,不是myeclipse中的,由於myeclipse之類的IDE都會模仿你本地的tomcat因此在配置的時候要格外注意。具體要怎麼正確使用本地的tomcat而不是被myeclipse克隆的tomcat欺騙了,我在之後的博客中會寫)數據庫

三:上面兩步完成後maven項目就算時建成了,下面開始整合ssm。這裏只記錄各個配置文件的寫法,並不寫這樣整合的原理(由於在下不會,不懂,往後會了再說)apache

先上一下項目結構,這是我認爲比較舒服的項目結構json

 

 

其中的各個文件就不介紹做用了,本身都懂,不懂看下名字也都知道是什麼意思

粘一下各個配置文件的內容

SqlMapConfig.xml(mybatis配置文件)

<?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>

 

</configuration>

 

applicationContext-dao.xml(spring整合mybaties配置文件)

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

 

<!-- 數據庫鏈接池 -->

<!-- 加載配置文件 -->

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

<!-- 數據庫鏈接池 -->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

destroy-method="close">

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

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

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

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

<property name="maxActive" value="10" />

<property name="minIdle" value="5" />

</bean>

<!-- spring管理sqlsessionfactory 使用mybatisspring整合包中的 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 數據庫鏈接池 -->

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

<!-- 加載mybatis的全局配置文件 -->

<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.zih.weixin.mapper" />

</bean>

</beans>

 

applicationContext.xml-service(spring掃描service類的配置文件)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

 

<context:component-scan base-package="com.zih.weixin.service"/>

 

</beans>

 

applicationContext-trans(spring配置自身事務以及aop的配置文件)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 事務管理器 -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 數據源 -->

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

</bean>

<!-- 通知 -->

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

<tx:attributes>

<!-- 傳播行爲 -->

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

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

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

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

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

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

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

<tx:method name="select*" propagation="SUPPORTS" read-only="true" />

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

</tx:attributes>

</tx:advice>

<!-- 切面 -->

<aop:config>

<aop:advisor advice-ref="txAdvice"

pointcut="execution(* com.zih.weixin.service.*.*(..))" />

</aop:config>

</beans>

Springmvc.xmlspringmvc配置文件)

<?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-4.0.xsd

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

    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

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

    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 

 

 

<context:component-scan base-package="com.zih.weixin.controller" />

<mvc:annotation-driven />

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

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

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

</bean>

//靜態資源映射(由於在web.xml中攔截的是/」,是全部資源,因此須要在這配置一下)

<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

 

</beans>

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">

<display-name>weixin</display-name>

<welcome-file-list>

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

</welcome-file-list>

<!-- 加載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>

<!-- log4j日誌文件配置 -->

<context-param>

    <param-name>log4jConfigLocation</param-name>

    <param-value>classpath:resource/log4j.properties</param-value>

</context-param>

<listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>

<!-- 解決post亂碼 -->

<filter>

<filter-name>CharacterEncodingFilter</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>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param> -->

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

 

<!-- springmvc的前端控制器 -->

<servlet>

<servlet-name>weixin</servlet-name>

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

<!-- contextConfigLocation不是必須的, 若是不配置contextConfigLocationspringmvc的配置文件默認在:WEB-INF/servletname+"-servlet.xml" -->

<init-param>

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

<param-value>classpath:spring/springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet>

        <servlet-name>wechatServlet</servlet-name>

        <servlet-class>com.zih.weixin.controller.WechatServlet</servlet-class>

    </servlet>

     <servlet-mapping>

        <servlet-name>wechatServlet</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

<servlet-mapping>

<servlet-name>weixin</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

 

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>com.zih</groupId>

  <artifactId>weixinDemo</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>weixinDemo Maven Webapp</name>

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

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

    <!-- spring -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-test</artifactId>

            <version>4.1.3.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>4.1.3.RELEASE</version>

        </dependency>

         <dependency>

 

    <groupId>dom4j</groupId>

 

    <artifactId>dom4j</artifactId>

 

    <version>1.1</version>

    </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>4.1.3.RELEASE</version>

        </dependency>

        <!-- xstream -->

        <dependency>

<groupId>com.thoughtworks.xstream</groupId>

<artifactId>xstream</artifactId>

<version>1.4.9</version>

</dependency>

 

        <!-- mybatis -->

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.1.1</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis-spring</artifactId>

            <version>1.1.1</version>

        </dependency>

 

        <!-- mysql -->

        <dependency>

            <groupId>mysql</groupId>

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

            <version>5.1.21</version>

        </dependency>

      <!-- mysql 阿里鏈接池 druid -->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>0.2.9</version>

        </dependency>

 

        <!-- spring aop  -->

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjweaver</artifactId>

            <version>1.7.1</version>

        </dependency>

 

        <!-- json  -->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.7</version>

        </dependency>

 

        <!-- 文件上傳包 -->

        <dependency>

            <groupId>commons-fileupload</groupId>

            <artifactId>commons-fileupload</artifactId>

            <version>1.2.2</version>

        </dependency>

 

        <!--servlet  -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>3.0-alpha-1</version>

        </dependency>

 

        <dependency>

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

            <artifactId>jsp-api</artifactId>

            <version>2.1</version>

            <scope>provided</scope>

        </dependency>

 

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

            <version>1.2</version>

        </dependency>

 

        <!-- 日誌包 -->

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>1.2.17</version>

        </dependency>

  </dependencies>

  <build>

    <finalName>weixinDemo</finalName>

  </build>

</project>

 

Db.properties(數據庫鏈接文件)

jdbc.driver=com.mysql.jdbc.Driver

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

jdbc.username=root

jdbc.password=123456

 

 

log4j.properties(日誌文件)

 

log4j.rootLogger=DEBUG,Console,File

#ERROR,WARN,INFO,DEBUG   \u65E5\u5FD7\u8F93\u51FA\u7B49\u7EA7\u4F9D\u6B21\u964D\u4F4E\uFF0C\u53EF\u4EE5\u6839\u636E\u81EA\u5DF1\u7684\u9700\u6C42\u81EA\u5DF1\u8C03\u6574\u8F93\u51FA\u7B49\u7EA7

 

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Threshold=DEBUG

log4j.appender.Console.Target=System.out

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

log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

 

log4j.appender.File=org.apache.log4j.DailyRollingFileAppender 

log4j.appender.File.File=${catalina.base}/wechatlogs/wechat.log

log4j.appender.File.Threshold=INFO

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

log4j.appender.File.Append=true

log4j.appender.File.ImmediateFlush=true

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

log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

 

 

而後就能夠開始寫代碼了,這中間有不少坑,也有不少要注意的地方,可是今天下班了,明天再補充吧

 

要注意有個webapp的版本要修改爲3.0,它自動生成的web.xml文件的頭也要修改,網上查一下ssm框架搭建,基本都有教程

相關文章
相關標籤/搜索