spring-boot(一)环境搭建

spring boot 学习记录

项目的目录结构

这里写图片描述

解释:

1. pom.xml

<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>cn.springbooot</groupId>
  <artifactId>SpringBootFrist</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBootFrist</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--  日志文件打印 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!-- MYSQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
         </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.1.1</version>
        </dependency>
        <!--  使用thymeleaf  模版引擎   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.4.3.RELEASE</version>
        </dependency>
        <!--  POI 配置信息 -->
        <dependency>  
           <groupId>org.apache.poi</groupId>  
           <artifactId>poi</artifactId>  
           <version>3.11</version>  
       </dependency>  
    </dependencies>

</project>

2.application.properties

1.数据库用的是mysql
2.server.port 修改启动端口号
3.数据库操作用的JPA,hibernate配合
4.前端框架用的是:thymeleaf ,
5.样式插件: bootStrap

# mysql 配置 
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# 服务器端口、连接时间
server.port=8011
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

#JPA Configuration:  
spring.jpa.database=MySQL
spring.jpa.show-sql=true  
spring.jpa.generate-ddl=true  
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect  
#spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy  

#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end

debug=true
spring.jpa.show-sql=true

2.ReadingListApplication.java 是启动类

其中需要注意:@ComponentScan(basePackages={“cn.springbooot.SpringBootFrist”})
扫描的路径

package cn.springbooot.SpringBootFrist;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"cn.springbooot.SpringBootFrist"})
public class ReadingListApplication {

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

}

4. 对于前端thymeleaf引入

首先需要在路径:/src/main/resources新建两个包:
命名分别为: static–(存放静态的资源),例如bootStarp
templates–(存放页面的资源)

如果不新建会报错,因为thtmeleaf默认的包就是这两个。

ThymeleafProperties.java
public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    public ThymeleafProperties() {
        this.checkTemplateLocation = true;
        this.prefix = "classpath:/templates/"; // 默认存放的路径
        this.suffix = ".html";  // 默认后缀是html
        this.mode = "HTML5";    // 模式html5
        this.encoding = "UTF-8"; // 编码格式是 UTF-8
        this.contentType = "text/html";
        this.cache = true;      // 是否开启缓存
        this.enabled = true;
    }

5. bootstarp

下载直接放在新建static下就可以了

6. 进行测试

SampleController .java

package cn.springbooot.SpringBootFrist.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SampleController {

    @RequestMapping("/")
    String home(ModelMap map){
        List<UserLogin> listus = new ArrayList<UserLogin>();

        UserLogin ul = new UserLogin();
        ul.setId(1);
        ul.setUsername("小名");
        ul.setPassword("1234");

        UserLogin u1 = new UserLogin();
        u1.setId(2);
        u1.setUsername("小明");
        u1.setPassword("1234");

        UserLogin u2 = new UserLogin();
        u2.setId(2);
        u2.setUsername("笑话");
        u2.setPassword("1234");

        UserLogin u3 = new UserLogin();
        u3.setId(3);
        u3.setUsername("小刚");
        u3.setPassword("1234");
        listus.add(u1);
        listus.add(u2);
        listus.add(u3);
        listus.add(ul);
        map.addAttribute("ul", listus);
        map.addAttribute("word", "世界很美好");
        return "index";
    }
}

main.html
作为一个公共的样式和js引入

<head>
<meta content="text/html;charset=UTF-8"></meta>
<meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
<meta name="viewport" content="width=device-width,initial-scale=1"></meta>
<link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/>
</head>
<body>
</body>

index.html
th:text 等都是thymeleaf的语法

<!DOCTYPE html>
<html>
<head th:include="main">
</head>
<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">访问model</h3>
        </div>
        <div class="panel-body">
            <span th:text="${word}"></span>
        </div>
    </div>

    <div th:if="${not #lists.isEmpty(ul)}">
        <div class="panel panel-primary">
            <h3 class="panel-title">列表</h3>
        </div>
        <div class="panel-body">
            <ul class="list-group">
                <li class="list-group-item" th:each="user:${ul}">
                    <span th:text="${user.id}"></span>
                    <span th:text="${user.loginName}"></span>
                    <span th:text="${user.loginPassword}"></span>
                </li>
            </ul>
        </div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值