标签:数据库名 response 目的 文件上传 stdout file std string map
因为:
1.随着业务增加,mapper或者service类越来越多,项目就会越来越臃肿---需要拆分来解决
2.项目代码越来越多,结构或者编译越来越慢---需要拆分来解决
3.有些内容是公共的内容,有很多项目都可以使用---需要公共的模块来解决
4.有些内容,不想让每个人都可以去改---解决方法抽取
一般是中大型项目都在使用分模块开发
以前ssm结构:
但随着项目的壮大:
(1) 大部分的domain或者一些service以及mapper在多个项目中是通用的
(2) Pom.xml中的依赖越来越长
(3) Build整个项目的时间越来越长,尽管你只是一直在web层工作,但是不得不build整个项目
(4) 某个模块,比如mapper,你只想让一些经验丰富的人来维护,但是现在每个开发者都能修改这个模块,这导致关键模块的代码质量达不到你的要求
一个大项目拆分为多个小项目(maven模块)组成,而且它们是有依赖关系的.
为了让项目尽量满足:
设计层面
高内聚(写一个方法 就应该完成该方法的事情, save 10 20 ),
低耦合(类层面和接口层面 分层 使用接口 -- 面向接口编程 -- 依赖减少)
怎么拆分:
把以前的一个模块(一个项目)拆分成多个模块拆分后的结构(下面一一分析) 在创建下面模块时只有crm_web需要选择项目原型maven-archetype-webapp
它没有一个java代码,只是用来对子模块分类 ,它就是装其他模块的盒子
crm-parent里面在pom.xml配置一个公共jar给儿子继承
什么mian,test文件都可以不要,因为不写东西进去
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- spring版本号 --> <spring.version>4.1.2.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.1</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.2</slf4j.version> <log4j.version>1.2.17</log4j.version> <!-- jackson包版本 --> <jackson.version>2.5.0</jackson.version> <!--swagger版本--> <springfox.version>2.4.0</springfox.version> </properties> <dependencies> <!--Spring核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--aop依赖包--> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> </dependencies>
作为各项目的公共工具的抽取
PageList类(分页对象):字段(total,rows)
AjaxResult类(请求状态响应对象):字段(success,message)
basic-util模块的结构
作为各项目的公共的抽取,依赖basic-utils模块
它是用来储存domain,mapper,query,service公用的代码
domain:所有的domain都会有id,所以提取出来放在domain的BaseDomain.java中
mapper:所有的mapper都会有CRUD,所以提取出来放在mapper的BaseMapper.java中
query:所有的请求也许都会有相同的请求对象,所以提取出来放在query的BaseQuery.java中
service:所有的service都会去调用mapper中的CRUD方法,所以提取出来放在query的BaseService.java中
pom.xml中添加依赖模块和所需的依赖包
<dependencies> <!--依赖basic_util模块--> <dependency> <groupId>crm_parent</groupId> <artifactId>basic_util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--目的是可以在base中使用@autowired @Transactional注解--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
crm-common本模块作为一个项目三层的公共部分的抽取,依赖basic_core模块
在该模块下面创建domian对象(不写id就去继承BaseDomain,因为BaseDomain提取了个id)和某张表的查询对象
crm_common模块结构
pom.xml中添加依赖模块
<dependencies> <dependency> <groupId>crm_parent</groupId> <artifactId>basic_core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
Department.java
import cn.itsource.basic.core.domain.BaseDomain; /** * 部门(crm_common是放普通的domain) */ public class Department extends BaseDomain{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department{" + "name=‘" + name + ‘\‘‘ + ", id=" + id + ‘}‘; } }
DepartmentQuery.java
/** * 员工的查询条件(普通类的查询条件) */ public class DepartmentQuery extends Query { }
crm_mapper本模块作为一个项目持久层,依赖crm_common模块包含mapper接口和映射;和持久层的配置
pom.xml中添加依赖模块和所需的依赖包
<dependencies> <dependency> <groupId>crm_parent</groupId> <artifactId>crm_common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--jdbc连接池--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</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>1.2.0</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <scope>test</scope> </dependency> <!--数据库连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--数据库连接池--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.3</version> </dependency> </dependencies>
DepartmentMapper.java
import cn.itsource.basic.core.mapper.BaseMapper; /** * 部门类的Mapper(普通类的接口) * 继承 * BaseMapper(CRUD的方法) */ public interface DepartmentMapper extends BaseMapper{ }
DepartmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mapper 映射 namespace:对那个接口进行实现 select:查询 id:必须和方法名一致 resultType:返回的数据类型 SELECT * FROM t_department:sql语句 --> <mapper namespace="cn.itsource.crm.mapper.DepartmentMapper"> <select id="queryAll" resultType="Department"> SELECT * FROM t_department </select> </mapper>
crm_service本模块作为一个项目业务层,依赖crm_mapper模块
crm_service模块的结构
pom.xml中添加依赖模块
<dependencies> <dependency> <groupId>crm_parent</groupId> <artifactId>crm_mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
IDepartmentService.java
import cn.itsource.basic.core.service.IBaseService; import cn.itsource.crm.common.domain.Department; public interface IDepartmentService extends IBaseService<Department> { }
db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///数据库名?useUnicode=true&characterEncoding=utf-8 jdbc.username=数据库账号 jdbc.password=数据库密码
log4j.properties
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
log4j.logger.cn.itsource=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
applicationContext.xml
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描的包--> <context:component-scan base-package="cn.itsource.crm.service"/> <!-- Jdbc配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据源dataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!--maxActive: 最大连接数量 --> <property name="maxActive" value="150" /> <!--minIdle: 最小空闲连接 --> <property name="minIdle" value="5" /> <!--maxIdle: 最大空闲连接 --> <property name="maxIdle" value="20" /> <!--initialSize: 初始化连接 --> <property name="initialSize" value="30" /> <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 --> <property name="maxWait" value="1000" /> <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. --> <property name="timeBetweenEvictionRunsMillis" value="10000" /> <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 --> <property name="numTestsPerEvictionRun" value="10" /> <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 --> <property name="minEvictableIdleTimeMillis" value="10000" /> <property name="validationQuery" value="SELECT NOW() FROM DUAL" /> </bean> <!--Mybatis核心对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 配置mybatis (mapper)映射器路径 --> <property name="mapperLocations" value="classpath*:cn/itsource/crm/mapper/*Mapper.xml" /> <!-- 配置mybatis 类型别名 --> <property name="typeAliasesPackage"> <value> cn.itsource.crm.common.domain cn.itsource.crm.common.query </value> </property> </bean> <!--注入映射器,一劳永逸的做法--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itsource.crm.mapper"></property> </bean> <!--事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--以注解的方式进行事务管理--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
1.7,crm-web
在创建这个模块时和前面的不同点是需要选择显目原型maven-archetype-webapp
crm_web本模块作为一个项目表现层,依赖crm_service模块
Controller
而现在我们的整体架构为前后端分离架构,所有的静态资源全部在前端项目中
crm_web模块结构
pom.xml中添加依赖模块和依赖包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>crm_parent</groupId> <artifactId>crm_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--SpringMVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--swagger配置--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> </dependencies> <build> <finalName>crm_web</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build>
SwaggerConfig.java
package cn.itsource.crm.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration //相当于写一个配置文件 application.xml @EnableWebMvc // 开启springmvc @EnableSwagger2 //开启swagger2 @ComponentScan(basePackages="cn.itsource.crm.web.controller") public class SwaggerConfig { //相当于 <bean ><property name=""> </bean> @Bean public Docket api(){ //生成接口信息 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(this.apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.itsource.crm.web.controller")) .paths(PathSelectors.any()) .build(); } //api的 注解 javaweb 文档的描述信息 private ApiInfo apiInfo(){ @SuppressWarnings("deprecation") ApiInfo info=new ApiInfo( "API接口测试文档", "接口测试", "1.0", "http://www.itsource.cn", "itsource", "111", "http://www.itsource.cn"); return info; } }
DepartmentController.java
import cn.itsource.crm.common.domain.Department; import cn.itsource.crm.service.IDepartmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; @Controller @RequestMapping("/department") public class DepartmentController { @Autowired private IDepartmentService departmentService; @RequestMapping("/list") @ResponseBody public List<Department> list(){ return departmentService.queryAll(); } }
applicationContext-mvc.xml
<?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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="cn.itsource.crm.web.controller" /> <context:component-scan base-package="cn.itsource.crm.web.config" /> <!-- 启动SpringMVC的注解功能 --> <mvc:annotation-driven/> <!--静态资源放行--> <mvc:default-servlet-handler/> <!-- 定义跳转的文件的前后缀 ,视图解析器配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> </bean> </beans>
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <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" version="3.0"> <display-name>crm</display-name> <!-- Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--Spring监听器 ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC 核心配置开始 --> <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:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
然后设置数据库和表,tomcat启动项目。
标签:数据库名 response 目的 文件上传 stdout file std string map
原文地址:https://www.cnblogs.com/bigbigxiao/p/11992684.html