介绍
Springboot 使用
# springboot war包在tomcat下部署
注意 此配置情况是在tomcat下启动springboot项目,tomcat启动成功,但访问地址时出现404问题
步骤一 设置packaging为war
<packaging>war</packaging>
1
步骤二 设置 spring-boot-starter-tomcat的scope属性为provided
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
1
2
3
2
3
步骤三 新建SpringBootStartApplication类
package com.cbdtelecom.web;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# springboot 打包成jar、war包
打包war包需要注意要在pom.xml中设置注释的属性、jar包不需要设置
<groupId>com.cbdtelecom</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <packaging>war</packaging>-->
<name>web</name>
<description>Demo project for Spring Boot</description>
1
2
3
4
5
6
7
2
3
4
5
6
7
mvn clean install
1
2
2
执行maven命令后,会在项目target文件夹下生成jar,war包
# springboot 项目tomcat上启动注意事项
*** 在本地运行项目正常启动,但是部署到tomcat上出现服务启动,但访问失败,后查看原因,要增加SpringBootStartApplication类 ***
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//XXXApplication 换成自己的启动类
return application.sources(XXXApplication.class);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# springboot jpa entity配置
- 如果实体有很多通用实体,需要靠继承来实现,那么需要在类上增加注解
@MappedSuperclass
public class BaseEntity implements Serializable {
}
1
2
3
2
3
@Entity
@Table(name = "t_app_authorize")
public class AuthorizeEntity extends BaseEntity {
}
1
2
3
4
2
3
4
# springboot jpa 配置
- And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd);
- Or --- 等价于 SQL 中的 or 关键字,比如 findByUsernameOrAddress(String user, String addr);
- Between --- 等价于 SQL 中的 between 关键字,比如 findBySalaryBetween(int max, int min);
- LessThan --- 等价于 SQL 中的 "<",比如 findBySalaryLessThan(int max);
- GreaterThan --- 等价于 SQL 中的">",比如 findBySalaryGreaterThan(int min);
- IsNull --- 等价于 SQL 中的 "is null",比如 findByUsernameIsNull();
- IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByUsernameIsNotNull();
- NotNull --- 与 IsNotNull 等价;
- Like --- 等价于 SQL 中的 "like",比如 findByUsernameLike(String user);
- NotLike --- 等价于 SQL 中的 "not like",比如 findByUsernameNotLike(String user);
- OrderBy --- 等价于 SQL 中的 "order by",比如 findByUsernameOrderBySalaryAsc(String user);
- Not --- 等价于 SQL 中的 "! =",比如 findByUsernameNot(String user);
- In --- 等价于 SQL 中的 "in",比如 findByUsernameIn(Collection
userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数; - NotIn --- 等价于 SQL 中的 "not in",比如 findByUsernameNotIn(Collection
userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;
# 注解
# @controller 控制器(注入服务)
用于标注控制层,相当于struts中的action层
# @service 服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理
# @repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件.
# @component (把普通pojo实例化到spring容器中,相当于配置文件中的 )
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。