系统架构师——搭建SpringMVC,Spring,Mybatis框架——从模块开发到系统架构搭建
做程序员已经有几年时光了,在期间参与过Android软件开发。Java软件开发,但就Java软件开发来说,由于项目模块众多而且复杂,从来没有自己搭建过项目框架,都是参与模块开发。每个人都一样,谁不向往成为项目中核心的一员或者成为系统架构师,项目经理等,我和众多的程序员一样,每天早早的上班,挤过公交,挤过地铁。在上海这座美丽的城市拼搏,最近终于成为了项目的主干,核心人物,我想记录下一路走来的点滴。
做程序员已经有几年时光了,在期间参与过Android软件开发。Java软件开发,但就Java软件开发来说,由于项目模块众多而且复杂,从来没有自己搭建过项目框架,都是参与模块开发。每个人都一样,谁不向往成为项目中核心的一员或者成为系统架构师,项目经理等,我和众多的程序员一样,每天早早的上班,挤过公交,挤过地铁。在上海这座美丽的城市拼搏,最近终于成为了项目的主干,核心人物,我想记录下一路走来的点滴。
曾经面试过上百家的企业,当问到你独立搭建过架构吗?我只能说没有,因此拼搏几年也没拿过高薪。你是否也这样呢?那就和我一起向架构师、项目经理、团队核心努力吧,一起去追逐我们的梦想和高薪。
思路:
1、分析需求,你要做什么,目标产品具有什么特点出发(决定了你需要什么样的jar包)
2、确定搭建的技术架构,即就是采用什么样的技术,使用什么样的层次结构使得项目更容易扩展,易维护,以及多项目的架构重用(以不变应万变)
3、确定开发工具和搭建项目的工具(统一代码管理和jar管理,团队环境一致不容易出现莫名的错误)
4、实施搭建
5、测试
技术架构:spring4.1.9+springMVC+Mybatis3.2.6+jsp+javascript
开发工具:1、idea2017(统一的代码开发工具)
2、maven3.5(用于jar管理和项目构建测试和打包)
3、svn(代码统一服务器管理)
4、Oracle数据库+pl/sql客户端(连接数据库建表)
5、PowerDesigner15.3(用于数据库设计和表结构管理)
开发环境:jdk1.8(支持Java),UTF-8
项目结构:
项目架构项目创建和pom.xml配置
项目目录结构:
项目架构师为了重复使用架构模块防止每次找必须jar包,该项目我们只需要配置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.banwxf.schoolspring</groupId>
<artifactId>school_spring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>school_spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.9.RELEASE</spring.version>
<mybatis.version>3.2.6</mybatis.version>
<mybatis_spring.version>1.2.0</mybatis_spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis_spring.version}</version>
</dependency>
<!--mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 整合数据库连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>11.2.0</version>
</dependency>
<!--文件相关-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>school_spring</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
详细标签和jar包引入说明:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>该标签配置了spring的jar包的版本和坐标。${spring.version}引用的是 < properties >标签中对应的版本,名字自定义,按住Ctrl能够链接到
<properties>中,鼠标移动上去会显示对应版本,否则配置< properties >
<spring.version>4.1.9.RELEASE</spring.version> <mybatis.version>3.2.6</mybatis.version> <mybatis_spring.version>1.2.0</mybatis_spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>该标签配置了spring,mybatis等Jar的版本号,内部标签自定义,同理,所有用到的jar版本都可以配置在 properties中
<!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis_spring.version}</version> </dependency> 这是mybatis的包,在该架构中必须有该包 <!--mysql连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency>这是MySQL链接包
<!-- 整合数据库连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>这是c3po数据库连接池,能控制数据库最大连接数等,需要在spring中进行配置,具体配置看后面
<!--数据库驱动--> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>11.2.0</version> </dependency>这是oracle的驱动包用于连接oracle数据库,配合c3p0使用,要连接oracle数据库必须有
<!--文件相关--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.0.1</version> </dependency>这三个是,spring上传下载文件必须的包,如果不需要上传下载可不需要次jar包,该包可不需要
<!--log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>该包是日志输出相关jar,需要专门配置,并非必须,只是辅助开发和系统日志处理。
其他相关jar包说明和引入
<!-- json转换 springmvc返回json格式必须引入--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency>这三个是用于json输出相关jar包,在spring4.1.9及以上必须使用,需要在spring的配置文件中配置
<!-- java servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>这个是servlet相关的jar包
<!-- office文档组件 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.4.0</version> </dependency>这是用于导入或导出成office文件相关的
<!-- 汉字拼音组件 --> <dependency> <groupId>net.sourceforge</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>这是汉子拼音相关的,能够获取汉子拼音等功能
<!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>这是spring当中使用httpservlet相关的,以及加载spring必须的,不能缺少,缺少web.xml的过滤器和分发配置就会报错,如下
<dependency> <groupId>com.banwxf.utils</groupId> <artifactId>school_utils</artifactId> <version>1.0-SNAPSHOT</version> </dependency>这是对本地工具项目的依赖,包括架构也要这样依赖。
这样所有的jar配置就完成了,后续就是加载spring和整合springmvc,mybatis使其成为一个整体。
二、整合springMVC+spring+mybatis+数据库需要编写如下文件:
先看jdbc配置:jdbc.properties,名字随便定义,在spring配置文件中能加载到即可
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/jkdb?characterEncoding=utf-8
#jdbc.username=root
#jdbc.password=root
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
jdbc.username=xysq
jdbc.password=xysq
c3p0.pool.maxPoolSize=20
c3p0.pool.minPoolSize=5
c3p0.pool.initialPoolSize=3
c3p0.pool.acquireIncrement=2
里面配置了c3p0和数据库驱动,数据库用户名和密码,连接地址,最大连接池等。
先看spring配置文件:文件名可以随便起
<?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-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
<!-- 1. 加载数据库配置的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2. 包扫描dao,service -->
<context:component-scan base-package="com.banwxf.schoolserver.dao,com.banwxf.schoolserver.service"/>
<!-- 3. 数据源dataSource C3P0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}"/>
<property name="minPoolSize" value="${c3p0.pool.minPoolSize}"/>
<property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}"/>
<property name="acquireIncrement" value="${c3p0.pool.acquireIncrement}"/>
</bean>
<!-- 4. SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 整合mybatis,包扫描 mapper文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<property name="mapperLocations" value="classpath:com/banwxf/schoolserver/**/*.xml"/>
</bean>
<!-- 事务控制 -->
<bean id="txManager-base"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice-base" transaction-manager="txManager-base">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="find*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
该文件主要配置了
1、加载jdbc文件,整合c3p0,注意spring中引用变量按住Ctrl能链接到,否则说明文件没找到,路径错误。
2、获取mybatis的对象
3、事务控制
4、service和dao的扫描路径
注意:其中文件头是必须的,而且后面的版本要和所引用jar一致,可以去jar底下找配置文件拷贝一个,也可以拷贝低版本的改后面的版本号
再看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>
该文件中不需要写任何东西,因为全部交给spring了
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-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
<!-- 组件扫描 -->
<context:component-scan base-package="com.banwxf.schoolserver.controller" />
<!--<!– 自定义注册属性编辑器 –>-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.banwxf.schoolserver.StringTrimConverter" />
<bean class="com.banwxf.schoolserver.DateConverter" />
<bean class="com.banwxf.schoolserver.DatetimeConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<!-- 异常处理 -->
<bean id="handlerExceptionResolver"
class="com.banwxf.schoolserver.exception.CustomExceptionResolver">
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
该文件中需要配置controller的扫描路径,自定义过滤器,自定义异常处理器,文件上传,和必须有的视图解析器。注意springmvc是通过id加载的所以id中的名字必须是这样的,不能随便定义。
这些类在上述jar包中,文件在文件上传下载相关jar包中。
最后一步,加载spring框架。我们需要在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- 1.spring框架 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2.springmvc servlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--屏蔽springmvc自动注册的异常处理器--> <init-param> <param-name>detectAllHandlerExceptionResolvers</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- 3.编码过滤器,解决中文乱码 --> <filter> <filter-name>SpringEncoding</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>SpringEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
该文件配置了spring的配置文件名字是你定义的spring文件名,spring监听,请求分发,异常处理屏蔽,防乱码,请求过滤,详细说明如下:
<!-- 1.spring框架 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>加载spring和监听,applicationContext.xml是你的spring配置文件,按Ctrl必须连接到,名字不能变。
<!-- 2.springmvc servlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--屏蔽springmvc自动注册的异常处理器--> <init-param> <param-name>detectAllHandlerExceptionResolvers</param-name> <param-value>false</param-value> </init-param> </servlet>springmvc的加载配置,springmvc.xml是你的springmvc的配置文件,同理可以链接,org.springframework.web.servlet.DispatcherServlet该类依赖servlet-api这个包需要引入,下面的springmvc的映射<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
中按Ctrl将链接到上面配置,配置了url的拦截方式,可以是.action或.do,/*表示拦截所有
<!-- 3.编码过滤器,解决中文乱码 --> <filter> <filter-name>SpringEncoding</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>SpringEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>同理,这是spring强制utf-8的过滤器。
这样整个springmvc+spring4.1.9+mybatis3.2.6就搭建成功了,启动tomcat进行测试或者采用Junit测试即可。
更多推荐
所有评论(0)