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 使用mybatis和spring整合包中的 -->

<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.xml(springmvc配置文件)

<?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不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-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框架搭建,基本都有教程

转载于:https://www.cnblogs.com/shendaxia1/p/10754947.html

ssm+maven+eclipse框架搭建相关推荐

  1. SSM+Maven+Eclipse进行单元测试时提示找不到Config配置项/文件

    场景 在Eclipse+SSM+Maven中进行单元测试时,会提示Config配置文件/信息找不到 原因 在测试类中调用了正常业务中的方法/接口,而在方法中有用到常量的地方, 而常量又是在封装的常量类 ...

  2. 复习SSM day01 SSM Maven工程的搭建及配置文件

    SSM专题复习 day01 1 课程回顾 必做 新建项目或者是git上拉取的老项目检查Maven环境.JDK版本 .项目的编码UTF-8 搭建项目工程 理清项目的结构和每一个模块的功能打包方式 (co ...

  3. 搭建eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo

    前言:我这里搭建好eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo.新手快速上手直接看demo. 最后处提供完整高质量de ...

  4. Maven搭建多模块企业级项目+SSM框架搭建

    一.开发环境: 0.Eclipse Java EE IDE for Web Developers:  /Version: 2018-09 (4.9.0)  /  Build id: 20180917- ...

  5. Spring+Mybatis+SpringMVC+Maven+MySql(SSM框架)搭建实例

    这篇文章我们来实现使用maven构建工具来搭建Spring+Mybatis+SpringMVC+MySql的框架搭建实例.工程下载 使用maven当然得配置有关环境了,不会配置的请看我前几篇文章,都有 ...

  6. 小菜鸟的SSM框架搭建【详细步骤】【SSM/IDEA/MAVEN】

    小菜鸟的SSM框架搭建 内容很长噢,一步步搭建 此框架是跟着b站上的黎曼的猜想所发布的视频搭建起来的,细节操作可以看视频.我只是在这里梳理一下ssm框架搭建的流程. 整合说明:SSM整合可以使用多种方 ...

  7. SSM框架搭建(Maven项目)

    一.项目的整体目录结构 二.构建Maven项目 三.pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  8. 【SSM框架】超详细的使用eclipse工具搭建SSM框架,一看就会

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架 ...

  9. SSM框架搭建,及遇到的问题

    SSM框架搭建,及遇到的问题 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Exp ...

最新文章

  1. python人脸识别毕业设计-Python基于Dlib的人脸识别系统的实现
  2. Java 洛谷 P1914 小书童——密码
  3. 【Tools】gcc4.4升级到gcc4.8
  4. java for遍历hashmap_Java中HashMap遍历几种方式
  5. python大众点评最新字体加密破解完结
  6. Node.js学习笔记(二)
  7. 如何设置背景图片的大小?
  8. prototype鼠标指针_html5鼠标点击页面光标圆点动画特效
  9. Paste for Mac(剪切板历史管理工具)
  10. Yolov5进阶之一摄像头实时采集识别
  11. 信息学竞赛有什么好的比赛网站?
  12. 杨卫华:新浪微博的架构发展历程
  13. qt弹窗界面模态设置
  14. dede首页php调用会员积分,dedecms织梦添加会员积分的方法
  15. ZPL II 语言编程基础
  16. 物联网架构及五大通信协议
  17. mac编码查看及修改
  18. 【Python核心】不可或缺的自定义函数
  19. python fontsize_python – Matplotlib图例fontsize
  20. python抓取wallhaven首页壁纸

热门文章

  1. Object Detection API 相关
  2. 自动化安装DHCP配置脚本
  3. Linux开启文件共享服务
  4. 阿里云主机(aliyun-Linux) x64安装Redis详解
  5. mongodb 3.0版本安装
  6. Flask 富文本编辑器
  7. Unity手游之路三 基于Unity+Java的聊天室源码
  8. flutter Radio 单选框
  9. Java 多条件复杂排序小结
  10. Linux下执行.sh文件