1 maven版本的ssm

1.1 最简单的版本步骤:

(1) 创建maven web项目

(2) 在pom.xml中导入依赖的jar包

(3) 再写配置文件:

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>

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.domain
cn.itsource.crm.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>

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" /> <!-- 启动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>

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///crm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

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

(4) 搭建service层,mapper层,controller层

(5) 进行测试

1.2 抽取一些公共的内容的版本

(1) 抽取BaseMapper

  把公共的crud方法抽取到BaseMapper里面。

(2) 抽取IBaseService和BaseServiceImpl

  把公共的crud方法抽取到BaseService里面,通过BaseServiceImpl去实现BaseService里面的方法。

(3) 抽取BaseDomain

  抽取公共的id。

2 maven项目的分模块开发

2.1 为什么需要分模块开发

使用传统的ssm结构,在随着项目的进行,我们可能遇到下面一系列的问题:

(1) 大部分的domain或者一些service以及mapper在多个项目中是通用的

(2) pom.xml中的依赖越来越长

(3) build整个项目的时间越来越长,尽管你只是一直在web层工作,但是不得不build整个项目

(4)  某个模块,比如mapper,你只想让一些经验丰富的人来维护,但是现在每个开发者都能修改这个模

块,这导致关键模块的代码质量达不到你的要求

2.2 什么是分模块开发

一个大项目拆分为多个小项目(maven模块)组成,而且它们是有依赖关系的。

2.3 怎么去分模块开发

写项目写代码尽量满足以下内容:

(1) 开闭原则

  对扩展开放

  对修改关闭-->公共的不要乱修改

(2) 高内聚:比如一个方法(方法就应该完成一个方法该干的事情) -- 最多40行

  低耦合:尽量的分层开发  mapper  service  controller(方便维护)

拆分:

  按照层次结构拆分

  按照业务功能拆分:电商、订单、物流……

(1) 代码拆分

  basic-util 工具类

  basic-core 公共内容

  crm-common 具体项目公共内容

  crm-mapper 项目里面具体mapper

  crm-service 项目里面service类

  crm-web 项目的controller层

  ……

  pss-common

(2) 配置文件拆分

web.xml  /  applicationContext-mvc.xml --> crm-web模块

applicationContext.xml --> crm-service模块

(3) 效果

3 RESTful风格

  RESTful是一种开发理念是设计风格而不是标准。 REST描述的是在网络中client和server的一种交互形式;REST本身不实用,实用的是如何设计 RESTful API(REST风格的网络接口),一种万维网软件架构风格。

前端代码 --> 后端代码进行交互,交互的时候前端代码(axios)发送请求到后端代码

axios --> get / post / put / delete / patch

3.1 RESTful风格理解

<a href="/xxx"> --> get

<form method="post"> --> post

ajax --> get/post

除了get/post以外,还支持其他请求 put/delete/patch  --> http协议扩展出来的

没有RESTful以前:

  /addUser?name='Bob'&age=38  -- get

  /delete?id=1

=====================================华丽的分割线=============================================

RESTful风格:传输请求风格写法  --  http get/post/patch/put/delete  完成增删改查:

(1) put动作 + /user(资源路径) {"name":"Bob","age":28} --> 新增

(2) post动作 + /user(资源路径) {"name":"Bob","age":28} --> 修改

(3) delete 动作 +/user/1 --> 表示删除id为1的用户

(4) get 动作 + /user/10 --> 查询id为10的用户

(5) patch 动作 + /user --> 批量查询

那么为什么要使用RESTful?

安全性好一点,现在比较流行的风格,不会暴露动作

3.2 RESTful代码实现

以下为测试代码:

新增:

/**
* 新增数据
*/
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public AjaxResult save(@RequestBody Department department) {
System.out.println("新增数据为:" + department);
return new AjaxResult();
}

修改:

/**
* 修改数据
*/
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public AjaxResult update(@RequestBody Department department) {
System.out.println("修改数据为:" + department);
return new AjaxResult();
}

删除:

/**
* 删除数据
*/
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
@ResponseBody
public AjaxResult delete(@PathVariable Long id) {
System.out.println("删除一条数据id为:" + id);
return new AjaxResult();
}

查询一条:

/**
* 查询一条数据
*/
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public AjaxResult findOne(@PathVariable Long id) {
System.out.println("查询一条数据id为:" + id);
return new AjaxResult();
}

查询所有:

/**
* 查询所有
*/
@RequestMapping(value = "/list",method = RequestMethod.PATCH)
@ResponseBody
public List<Department> findAll() {
return departmentService.findAll();
}

4 swagger(前端人员使用很多)

实现

  <springfox.version>2.4.0</springfox.version>

    <dependencies>
<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>
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",
"123",
"http://www.itsource.cn");
return info;
}
}

和spring整合,根据controller 生成的接口的文档,通过页面访问

运行:http://localhost/swagger-ui.html

5 postman

postman就是一个工具,可以来发送各种http请求,可以用它来测试http协议接口。

postman就是http协议接口测试工具。

测试 put/get/post/delete/patch这些请求:

6 前端vue-element-admin

基于 vue-cli 和 elementui 搭建出来一个模块框架,框架基本功能,路由,插件,国际化等

搭建模板:

(1) 解压文件 --修改名称

(2) 使用idea 打开

(3) 执行命令 npm install 安装依赖

(4) 启动 npm run dev

(5) 访问

ssm集成(maven)& 分模块开发--详细教程的更多相关文章

  1. spring+springmvc+hibernate架构、maven分模块开发样例小项目案例

    maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准 ...

  2. java使用maven项目(二)分模块开发

    1       整合ssh框架 1.1     依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 1.2     依赖版本冲突的解决 1.  第 ...

  3. Maven02——回顾、整合ssh框架、分模块开发、私服

    1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...

  4. SSH框架分模块开发

    ------------------siwuxie095 SSH 框架分模块开发 1.在 Spring 核心配置文件中配置多个内容,容易造成 配置混乱,不利于维护 「分模块开发主要针对 Spring ...

  5. Spring_day04--HibernateTemplate介绍_整合其他方式_Spring分模块开发

    HibernateTemplate介绍 1 HibernateTemplate对hibernate框架进行封装, 直接调用HibernateTemplate里面的方法实现功能 2 HibernateT ...

  6. Struts2_day01--Struts2的核心配置文件_常量配置_分模块开发_Action编写方式

    Struts2的核心配置文件 1 名称和位置固定的 2 在配置文件中主要三个标签 package.action.result,标签里面的属性 标签package 1 类似于代码包,区别不同的actio ...

  7. Struts2分模块开发

    -------------------siwuxie095 Struts2 分模块开发 在实际开发中,如果一个项目是团队开发的,也就是很多人开发的, 每个人都需要去修改 struts.xml,因为 s ...

  8. 七 Spring的分模块开发的配置,保存客户案例

    Spring的分模块开发的配置 加载配置文件的时候,加载多个 在一个配置文件中引入多个配置文件(常用) 保存客户案例 applicationContext.xml: <?xml version= ...

  9. Maven高级:01.maven分模块构建&&02.私服的应用

    IntelliJ IDEA 2018.3.6 x64 07 Maven高级:01.maven分模块构建(上) 07 Maven高级:01.maven分模块构建(中) 07 Maven高级:01.mav ...

随机推荐

  1. Deep attention tracking via Reciprocative Learning

    文章:Deep attention tracking via Reciprocative Learning 出自NIPS2018 文章链接:https://arxiv.org/pdf/1810.038 ...

  2. 【2018寒假集训Day 8】【并查集】并查集模板

    Luogu并查集模板题 #include<cstdio> using namespace std; int z,x,y,n,m,father[10001]; int getfather(i ...

  3. Redis系列(三):Redis集群的水平扩展与伸缩

    一.Redis集群的水平扩展 Redis3.0版本以后,有了集群的功能,提供了比之前版本的哨兵模式更高的性能与可用性,但是集群的水平扩展却比较麻烦,接下来介绍下Redis高可用集群如何做水平扩展,在原 ...

  4. cesium定义线面

    面: var polygon = viewer.entities.add({ polygon : { hierarchy : { positions : null, holes : [{ positi ...

  5. Linux发展历史(简略)

    LINUX UNIX历史发展 1969肯 汤姆森在DEC PDP-7机器上开发出了UNIX系统 1971肯 汤姆森的同事丹尼斯 里奇发明了C语言 1973UNIX系统绝大部分用C语言重写,为提高UNI ...

  6. mac安装jupyter

    SaintKings-Mac-mini:.pip saintking$ pip install jupyter --user Collecting jupyter Downloading jupyte ...

  7. 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】

    [写在前面] 有时候会与同事共享文件,正常人的操作是鼠标右键,点击共享.其实有个装逼的方法,用python的一行代码快速实现基于http服务的共享方式. [效果如下] [示例代码] 在cmd窗口进入想 ...

  8. Unity3D for iOS初级教程:Part 1/3(下)

    转自:http://www.cnblogs.com/alongu3d/archive/2013/06/01/3111735.html 一个手指来统治他们 但是等等,你还没有完全完成! 如果你玩游戏有一 ...

  9. iOS libsqlite3.0.tbd和libsqlite3.tbd的区别

    ibsqlite3.0.tbd 只是一个快捷方式,其实也是指向libsqlite3.tbd的,如果libsqlite3.0.tbd指向的就是最新的libsqlite3.tbd,就不用更新了.

  10. BOM对象学习

    location,history,screen <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...