Today the company to do a small project, long time No Touch project (Brush the problem is very tired ...) ), I heard that spring boot was very hot and decided to try it. Read the data from MySQL for the time being, use Hiberante.
1. Get the jar package.
Get from http://start.spring.io/, of course for using eclipse (without ... Students, there is STS plug-in support, about plug-in usage of Baidu, very simple. The key is to select the features that you want to support. Here is the reading data, I remember only selected WEB,JPA and MySQL three tags.
2. Import the project.
New entity class:
[HTML]View PlainCopy
- Package Com.example.demo;
- Import javax.persistence.Entity;
- Import Javax.persistence.GeneratedValue;
- Import Javax.persistence.Id;
- @Entity
- public class Person {
- @Id
- @GeneratedValue
- Private Long ID;
- private String name;
- Private String address;
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- Public String getaddress () {
- return address;
- }
- public void setaddress (String address) {
- this.address = address;
- }
- }
Be aware that @entity comes from the Javax package.
DAO Class:
[HTML]View PlainCopy
- Package Com.example.demo;
- Import java.util.List;
- Import Org.springframework.data.jpa.repository.JpaRepository;
- Public interface Dao extends jparepository< person, Long>{
- List<person> Findbyname (String name);
- }
You only need to inherit a JPA class. And do not need to implement, provide the default lookup method ... Very convenient.
Controller
[HTML]View PlainCopy
- Package Com.example.demo;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import org.springframework.boot.SpringApplication;
- Import org.springframework.boot.autoconfigure.SpringBootApplication;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class Demo2application {
- @Autowired
- DAO DAO;
- @RequestMapping ("/get")
- Public person GETP (String name) {
- Person person = dao.findbyname (name). Get (0);
- return person;
- }
- @RequestMapping ("/")
- Public String Index () {
- Return "Hello Spring Boot";
- }
- public static void Main (string[] args) {
- Springapplication.run (Demo2application.class, args);
- }
- }
Add only one URL to check the data.
Finally, the configuration file:
[HTML]View PlainCopy
- Spring.datasource.driver-class-name=Com.mysql.jdbc.Driver
- Spring.datasource.url=jdbc:mysql://localhost:3306/test? characterencoding=UTF-8
- Spring.datasource.username=Root
- spring.datasource.password=
- Spring. jpa.hibernate.ddl-auto=Update;
- Spring.jpa.show-sql=True
- Spring.jackson.serialization.indent-output=True
Before accessing, the database is now built into tables and interpolated data.
Then launch, browser access.
Browser:
Indicates that the access was successful.
The overall feeling is that this framework helps programmers achieve too many functions, originally light a hibernate need n multi-configuration, but in the springboot there is no sense of hibernate. Have time to study the source code, see how he did it.
This is the end of this simple demo.
"Spring Boot" first project Springboot + MySQL + hibernate