SpringBoot 简单入门

1...SpringBoot 简单介绍:

      设计目的:是用来简化新的spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不在需要定义模板化的配置。

             特点:1  可以创建独立的spring应用程序,并且基于maven 或 gradle 插件,可以创建可执行的jars和wars。 2 内嵌tomcat或jetty 等servlet容器。

                        3  提供自动配置的 starter 项目对象模型 (poms)以简化maven配置。 4  尽可能自动配置spring容器。   5  提供准备好的特性,如:指标健康检查和外部化配置。    6  绝对没有代码的生成,不                               需要xml的配置。

            说明: 前端常使用模板引擎,主要有FreeMarker和Thymeleaf,它们都是用Java语言编写的,渲染模板并输出相应文本,使得界面的设计与应用的逻辑分离,同时前端开发还会使用到Bootstrap、                                        AngularJS、JQuery等;在浏览器的数据传输格式上采用Json,非xml,同时提供RESTfulAPI;SpringMVC框架用于数据到达服务器后处理请求;到数据访问层主要有Hibernate、MyBatis、JPA等持                         久层框架;数据库常用mysql;开发工具推荐IntelliJIDEA.

2....简单入门程序:

1...pom.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.wsc</groupId>
 8     <artifactId>springBoot01</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <!--继承SpringBoot父类  spring-boot-starter-parent-->
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.0.2.RELEASE</version>
15     </parent>
16 
17     <properties>
18         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19         <maven.compiler.source>1.8</maven.compiler.source>
20         <maven.compiler.target>1.8</maven.compiler.target>
21     </properties>
22     <dependencies>
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <!--添加起步依赖-->
28         <dependency>
29             <groupId>mysql</groupId>
30             <artifactId>mysql-connector-java</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-freemarker</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-data-jpa</artifactId>
39         </dependency>
40     </dependencies>
41 </project>
pom.xml

2......./resouces/application.properties(数据源 文件 不可变)

 1 #DB Configuration:
 2 spring.datasource.driverClassName=com.mysql.jdbc.Driver
 3 spring.datasource.password=wsc
 4 spring.datasource.username=root
 5 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?CharacterEncoding=utf-8
 6 
 7 #JPA Configuration:
 8 #spring.jpa.database=MySQL
 9 #spring.jpa.show-sql=true
10 #spring.jpa.generate-ddl=true
数据库信息

3.......resouces/tempplates/flight.ftl ( *.ftl  后缀 不可改变)

 1 <html>
 2 <head>
 3     <title>spring boot</title>
 4 </head>
 5 <body>
 6 <table border="1">
 7     <thead>
 8     <tr>
 9         <th>航班ID</th>
10         <th>航班编号</th>
11         <th>起飞城市</th>
12         <th>起飞时间</th>
13         <th>到达城市</th>
14         <th>到达时间</th>
15     </tr>
16     </thead>
17     <tbody>
18     <!--循环
19         list
20     -->
21     <#list flightList as flight>
22         <tr>
23             <td>${flight.id}</td>
24             <td>${flight.flightNo}</td>
25             <td>${flight.departureCity}</td>
26             <td>${flight.departureTime}</td>
27             <td>${flight.arrivalCity}</td>
28             <td>${flight.arrivalTime}</td>
29         </tr>
30     </#list>
31     </tbody>
32 </table>
33 <#--图片访问地址格式-->
34 <img src="/images/wsc.jpg">
35 </body>
36 </html>
页面

4...resouces/static/images   js    css   

5......entity 

 1 package com.wsc.core.entity;
 2 
 3 import javax.persistence.*;
 4 import java.util.Date;
 5 
 6 /**
 7  * @version 1.0
 8  * @ClassName Flight
 9  * @Description TODO
10  * @Author WSC
11  * @Date 2019/8/22 14:35
12  **/
13 @Entity //实体构造
14 @Table(name="flight") //表名
15 public class Flight {
16     @Id
17     @GeneratedValue(strategy = GenerationType.IDENTITY) //自增设置
18     /**
19      * @Column 注解(表的字段)  必须加上 ,不然 test 会报错
20      *   报错信息 : 数据库表 不存在 某个字段
21      *   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.ac_flight' doesn't exist
22      */
23     @Column(name="flight_id")
24     private Integer id;
25     @Column(name="flight_no")
26     private String flightNo;
27         @Column(name="departure_city")
28     private String departureCity;
29     @Column(name="departure_time")
30     private Date departureTime;
31     @Column(name="arrival_time")
32     private Date arrivalTime;
33     @Column(name="arrival_city")
34     private String arrivalCity;
35 
36     public Integer getId() {
37         return id;
38     }
39 
40     public void setId(Integer id) {
41         this.id = id;
42     }
43 
44     public String getFlightNo() {
45         return flightNo;
46     }
47 
48     public void setFlightNo(String flightNo) {
49         this.flightNo = flightNo;
50     }
51 
52     public String getDepartureCity() {
53         return departureCity;
54     }
55 
56     public void setDepartureCity(String departureCity) {
57         this.departureCity = departureCity;
58     }
59 
60     public Date getDepartureTime() {
61         return departureTime;
62     }
63 
64     public void setDepartureTime(Date departureTime) {
65         this.departureTime = departureTime;
66     }
67 
68     public Date getArrivalTime() {
69         return arrivalTime;
70     }
71 
72     public void setArrivalTime(Date arrivalTime) {
73         this.arrivalTime = arrivalTime;
74     }
75 
76     public String getArrivalCity() {
77         return arrivalCity;
78     }
79 
80     public void setArrivalCity(String arrivalCity) {
81         this.arrivalCity = arrivalCity;
82     }
83 
84     @Override
85     public String toString() {
86         return "Flight{" +
87                 "id=" + id +
88                 ", flightNo='" + flightNo + '\'' +
89                 ", departureCity='" + departureCity + '\'' +
90                 ", departureTime=" + departureTime +
91                 ", arrivalTime=" + arrivalTime +
92                 ", arrivalCity='" + arrivalCity + '\'' +
93                 '}';
94     }
95 }
View Code

6.....dao

1 package com.wsc.core.dao;
2 
3 import com.wsc.core.entity.Flight;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 // 继承JpaRepository
6 public interface FlightDao extends JpaRepository<Flight,Integer> {
7 }
View Code

7.....controlle

 1 package com.wsc.core.controller;
 2 
 3 import com.wsc.core.dao.FlightDao;
 4 import com.wsc.core.entity.Flight;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import java.util.List;
11 
12 /**
13  * @version 1.0
14  * @ClassName ShowController
15  * @Description TODO
16  * @Author WSC
17  * @Date 2019/8/22 16:24
18  **/
19 @Controller
20 public class ShowController {
21     @Autowired
22     private FlightDao flightDao;
23     @RequestMapping("/flight/data") //访问地址
24     //返回页面数据
25     public String showData(Model model){
26         List<Flight> flightList = flightDao.findAll();
27        model.addAttribute("flightList", flightList);
28         return "flight";
29     }
30 }
显示页面controller
 1 package com.wsc.core.controller;
 2 
 3 import com.wsc.core.dao.FlightDao;
 4 import com.wsc.core.entity.Flight;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 import java.util.List;
10 
11 /**
12  * @version 1.0
13  * @ClassName FlightController
14  * @Description TODO
15  * @Author WSC
16  * @Date 2019/8/22 14:58
17  **/
18 @RestController //返回json数据 的格式
19 public class FlightController {
20     @Autowired
21     private FlightDao flightDao;
22     @RequestMapping("/flight/findAll") //访问地址
23     //全部数据 查询
24     public List<Flight> findAll(){
25         List<Flight> flightList = flightDao.findAll();
26         return flightList;
27     }
28 }
返回json

8....启动类

 1 package com.wsc.core;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * @version 1.0
 8  * @ClassName Start
 9  * @Description TODO
10  * @Author WSC
11  * @Date 2019/8/22 14:56
12  **/
13 
14 /**
15  * springBoot启动类
16  */
17 @SpringBootApplication
18 public class Start {
19     /**
20      * springBoot 启动方法
21      * @param args
22      */
23     public static void main(String[] args) {
24         //类名.class
25         SpringApplication.run(Start.class,args);
26     }
27 }
View Code

 

转载于:https://www.cnblogs.com/wangshichang/p/11396188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值