欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot(一)

程序员文章站 2022-03-02 14:39:07
...

springboot maven文件继承 spring-boot-starter-parent。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
</parent>

springboot还集成以下组件。

 springboot是基于spring基础上进行集成开发的。像spring-boot-starter-web组件就包括spring-web、spring-webmvc、jackson等。

使用spring-boot-devtools模块的应用,当classpath中的文件有改变时,会自动重启,就是说,默认会监视classpath入口。

静态资源和视图模板不需要重启。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>

 

  Spring Boot的这种打包方式需要使用Spring Boot提供的 spring-boot-maven-plugin 。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

SpringBoot启动方法

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
public class RenrenApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(RenrenApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(RenrenApplication.class);
    }
}

SpringBoot部署方式

(1)jar方式

 <packaging>jar</packaging>

 通过java -jar <name>.jar命令运行

(2)tomcat部署

项目启动路径

Spring boot默认是/,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/访问的话,那么需要在Application.properties文件中加入server.context-path= /你的path,比如:spring-boot,那么访问地址就是http://ip:port/spring-boot路径。

# Tomcat
server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 1000
        min-spare-threads: 30
    port: 8080
    connection-timeout: 5000
    context-path: /springboot

 

SpringBoot 单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisDemo2ApplicationTests {


    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){

        userMapper.insert("winterchen", "123456", "12345678910");
        User u = userMapper.findUserByPhone("12345678910");
        Assert.assertEquals("winterchen", u.getName());
    }



}

 SpringBoot 构建restful API 

@Controller:修饰class,用来创建处理http请求的对象.

@RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。

但是与页面交互还是需要@Controller

@RequestMapping:配置url映射

 

SpringBoot配置文件application.properties与application.yml区别

application.properties

server.port=8080  
server.session-timeout=30  
server.context-path=  
server.tomcat.max-threads=0  
server.tomcat.uri-encoding=UTF-8  
  
spring.datasource.url = jdbc:mysql://localhost:3306/spring  
spring.datasource.username = root  
spring.datasource.password = root  
spring.datasource.driverClassName = com.mysql.jdbc.Driver  
# Specify the DBMS  
spring.jpa.database = MYSQL  
# Show or not log for each sql query  
spring.jpa.show-sql = true  
# Hibernate ddl auto (create, create-drop, update)  
spring.jpa.hibernate.ddl-auto = update  
# Naming strategy  
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy  
  
# stripped before adding them to the entity manager)  
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

application.yml  yml文件的好处,天然的树状结构,一目了然。key后面的冒号需要跟一个空格

server:  
  port: 8080  
  session-timeout: 30  
  tomcat.max-threads: 0  
  tomcat.uri-encoding: UTF-8  
  
spring:  
  datasource:  
    url : jdbc:mysql://localhost:3306/springboot  
    username : root  
    password : root  
    driverClassName : com.mysql.jdbc.Driver  
  jpa:  
    database : MYSQL  
    show-sql : true  
    hibernate:  
      ddl-auto : update  
      naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy  
    properties:  
      hibernate:  
        dialect : org.hibernate.dialect.MySQL5Dialect

 

读取application.yml里面的常量值

com:
   name: chenkai
@Component
public class BlogProperties {
    @Value("${com.name}")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

测试不同配置的加载

执行java -jar xxx.jar,可以观察到服务端口被设置为1111,也就是默认的开发环境(dev)

执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为2222,也就是测试环境的配置(test)

执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为3333,也就是生产环境的配(prod)

相关标签: SpringBoot