文章目录
- SSM
- 导坐标
- 相关类
- config文件夹下
- result文件夹下
- 其他文件夹下
- 自定义异常类
- 拦截器相关
- 分模块开发
- 可选依赖(不透明)
- 排除依赖(不需要)
- 聚合
- 继承
- mvn命令
SSM
导坐标
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
相关类
config文件夹下
ServletConfig类
package com.demo.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
SpringMvcConfig类
package com.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan("com.demo.controller")
public class SpringMvcConfig {
}
SpringConfig类
package com.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan("com.demo.service")
@EnableTransactionManagement
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
JdbcConfig类
package com.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class JdbcConfig {
private String driver = "com.mysql.cj.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/mydatabase";
private String username="root";
private String password="root";
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
MybatisConfig类
package com.demo.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.demo.entry");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.demo.dao");
return mapperScannerConfigurer;
}
}
SpringMvcSupport类
package com.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pagpes/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectIntercepteor).addPathPatterns("/users","/users/*");//可添加其他路径用“,”隔开
}
}
result文件夹下
Code类
package com.demo.result;
public class Code {
public static final Integer SUCCESS=200;
public static final Integer ERROR=999;
//其他自定义Code值
}
Result类
package com.demo.result;
public class Result {
private Integer code;
private Object data;
private String msg;
//其他自定义属性值
public Result() {}
public Result(Integer code, Object data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public Result(Integer code, Object data) {
this.code = code;
this.data = data;
}
public Result(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
其他文件夹下
自定义异常类
BusinessException类
package com.demo.exception;
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code,String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public BusinessException(Integer code,Throwable cause) {
super(cause);
this.code = code;
}
}
SystemException类
package com.demo.exception;
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code,String message) {
super(message);
this.code = code;
}
public SystemException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public SystemException(Integer code,Throwable cause) {
super(cause);
this.code = code;
}
}
异常处理类
package com.demo.controller;
import com.demo.exception.BusinessException;
import com.demo.exception.SystemException;
import com.demo.result.Code;
import com.demo.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
System.out.println("1");
return new Result(Code.SYSTEM_UNKONW_ERR,null,"其他未定义异常");
}
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
System.out.println("2");
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
System.out.println("3");
return new Result(ex.getCode(),null,ex.getMessage());
}
}
拦截器相关
package com.demo.controller.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ProjectIntercepteor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
//false终止原始操作。
//true执行原始操作。
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
}
1、多拦截器时,执行顺序根据书写顺序执行。
2、三个拦截器的执行顺序为ABC
pre | post | after | |
---|---|---|---|
返回结果为都为true时 | A,B,C执行 | A,B,C执行 | A,B,C执行 |
C返回为false时 | A,B,C执行 | 都不执行 | A,B执行 |
B返回为false时 | A,B执行 | 都不执行 | A执行 |
A返回为false时 | A执行 | 都不执行 | 都不执行 |
分模块开发
可选依赖(不透明)
在dependency中加入<optional>true</optional>修饰
例:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
<optional>true</optional>
</dependency>
排除依赖(不需要)
在<dependency></dependency>中加入以下内容,设置要排除的依赖
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId></artifactId>
</exclusion>
</exclusions>
例:
<dependency>
<groupId>com.demo</groupId>
<artifactId>pro_entry</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
聚合
1.打包方式
<packaging>pom</packaging>
2.引入模块
<modules>
<module>../pro_entry</module>
<module></module>
<!-- 其他模块 -->
</modules>
继承
1.在子工程中添加父工程的pom信息继承父工程中的jar包
<parent>
<version></version>
<artifactId></artifactId>
<groupId></groupId>
<relativePath></relativePath><!-- 父工程路径-->
</parent>
2.可选依赖父工程中配置的
<dependencyManagement>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</dependency>
</dependencies>
</dependencyManagement>
配置多环境开发
<profiles>
<profile>
<id>env_dep</id>
<!-- 设定是否为默认启动环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
设置跳过测试
<build>
<!-- 设置跳过测试的内容 -->
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<!-- true:跳过测试;false:不跳过测试,可用excludes设置不进测试的内容 -->
<skipTests>false</skipTests>
<!-- 排除掉不参与测试的内容 -->
<excludes>
<exclude>**/UserTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
mvn命令
跳过测试:
mvn 指令 -D skipTests
例:mvn install -D skipTests
多环境开发执行:
mvn 指令 -P 定义的id
例:mvn install -P env_pro
设置资源目录,并能够解析${}
<build>
<!-- 设置资源目录,并设置能够解析${}-->
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>