SpringBoot 1.5.10.RELEASE学习

作者: tanoak | 来源:发表于2018-02-10 17:20 被阅读729次

    SpringMvc+SpringData+thymeleaf

    接触了SpringBoot也有4个月了,一直没有整理自己的笔记,骚年,接招


    我使用的是IntelliJ IDEA 的环境,jdk1.8 SpringBoot采用的是约定由于配置,这一点要牢牢记住,后面我会在解析源码时做解释,首先查看下目录结构 :



    然后开始我们的配置之路

    1. pom文件,可以直接粘贴
      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.tanoak</groupId>
       <artifactId>bootjpa</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
       <name>bootjpa</name>
     <description>Demo project for Spring Boot</description>
      <parent> 
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
       <relativePath/> 
      </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-aop</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-web</artifactId>
       </dependency>
       <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
       </dependency>
       <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
       </dependency>
       <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
       </dependency>
       <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
        </dependency>
     </dependencies>   
    <build>
     <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
       <configuration>
         <fork>true</fork>
       </configuration> 
      </plugin>
     </plugins>
     </build>     
    </project>
    
    1. 导入了相应的依赖,我们开始编写我们的配置文件,application.yml,注意这里有坑,yml文件是以空格控制的,因此要留意空格
    server:
     port: 8082
      session-timeout: 30
      tomcat.max-threads: 0
      tomcat.uri-encoding: UTF-8
    #HTTPS配置 
    #  ssl: 
    #    key-store: keystore.p12 
    #    key-store-password: tanoak 
    #    key-store-type: JKS 
    #    key-alias: tomcat 
    #数据源配置
    spring:
     datasource: url : jdbc:mysql://localhost:3306/test
        username : root
        password : 123123
        driverClassName : com.mysql.jdbc.Driver
    #SpringData配置
      jpa:
     database : MYSQL
        show-sql : true
        hibernate:
     ddl-auto : update
          naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy
        properties:
     hibernate: dialect : org.hibernate.dialect.MySQL5Dialect
    #thymeleaf配置
      thymeleaf:
     cache: false
        mode: HTML5
        encoding: utf-8
        content-type: text/html
    
    1. 配置都OK,我们开始编写Dao层,配置Bean,这里注意下。我使用了lombok,不会可以参考这篇文章lombok使用
      a. entity层 Hello实体
    /**
     * @Author tanoak
     * Created By  on 2018/1/19 * @Describe 测试类
     */
    @Entity
    @Data
    public class Hello {
     @Id 
     @GeneratedValue
     private Long id ;
     @Column(nullable = true)
     private String name ;
    }
    

    b.Dao层 只需要继承JpaRespsitory类,就具备了基本的CRUD

    /**
     * @Author tanoak
     * Created By  on 2018/1/19 * @Describe
      */
    @Repository public interface HelloDao extends JpaRepository<Hello ,Long> {
    }
    

    c. Service层 正常情况应该是Controller调用Service,这里调试方便省略


    d. Controller层

    /**
     * @Author tanoak
     * Created By  on 2018/1/19 
     * @Describe 测试 页面
     */ 
    @Controller
    public class HelloController {
      @Resource
      private HelloDao helloDao;
       
      @RequestMapping("/test")
      public String test(Model model){
        Hello hello = helloDao.findOne((long) 1);
        String name = hello.getName();
        model.addAttribute("name",name) ;
       return "hello" ;//跳转到 templates下的hello.html
      }
    }
    

    e. hello.html 注意重点是(xmlns:th="http://www.thymeleaf.org")这个依赖

    <!DOCTYPE html> 
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
     <meta charset="UTF-8"/>
     <title>Hello html</title>
    </head>
    <body>
     <h1 th:text="${name}">hello</h1>
    </body>
    </html>
    

    项目书写完毕,接下来我们访问下看是否OK

    访问OK!!!,下一篇文章开始thymeleaf模板学习之旅,抛弃传统的jsp

    相关文章

      网友评论

        本文标题:SpringBoot 1.5.10.RELEASE学习

        本文链接:https://www.haomeiwen.com/subject/wjzctftx.html