SpringBoot

 SpringBoot启动

  @SpringBootApplication作用

      @SpringBootConfiguration(@Configuration)

         @EnableAutoConfiguration

          Spring Boot 自动配置标记。SpringBoot内置大量的自动配置组件,可以在启动时加载创建对象纳入Spring容器,例如dispatcherServlet、dataSource、jdbcTemplate等等。

         @ComponentScan

           SpringBoot 扫描标记.等价于<context:component-scan base-package="xx"/>

 

Properties配置参数注入
  可以将application.properties文件中的参数注入给Spring容器中的Bean对象。

 

server.port=8888

spring.datasource.username=root
spring.datasource.password=123123123
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driverClassName=com.mysql.jdbc.Driver

my.db.url=jdbc.oracle.thin:@asdasdasdasd
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class MyBean {

    private String username;
    private String password;

    @Value("${my.db.url}")
    private String url1;

 

 

 

 

 

 

单独构建Springboot框架: 

pom.xml   

<parent>
        <!--设置boot版本 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
</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>
        <!--springboot Web包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf模板包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <dependency>
            <!--数据库访问基于jdbc -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <!-- 数据库驱动包 -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>

        <!-- mybatis、mybatis-spring、autocofigurer -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        
</dependencies>

<build />

 

application.properties

server.port=8888 
 
spring.datasource.username=root
spring.datasource.password=666666
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driverClassName=com.mysql.jdbc.Driver


my.db.url=jdbc.oracle.thin\:@asdasdasdasd
 

启动类

@SpringBootApplication
@MapperScan(basePackages = { "com.xiaoka.dao" })
public class MyBegin { public static void main(String[] args) { //启动Boot程序 SpringApplication.run(MyBegin.class, args); } }

Controller

@RestController
public class MyControllerDemo {
    @Autowired
    private MySpringbootService mySpringbootService;

    @RequestMapping("/hello")
    public ModelAndView dCon() {
        List<Employee> list = mySpringbootService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("asd");
        modelAndView.getModel().put("list", list);

        return modelAndView;
    }

Service

@Service
public class MySpringbootService {
    @Autowired
    private MySpringbootDao mySpringbootDao;

    public List<Employee> getAll() {
        List<Employee> list = mySpringbootDao.selectAll();
        return list;
    }

}

Dao

public interface MySpringbootDao {
    @Select("select * from Employee")
    public List<Employee> selectAll();

}

templates (src/main/resources目录下 templates 文件夹中添加html文件)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <tr th:each="employee: ${list}">
        <td th:text="${employee.name}"></td>
        <td th:text="${employee.id}"></td>
        <td th:text="${employee.age}"></td>
    </tr>
</body>
</html>

 

启动MyBootApplication测试

http://localhost:8888/hello

其他功能

  1. 热启动

    修改Java代码后,自动redeploy

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <version>2.0.1.RELEASE</version>
    </dependency>
 
 
 


posted @ 2018-09-21 20:08  Hero丶小卡  阅读(135)  评论(0编辑  收藏  举报