Skip to content

Commit 723fec3

Browse files
committed
创建SpringBoot的Demo
1 parent 25b4395 commit 723fec3

File tree

13 files changed

+355
-36
lines changed

13 files changed

+355
-36
lines changed

SimplestSpringBootDemo/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 最简JAVA程序之最简SpringBoot
2+
### 1.运行前说明:
3+
本项目使用maven进行搭建,如果您的IDE中集成了maven,请直接导入maven项目;
4+
5+
### 2.最简SpringBoot程序的搭建步骤:
6+
1. 最为关键的是使用maven来构建程序.创建pom.xml文件,引入相应的starter模块,即可将spring相关的jar包引进来,具体到
7+
本项目:
8+
1. 定义parent,这个会限定spring的版本:
9+
2. 引入相应的模块,在本项目中包括:
10+
11+
| 模块 | 作用 |
12+
|------------|-------------|
13+
|spring-boot-starter-web|引入web相关的springboot模块|
14+
|spring-boot-starter-jdbc|引入数据库访问的springboot模块|
15+
16+
3. 引入其他的相关技术:包括thymeleaf(用来构建前台页面)和h2Database(用来创建一个运行在内存中的数据库)
17+
18+
2. 按照模块的**约定**创建主体的程序:
19+
- Controller:使用@Controller注解标注,使用@RequestMapping映射访问路径(和SpringMVC的用法一致)
20+
- Repository:对本项目来说,使用jdbc,只需要在相应类中注入JDBCTemplate,使用JDBCTemplate执行sql语句即可.
21+
- 前台页面相关:Controller返回的string会默认配置在templates目录下,所以将相应的html文件放到这个目录下,
22+
springBoot将自动去这个目录下寻找资源;
23+
- 静态资源:默认在static等目录下,所以放在这几个目录下即可.
24+
25+
3. Application的创建:
26+
启动SpringBoot需要创建一个带main方法的类启动,并且需要@ComponentScan@EnableAutoConfiguration开启自动配置,
27+
然后SpringBoot将会自动所有包中的文件,启动.
28+
```
29+
package chentao.project;
30+
31+
import org.springframework.boot.SpringApplication;
32+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
33+
import org.springframework.context.annotation.ComponentScan;
34+
35+
@ComponentScan
36+
@EnableAutoConfiguration
37+
public class Application {
38+
public static void main(String [] args){
39+
SpringApplication.run(Application.class,args);
40+
}
41+
}
42+
43+
44+
```
45+
### 3 更多说明:
46+
1. SpringBoot使用约定的方式进行项目的管理,会根据项目中引入的模块自动的开启相关的配置,比如说引入了Jdbc模块,就会根据项目中属性配置
47+
去自动创建Connection,JDBCTemplate的相应的依赖,或者进行注册,简化了XML的配置.
48+
2. SpringBoot中集成了Tomcat模块,所以启动SpringBoot很简单,不需要额外通过IDE配置.

SimplestSpringBootDemo/pom.xml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>chentao.project</groupId>
6+
<artifactId>SpringBoot</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<repositories>
11+
<!-- for spring milestone -->
12+
<repository>
13+
<id>spring.milestone</id>
14+
<url>http://repo.spring.io/milestone</url>
15+
</repository>
16+
</repositories>
17+
<pluginRepositories>
18+
<!-- for spring milestone -->
19+
<pluginRepository>
20+
<id>spring.milestone</id>
21+
<url>http://repo.spring.io/milestone</url>
22+
</pluginRepository>
23+
</pluginRepositories>
24+
25+
<parent>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>1.1.4.RELEASE</version>
29+
</parent>
30+
31+
<properties>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
</properties>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.thymeleaf</groupId>
43+
<artifactId>thymeleaf-spring4</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-jdbc</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.h2database</groupId>
53+
<artifactId>h2</artifactId>
54+
</dependency>
55+
</dependencies>
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package chentao.project;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
@ComponentScan
8+
@EnableAutoConfiguration
9+
public class Application {
10+
public static void main(String [] args){
11+
SpringApplication.run(Application.class,args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package chentao.project;
2+
3+
public class Contact {
4+
private Long id;
5+
private String firstName;
6+
private String lastName;
7+
private String phoneNumber;
8+
private String emailAddress;
9+
10+
public Long getId() {
11+
return id;
12+
}
13+
14+
public void setId(Long id) {
15+
this.id = id;
16+
}
17+
18+
public String getFirstName() {
19+
return firstName;
20+
}
21+
22+
public void setFirstName(String firstName) {
23+
this.firstName = firstName;
24+
}
25+
26+
public String getLastName() {
27+
return lastName;
28+
}
29+
30+
public void setLastName(String lastName) {
31+
this.lastName = lastName;
32+
}
33+
34+
public String getPhoneNumber() {
35+
return phoneNumber;
36+
}
37+
38+
public void setPhoneNumber(String phoneNumber) {
39+
this.phoneNumber = phoneNumber;
40+
}
41+
42+
public String getEmailAddress() {
43+
return emailAddress;
44+
}
45+
46+
public void setEmailAddress(String emailAddress) {
47+
this.emailAddress = emailAddress;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chentao.project;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@Controller
12+
@RequestMapping("/")
13+
public class ContactController {
14+
15+
private ContactRepository contactRepo;
16+
17+
@Autowired
18+
public ContactController(ContactRepository contactRepo){
19+
this.contactRepo = contactRepo;
20+
}
21+
22+
@RequestMapping(method = RequestMethod.GET)
23+
public String home(Map<String,Object> model){
24+
List<Contact> contacts = contactRepo.findAll();
25+
model.put("contacts",contacts);
26+
return "home";
27+
}
28+
29+
@RequestMapping(method = RequestMethod.POST)
30+
public String submit(Contact contact){
31+
contactRepo.save(contact);
32+
return "redirect:/";
33+
}
34+
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package chentao.project;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.jdbc.core.RowMapper;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.util.List;
11+
12+
@Repository
13+
public class ContactRepository {
14+
15+
@Autowired
16+
private JdbcTemplate jdbc;
17+
18+
public List<Contact> findAll(){
19+
return jdbc.query(
20+
"select id,firstName,lastName,phoneNumber,emailAddress " +
21+
"from contacts order by lastName",
22+
new RowMapper<Contact>() {
23+
@Override
24+
public Contact mapRow(ResultSet rs, int i) throws SQLException {
25+
Contact contact = new Contact();
26+
contact.setId(rs.getLong(1));
27+
contact.setFirstName(rs.getString(2));
28+
contact.setLastName(rs.getString(3));
29+
contact.setPhoneNumber(rs.getString(4));
30+
contact.setEmailAddress(rs.getString(5));
31+
return contact;
32+
}
33+
}
34+
);
35+
}
36+
37+
public void save(Contact contact){
38+
jdbc.update(
39+
"insert into contacts "+"(firstName,lastName ,phoneNumber, emailAddress)"
40+
+ "values (?,?,?,?)",
41+
contact.getFirstName(),contact.getLastName(),
42+
contact.getPhoneNumber(),contact.getEmailAddress()
43+
);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
create table contacts(
2+
id IDENTITY,
3+
firstName VARCHAR (30) not null,
4+
lastName VARCHAR (50) not null,
5+
phoneNumber VARCHAR (13),
6+
emailAddress varchar(30)
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
body{
2+
background-color: #eeeeee;
3+
font-family:sans-serif;
4+
}
5+
label {
6+
display:inline-block;
7+
width: 120px;
8+
text-align: right;
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Spring Boot Contacts</title>
5+
<link rel="stylesheet" th:href="@{/style.css}"/>
6+
</head>
7+
<body>
8+
<h2>Spring Boot Contacts</h2>
9+
10+
<form method="POST">
11+
<label for="firstName">First Name:</label>
12+
<input type="text" name="firstName" id="firstName"/><br/>
13+
14+
<label for="lastName">Last Name:</label>
15+
<input type="text" name="lastName" id="lastName"/><br/>
16+
17+
<label for="phoneNumber">Phone #:</label>
18+
<input type="text" name="phoneNumber" id="phoneNumber"/><br/>
19+
20+
<label for="emailAddress">Email:</label>
21+
<input type="text" name="phoneNumber" id="emailAddress"/><br/>
22+
23+
<input type="submit"/>
24+
</form>
25+
26+
<ul th:each="contact : ${contacts}">
27+
<li>
28+
<span th:text = "${contact.firstName}">First</span>
29+
<span th:text="${contact.lastName}">Last</span>
30+
<span th:text="${contact.phoneNumber}">PhoneNumber</span>
31+
<span th:text="${contact.emailAddress}">emailAddress</span>
32+
</li>
33+
</ul>
34+
</body>
35+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
create table contacts(
2+
id IDENTITY,
3+
firstName VARCHAR (30) not null,
4+
lastName VARCHAR (50) not null,
5+
phoneNumber VARCHAR (13),
6+
emailAddress varchar(30)
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
body{
2+
background-color: #eeeeee;
3+
font-family:sans-serif;
4+
}
5+
label {
6+
display:inline-block;
7+
width: 120px;
8+
text-align: right;
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Spring Boot Contacts</title>
5+
<link rel="stylesheet" th:href="@{/style.css}"/>
6+
</head>
7+
<body>
8+
<h2>Spring Boot Contacts</h2>
9+
10+
<form method="POST">
11+
<label for="firstName">First Name:</label>
12+
<input type="text" name="firstName" id="firstName"/><br/>
13+
14+
<label for="lastName">Last Name:</label>
15+
<input type="text" name="lastName" id="lastName"/><br/>
16+
17+
<label for="phoneNumber">Phone #:</label>
18+
<input type="text" name="phoneNumber" id="phoneNumber"/><br/>
19+
20+
<label for="emailAddress">Email:</label>
21+
<input type="text" name="phoneNumber" id="emailAddress"/><br/>
22+
23+
<input type="submit"/>
24+
</form>
25+
26+
<ul th:each="contact : ${contacts}">
27+
<li>
28+
<span th:text = "${contact.firstName}">First</span>
29+
<span th:text="${contact.lastName}">Last</span>
30+
<span th:text="${contact.phoneNumber}">PhoneNumber</span>
31+
<span th:text="${contact.emailAddress}">emailAddress</span>
32+
</li>
33+
</ul>
34+
</body>
35+
</html>

0 commit comments

Comments
 (0)