Spring Boot(一)

1、starter

spring boot是以极简为风格,以默认替代配置的思想。内嵌了tomcat、jetty等容器。pom.xml文件如下,其中spring-boot-starter-web不仅包含spring-boot-starter,还自动开启了web功能。

<?xml version="1.0" encoding="UTF-8"?>
<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.forezp</groupId>
    <artifactId>springboot-first-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-first-application</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication .class, args);
    }

    @RequestMapping(value = "/test")
    public String test(){
        return "test";
    }

}

启动springboot有三种方式,运行Application的main方法,通过maven命令中的spring-boot插件启动,以jar包方式启动。

// maven命令启动
mvn clean
mvn package
mvn spring-boot:run
//在target目录下面找到jar包
java -jar springBoot.jar

2、配置文件

 springBoot支持两种格式的配置,yml和properties。新建项目的时候,默认配置文件为application.properties。

将application.yml中的配置赋值给java对象

student:
 name: forezp
 age: 12
@ConfigurationProperties(prefix = "student")
public class Student{
    private String name;
    private int age;
}

在启动类MyApplication前面加上注解

@EnableConfigurationProperties({Student.class})
@SpringBootApplication
public class MyApplication {

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

如果不写在application.yml,比如新建一个test.properties

student.name=forezp
student.age=12
@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "student")
public class Student{
    private String name;
    private int age;
}

多环境配置格式为application-{profile}.properties,其中{profile}对应你的环境标识,例如

  • application-test.properties:测试环境
  • application-dev.properties:开发环境
  • application-prod.properties:生产环境

在application.yml中指定使用哪个配置文件

 spring:
  profiles:
    active: dev

3、引入mybatis

在pom文件中添加三个依赖

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter<artifactId>
            <version>1.3.0</version>
</dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
</dependency>
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
 </dependency>

在application.yml中添加配置

spring:
  datasource:
    url:jdbc:mysql://localhost:3306/test
    username:root
    password:123456
    driver-class-nam:com.mysql.jdbc.Driver

dao层

@Mapper
public interface AccountMapper {
    @Insert("insert into account(name, money) values(#{name}, #{money})")
    int add(@Param("name") String name, @Param("money") double money);
}

service层

@Mapper
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;
    
    public void test(String name,Double money){
            accountMapper.add(name,money);
    }
}

可以发现,只要修改pom文件和配置文件,就可以很轻松的在spring boot中引入框架了。

posted @ 2019-03-25 11:54  吴浩2008  阅读(186)  评论(0)    收藏  举报