如果想要获取第一天的源码,笔记,和相关工具,增加需求,代码重构,可以关注我并私信!!!
一 主工程模块yycgproject的SSM整合
1 创建Base业务基础模块
Base业务基础模块中主要的就是系统管理的功能.
因为base基础模块依赖技术架构模块,所以整合的关键就是spring+springmvc+mybatis的整合
创建Base业务基础模块的步骤如下:
2 创建配置文件
在yycgproject主工程模块的src/main/resources目录下来配置资源文件的目录,按照不同的技术架构来分目录,不同的目录下存放不同的技术框架下的配置文件.
注意:作为一个系统架构师,这部分内容是必须掌握的,但是作为开发人员就了解即可!!
其实,配置文件中的内容都是固定的内容,需要时翻翻相应的文档即可:请先参考“mybatis.doc文档”,然后再参考“springmvc.doc文档”(注意顺序)
2.1 配置文件完整目录结构图
这里先给出一个创建base业务基础模块的完整的目录和目录下的创建好了的配置文件的正确的图示:
这里只先给出目前为止yycgproject工程下的src/main/resources目录下所有的公共的配置文件,其他的配置文件会随之项目的深入而逐步给出来。
而配置文件中的内容可以参考下面的“整合dao”一节。
另外,src/main/java下面的所有目录也可以依照base和business两大模块分别创建出来。目录下的内容可以参考下面的“整合dao”一节.
如果有些目录和目录下的java文件不清楚的话,随之项目的深入会逐步的解释清楚的!!!
2.2 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 |
2.3 db.properties
连接数据库,配置连接数据库的参数
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl jdbc.username=yycg jdbc.password=123 jdbc.maxActive=5 jdbc.maxIdle=2 |
2.4 SqlMapConfig.xml
mybatis的核心配置文件,这里不用任何的配置,因为在spring和mbatis的整合包中自带了了mapper扫描器,它会自动扫描包下的接口和配置文件。
但是,该文件必须存在,而且该文件将来在配置二级缓存时可以派上用场,所以还需要留住,不能删除!
<?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> |
2.5 applicationContext.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<!-- 加载db.properties配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据库连接池(数据源)的配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 开发阶段建议最大连接数据尽量少,够用即可 --> <property name="maxActive" value="${jdbc.maxActive}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> </bean> </beans> |
2.6 springmvc.xml
2.6 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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 1.配置组件扫描器:扫描所有标记了@Controller注解的action类,由于使用自动扫描所以action类不用在spring配置文件中配置 . 2.关于组件扫描器的作用:可以参考"springmvc_基础知识.doc"中的"注解驱动器"一节中的介绍. 因为主工程yycgprojict模块下包含两个子模块base模块和business模块, 所以yycg.**.action就表示:base模块和business模块下所有的action包下的类都会被扫描到(当然,组件扫描器可以正确使用的:前提是action包下的类都使用了@Controller注解进行了定义)。 --> <context:component-scan base-package="yycg.**.action" /> <!-- 处理器映射器和处理器适配器这两个可以使用mvc注解驱动来代替 --> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 将jstl的jar包加入工程,默认支持jstl --> <!-- 前缀和后缀可以去掉的,为了方便开发才加的 --> <property name="prefix" value="/WEB-INF/jsp" /> <property name="suffix" value=".jsp" /> </bean> </beans> |
2.7 applicationContext-base-service.xml
注意该配置文件的命名规范是:aplicationContext+模块名称+service的格式。
因为这里配置的是base模块的service,所以命名为applicationContext-base-service.xml。如果开发的是business模块,那么相应的文件名称是:applicationContext-business-service.xml.
该配置文件中主要配置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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> </beans> |
2.8 applicationContext-base-dao.xml
实际开发中,在dao中存在很多的模块,例如这里属于base业务模块的dao,那么配置文件的命名方式就是:applicationContext- base-dao.xml;
如果配置的是bunisess模块,那么相应的配置文件名称为:applicationContext- business-dao.xml。
该配置文件主要配置base模块的dao,文件中主要配置SqlSessionFactory,dao(mapper)。
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd |