Spring Boot Database Operations

Source: Internet
Author: User

# Spring Boot Database operation data Source configuration
    • Oracle Data Source Configuration

      Reference to "Build Spring Boot project-six"

    • MySQL Data Source Configuration

      • Adding dependencies in the POM

        <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 following configuration in the configuration file

        spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/mysqlspring.datasource.username=用户名spring.datasource.password=密码# [create|create-drop|update|validate]spring.jpa.hibernate.ddl-auto=update
Database operations
  • Adding entity classes

    @Entity@Table(name = "OMS_USER")public class OmsUser implements Serializable {    private static final long serialVersionUID = 1L;    @Id    private String id;    @Column(name = "DISPLAY_ID")    private String displayId;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getDisplayId() {        return displayId;    }    public void setDisplayId(String displayId) {        this.displayId = displayId;    }}

    PS: Entity fields that are not mapped into columns, use @Transient annotations.

  • Add DAO

    Inherit the JpaRepository class (a simple crud operation is encapsulated).

    public interface OmsUserRepository extends JpaRepository<OmsUser, String> {}
  • Test

    @RestController@RequestMapping(value = "/api")public class StudentController {    @Autowired    OmsUserRepository omsUserRepository;    @GetMapping(value = "/user")    public List<OmsUser> getAllUser() {        return this.omsUserRepository.findAll();    }    @GetMapping(value = "/user/{id}")    public OmsUser getUserById(@PathVariable("id") String id) {        return omsUserRepository.findById(id).get();    }}

Spring Boot Database Operations

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.