"Spring Boot" first project Springboot + MySQL + hibernate

Source: Internet
Author: User

 

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
  1. Package Com.example.demo;
  2. Import javax.persistence.Entity;
  3. Import Javax.persistence.GeneratedValue;
  4. Import Javax.persistence.Id;
  5. @Entity
  6. public class Person {
  7. @Id
  8. @GeneratedValue
  9. Private Long ID;
  10. private String name;
  11. Private String address;
  12. Public String GetName () {
  13. return name;
  14. }
  15. public void SetName (String name) {
  16. this.name = name;
  17. }
  18. Public String getaddress () {
  19. return address;
  20. }
  21. public void setaddress (String address) {
  22. this.address = address;
  23. }
  24. }
Be aware that @entity comes from the Javax package.

DAO Class:

[HTML]View PlainCopy
    1. Package Com.example.demo;
    2. Import java.util.List;
    3. Import Org.springframework.data.jpa.repository.JpaRepository;
    4. Public interface Dao extends jparepository< person, Long>{
    5. List<person> Findbyname (String name);
    6. }
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
  1. Package Com.example.demo;
  2. Import org.springframework.beans.factory.annotation.Autowired;
  3. Import org.springframework.boot.SpringApplication;
  4. Import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. Import org.springframework.web.bind.annotation.RequestMapping;
  6. Import Org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @SpringBootApplication
  9. public class Demo2application {
  10. @Autowired
  11. DAO DAO;
  12. @RequestMapping ("/get")
  13. Public person GETP (String name) {
  14. Person person = dao.findbyname (name). Get (0);
  15. return person;
  16. }
  17. @RequestMapping ("/")
  18. Public String Index () {
  19. Return "Hello Spring Boot";
  20. }
  21. public static void Main (string[] args) {
  22. Springapplication.run (Demo2application.class, args);
  23. }
  24. }

Add only one URL to check the data.

Finally, the configuration file:

[HTML]View PlainCopy
  1. Spring.datasource.driver-class-name=Com.mysql.jdbc.Driver
  2. Spring.datasource.url=jdbc:mysql://localhost:3306/test? characterencoding=UTF-8
  3. Spring.datasource.username=Root
  4. spring.datasource.password=
  5. Spring. jpa.hibernate.ddl-auto=Update;
  6. Spring.jpa.show-sql=True
  7. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.