ssm+maven+eclipse框架搭建

ssm框架搭建

 

因为之前没有独立搭建过ssm框架,做的项目也都是从视频上看着照做,没有自己的想法,也不符合自己的意愿,同时对ssm整合的了解并没有起到太大的作用,所以自己尝试搭建了一个ssm的框架,现在记录一下个个配置,以便以后搭建框架出先问题可以回来查阅。

 

 

一:首先创建maven工程(因为以前在学校搭建ssh框架自己找jar包,各种依赖不对应,各种jar包找不到,所以现在很喜欢maven这个依赖管理工具)。

这中间需要注意的是下面这个图要选择webapp这个选项(之前看教程说是这个webapp的版本也有讲究,但是我这次配置没有改也成功了,所以就先不管了,以后遇到这个问题在说吧)

 

 

二:名字什么的都起好之后,点击finish,现在的项目结构是这样的

 

 

跟咱们平时的web项目相比少了一些东西(而且我这里的maven是修改过conf下的setting文件的,否者他的JRE默认是1.5的,不过我们都是需要修改的)。然后我们右键项目进入BulidPath中

 

 

这里很明显少了server(因为我是在修改了maven的jar包地址的同时写这篇博客,本来项目没有server环境的话index.jsp是应该报错的),我们点击上图中的add Library

 

 

把我画红线的两个都配置成本地的(记住是你本地的,不是myeclipse中的,因为myeclipse之类的IDE都会模仿你本地的tomcat所以在配置的时候要格外注意。具体要怎么正确使用本地的tomcat而不是被myeclipse克隆的tomcat欺骗了,我在以后的博客中会写)

三:上面两步完成后maven项目就算时建成了,下面开始整合ssm。这里只记录各个配置文件的写法,并不写这样整合的原理(因为在下不会,不懂,日后会了再说)

先上一下项目结构,这是我认为比较舒服的项目结构

 

 

其中的各个文件就不介绍作用了,自己都懂,不懂看下名字也都知道是什么意思

粘一下各个配置文件的内容

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框架搭建,基本都有教程

posted on 2019-04-23 10:33  沈大侠  阅读(513)  评论(0编辑  收藏  举报