在整合三大框架SSM , 即 Spring 和 SpingMVC和Mybatis的时候,搭建项目最初需要先配置好配置文件.

有人在刚开始学习框架的时候会纠结项目搭建的顺序,因为频繁的报错提示是会很影响强迫症和编程心情的,这里分享我在构建项目时候的心得和配置文件的编写

首先你需要知道你的项目需要哪些结构,我们这里举一个基于Java的简单Web项目,实现最简单的功能,我需要Mybatis来进行持久层的控制,那么Mybatis就需要配置文件,我需要Dao层提供数据,那就需要Dao层的配置文件,我还需要服务层,那就需要Service的配置文件,再其次,我在进行一些选项的时候需要开启事务操作,那就需要事务的配置文件,最后是整合的SpringMVC的配置文件.

那么综上而言 我们把文件列表可以罗列出来:

Mybatis的配置文件: sqlMapConfig.xml

Spring的配置文件:  applicationContext-dao.xml

          applicationContext-service.xml

          applicationContext-tx.xml

          springmvc.xml

外部的Properties配置文件: jabc.properties 和 log4j.properties

学会配置xml文件是学习spring的基础,需要长时间的练习来达到熟练的效果.

我们先编写jabc.properties的配置文件:

这里使用的是阿里云的连接池Druid:

druid.url=jdbc:mysql://localhost:3306/egoubuy?characterEncoding=utf-8
druid.password=root
druid.username=root
druid.driverClassName=com.mysql.jdbc.Driver
druid.initialSize=5
druid.minIdle=3
druid.maxActive=20

再编写log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

现在,我们开始编写SSM的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>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin>
</plugins>
</configuration>

然后配置Spring下的Dao配置文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--加载外部属性配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${druid.url}"></property>
<property name="username" value="${druid.username}"></property>
<property name="password" value="${druid.password}"></property>
<property name="driverClassName" value="${druid.driverClassName}"></property>
<property name="initialSize" value="${druid.initialSize}"></property>
<property name="minIdle" value="${druid.minIdle}"></property>
<property name="maxActive" value="${druid.maxActive}"></property>
</bean> <!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置mybatis配置文件,这里的名称一定要对应-->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
<!--配置数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--批量配置mapper接口:
通过mapper扫描器,扫描某个包下的所有mapper接口,批量加载配置
--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--设置需要扫描的包-->
<property name="basePackage" value="这里是你需要的被扫描的包"></property>
<!--配置sqlSessionFactory对象-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> </beans>

再编写service的配置文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service包,将service包下添加@Servcie注解的类添加到容器中-->
<context:component-scan base-package="这里是你编写的service的包"></context:component-scan>
</beans>

由于需要开启事务,再编写事务的配置文件

<?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: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/aop https://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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置通知-->
<tx:advice id="tx_advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="add*"></tx:method>
<tx:method name="update*"></tx:method>
<tx:method name="modify*"></tx:method>
<tx:method name="delete*"></tx:method>
<tx:method name="remove*"></tx:method>
<tx:method name="get*" read-only="true"></tx:method>
<tx:method name="select*" read-only="true"></tx:method>
<tx:method name="query*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice> <!--配置切面-->
<aop:config>
<aop:advisor advice-ref="tx_advice" pointcut="execution(* 这里一般写service的类所在包.*.*(..))"></aop:advisor>
</aop:config> </beans>

最后是编写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: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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置扫描controller包下的所有以@Controller注解修饰的类-->
<context:component-scan base-package="这里是控制器的所在类,你可以放一起写,也可以分开写"></context:component-scan> <!--配置注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven> <!--放行静态资源文件-->
<mvc:default-servlet-handler></mvc:default-servlet-handler> <!--视图解析器-->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--配置文件上传解析器 吐过需要,你就打开注释commons-io commons-fileupload-->
<!--<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"></property>
</bean>--> </beans>

Java代码部分不做展示没有意义,当你在写项目的时候也要注意web.xml的配置,前提是web的项目

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!--配置post提交中文乱码的过滤器-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping> <!--给监听器指定初始化参数,用于加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--classpath:spring/applicationContext-dao.xml,classpath:spring/applicationContext-tx.xml,classpath:spring/applicationContext-servcie.xml-->
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param> <!--配置监听器 加载spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

关于涉及到spring相关的项目的xml文件bean头文件导包的问题,在idea中一般是自动的,你可以在setting中配置自动加载,同时,每一个需要的包都是对应好的,所以不用担心,编程使我们快乐,欢迎入坑框架.

Spring整合SSM的配置文件详解的更多相关文章

  1. Spring整合jdbc-jdbc模板api详解

    1, package com.songyan.jdbc2; public class User { private int id; private String name; public int ge ...

  2. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  3. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  4. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  5. J2EE进阶(四)Spring配置文件详解

    J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...

  6. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  7. SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot2-config-file/ 本文出自方志朋的博客 ...

  8. [转载]Spring配置文件详解一:

    原文地址:与base-package="com.xx">Spring配置文件详解一:<context:annotation-config/>与<contex ...

  9. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

随机推荐

  1. shell脚本,awk合并一列的问题。

    文件 file2内容如下:0 qwert1 asdfghjk2 asdjkl2 zxcvbn3 dfghjkll4 222224 tyuiop4 bnm 让第一列相等的合并成一行,不要第一列,也就是变 ...

  2. sublime点击预览未起作用?教你如何设置支持浏览器预览

    我用的text3版,其他版本未试,但应该也有效. 安了个view in browser插件,然而点击预览未起作用. 搜解决方法,发现了另一个插件,sidebar enhancements,设置快捷键预 ...

  3. 【分块】[HNOI2010]弹飞绵羊&分块大法祭

    分块(似乎还有一种动态树(LCT)做法) 第一次学习分块,似乎有点小激动 这是黄学长的分块入门博客「分块」数列分块入门1 – 9 by hzwer 题目描述 某天,Lostmonkey发明了一种超级弹 ...

  4. pip install mysqlclient 报错:error: Microsoft Visual C++ 14.0 is required.

    解决办法: 1. 在网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/  下载对应的whl文件,如我的环境是python3.7.2  windows32位版本 ...

  5. Linux文件权限与文件夹权限实践

    文件权限在基础中有介绍,不在重复 一.文件夹权限: 示例: 解释说明: r --read 既ls w --write     既创建新的目录或者文件 x --execute 既cd 现在有4个用户分属 ...

  6. 启动myeclipse出现JVM terminated. Exit code=-1

    在启动myeclipse时出现如图: 解决方法 第一种: myeclipse.ini中内存设置过大导致 修改: 256m改成128m,512m 改为 256m. 第二种:在myeclipse.ini ...

  7. 【笔记】PIL 中的 Image 模块

    Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片.例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来: 1 >>> ...

  8. The North American Invitational Programming Contest 2018 D. Missing Gnomes

    A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...

  9. 出现Android.os.NetworkOnMainThreadException 错误

    两种方法解决: 1.如果用的gradle打包,在build.gradle中修改配置 修改SDKVersion 为低版本(7),不能版本降低过多,否则会出现很多不适配. 2.将网络访问放在一个新的线程中 ...

  10. pytion3--文档字符串:__doc__

    除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看.从语法上来说,这类注释是写成字符串,放在模块文档.函数以及类语句的顶端.就在任何可执行程序代码前(#注释在其前也没问 ...