SpringBoot unofficial tutorials | Article 25th: 2 hours to learn springboot,
Reprinted please indicate the source:
Http://blog.csdn.net/forezp/article/details/61472783
This article is from Fang zhipeng's blog
1. What is spring boot?
Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.
From official website
Translation: adopted the idea of building a production-ready Spring application. Spring Boot takes precedence over configuration practices and is designed to enable and run as soon as possible.
Spring boot is committed to being concise, allowing developers to write less configuration and faster program running and startup. It is the next-generation javaweb framework and is the basis of spring cloud (microservice.
2. Build the first sping boot program
You can create a project on start. spring. io or using idea. Idea is used in this case.
Procedure:
new prpject -> spring initializr ->{name :firstspringboot , type: mavenproject,packaging:jar ,..} ->{spring version :1.5.2 web: web } -> ....
After the application is created, the corresponding directories and files are generated.
There is an Application class, which is the entry of the program:
@SpringBootApplicationpublic class FirstspringbootApplication { public static void main(String[] args) { SpringApplication.run(FirstspringbootApplication.class, args); }}
Another application. yml file under the resources file is the configuration file of the program. The default value is null. The write point is configured. The program port is 8080, and the context-path is/springboot:
server: port: 8080 context-path: /springboot
Write a HelloController:
@ RestController // is equivalent to adding @ Controller and @ ResponseBodypublic class HelloController {// access/hello or/hi to any address, will return the same result @ RequestMapping (value = {"/hello", "/hi"}, method = RequestMethod. GET) public String say () {return "hi you !!! ";}}
Run main () of the Application, and the rendering starts. Because springboot automatically has a servlet container, you do not need to deploy it to the container and then start the container in a traditional way. You only need to run main (). Open the browser and enter the URL: localhost: 8080/springboot/hi. Then you can see the following in the browser:Hi you !!!
3. Attribute Configuration
Add attributes to the appliction. yml file:
server: port: 8080 context-path: /springbootgirl: name: B age: 18 content: content:${name},age:${age}
In the java file, obtain the name attribute as follows:
@Value("${name}") private String name;
You can also inject attributes into the bean through the ConfigurationProperties annotation, and annotate the bean to the spring container through the Component annotation:
@ConfigurationProperties(prefix="girl")@Componentpublic class GirlProperties { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
In addition, you can use the configuration file to create configuration documents for different environments. For details, see the source code:
spring: profiles: active: prod
4. database operations through jpa
Import jar and add dependency in pom. xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
Add the database configuration in appilication. yml:
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 username: root password: 123 jpa: hibernate: ddl-auto: create show-sql: true
These are common database configurations. ddl_auto: create indicates creating a table in the database, update indicates updating, and create is required for the first startup, if you want to create a database table using the hibernate annotation, you need to change it to update later.
Create an object girl, which is based on hibernate:
@Entitypublic class Girl { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; }}
Create Dao interface. springboot automatically annotates the interface class to the spring container. If you do not need to configure it, you only need to inherit JpaRepository:
// The second parameter is the Id type public interface GirlRep extends JpaRepository <Girl, Integer> {}
Create a GirlController, write an api for getting all the girls, and add the girl's api. Just run it:
@ RestControllerpublic class GirlController {@ Autowired private GirlRep girlRep;/*** query the list of all girls * @ return */@ RequestMapping (value = "/girls", method = RequestMethod. GET) public List <Girl> getGirlList () {return girlRep. findAll ();}/*** Add a girl * @ param cupSize * @ param age * @ return */@ RequestMapping (value = "/girls", method = RequestMethod. POST) public Girl addGirl (@ RequestParam ("cupSize") String cupSize, @ RequestParam ("age") Integer age) {Girl girl Girl = new girl (); Girl. setAge (age); girl. setCupSize (cupSize); return girlRep. save (girl );}}
If a Transaction is required, add the @ Transaction annotation to the service layer. It's already early in the morning. I'm going to bed.
Source code; http://download.csdn.net/detail/forezp/9778235
Follow my column the simplest SpringCloud tutorial ever http://blog.csdn.net/column/details/15197.html
-
Top
-
44
-
Step on
-
4
View comments