2015-07-30 创建
最近项目使用了spring boot ,因为之前一直没有过,心里也比较抗拒【之前的架构用的好好地,干嘛就换了呢,而且还是让我自己去琢磨这个东西】。
一路磕磕绊绊还好到现在也算对这个东西比较了解了。在此也想把在此过程中自己觉得比较有用的东西记录下来。
0、项目文件结构
如下图:
其中配置文件 mybatisGenerator.xml 是用来反转生成实体类的
1、首先你要搭建一个spring boot 的工程
这是必须的,不然你练最基本的流程都不清楚那还搞什么。
对此我也不多说了,有一篇博客写的很清楚,我也不重复了没意思。
参见地址:http://www.cnblogs.com/rollenholt/p/3693055.html
2、再项目中使用的pom.xml 信息
maven会根据pom.xml中的信息导入相应的jar包
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.XXXX.xxxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- apache commons -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Spring 文件上传的包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Spring 文件上传的依赖包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- SFTP上传 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>bsh</groupId>
<artifactId>bsh</artifactId>
<version>1.3.0</version>
</dependency>
<!-- poi office -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-FINAL</version>
</dependency>
<!-- 上传Zip中文乱码问题,使用Java自带的解压Zip,如果有中文那么会出错 -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<!-- 打包时排除 配置文件 application.properties
-->
<exclude>**/application.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties 信息
#test environment
spring.datasource.url=jdbc:mysql://IP:3306/wpms_test?characterEncoding=utf-8
spring.datasource.username=test
spring.datasource.password=test123
spring.datasource.max-active=4
spring.datasource.initial-size=1
spring.datasource.max-wait=30000
spring.datasource.max-idle=20
spring.datasource.min-idle=4
spring.datasource.time-between-eviction-runs-millis=3000
spring.datasource.min-evictable-idle-time-millis=300000
#if you don't mind a hit for every getConnection(), set to "true"
spring.datasource.validation-query=SELECT 1
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
#将实体类对应的表名全部改为大写【数据库中表名为大写----Linux中严格区分大小写】
spring.jpa.hibernate.naming_strategy=com.ailk.woshop.common.utils.MySQLUpperCaseStrategy
#打印SQL语句
spring.jpa.show-sql=true
#logging.level.*=DEBUG
server.port = 8082
spring.freemarker.charset=UTF-8
3、类 Application 信息
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import com.ailk.woshop.common.utils.security.UserSessionAuthFilter;
/**
* 作者: XXX
* 说明: TODO 描述功能... <br/>
* 版本: 1.0 <br/>
*/
@SpringBootApplication
public class Application implements CommandLineRunner {
@Bean // 过滤器
FilterRegistrationBean secureFilter(){
return new FilterRegistrationBean(new UserSessionAuthFilter());
}
@Bean // 文件上传大小限制【如果文件大小超过了规定大小,那么后台不会走到controller层,也不会返回任何信息,所以前台也没有任何反应】
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("600MB");
factory.setMaxRequestSize("600MB");
return factory.createMultipartConfig();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public void run(String... args) throws Exception {
System.out.println("这是商户端后台系统!");
}
}
4、上传方法
在spring boot 中文件上传和通常的上传有些不一样。就是和spring mvc 上传也有区别。所以在这里特别提出来。
参见博文:http://www.cnblogs.com/rollenholt/p/3693087.html
或者
/**
* 商品Excel上传
* @param request
* @param response
* @param file
* @return
* @throws Exception
*/
@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
public Map<String, Object> uploadGoodsExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") MultipartFile file) {
LoginInfo loginInfo = (LoginInfo) request.getSession().getAttribute("loginInfo");
Map<String, Object> msgMap = new HashMap<String, Object>();
if(!"".equals(loginInfo.getPartnerId()) && file != null){
String partnerId = loginInfo.getPartnerId();
msgMap = this.goodsSrc.uploadGoodsExcel(file, partnerId);
}else{
msgMap.put("state", false);
msgMap.put("msg", "商品信息导入失败,请重新操作!");
}
msgMap.put("type", "excel");
return msgMap;
}
5、项目打包
因为项目是前后台完全分离的,所以后台直接打为jar包。
6、spring boot 打包后运行
因为在pom.xml 中信息的原因,所以打包时没有配置文件 application.properties 。如果要运行该项目,那么将jar包和配置文件放到一个文件夹里面即可。
Linux运行,后台运行 nohup java -jar test-RELEASE-01.jar & 停止运行可以查看该项目所占的进程号,并使用 kill -9 进程号 就可以停止项目了
其他:把数据库名全部转换为大写
import org.hibernate.cfg.ImprovedNamingStrategy;
/**效果:把数据库名全部转换为大写
*
* jpa在正常情况下向Linux发送的数据库表名全部转换为小写,如下:
* com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'wpms_test.pm_oto_goods_partner' doesn't exist
* 解决方案如下
*
* @author Administrator
*
*/
public class MySQLUpperCaseStrategy extends ImprovedNamingStrategy {
private static final long serialVersionUID = -7351332284160156572L;
@Override
public String tableName(String tableName) {
return tableName.toUpperCase();
}
}