Spring boot access MySQL (JPA mode) The simplest configuration

Source: Internet
Author: User

0. First recommend a tool--lombok,pom file as follows:

<Dependency>    <groupId>Org.projectlombok</groupId>    <Artifactid>Lombok</Artifactid>    <Scope>Compile</Scope></Dependency>

You can use annotations @data compile-time to automatically generate Get,set methods, constructors, and ToString methods.

@Data @entity Public classaccount{@IdPrivateString ID; PrivateString account; @Column (Name= "Call_phone")    PrivateString Phone; @Column (Name= "Nick_name")    PrivateString Nickname; PrivateString password; PrivateString Salt; Private intusertype; PrivateString CreateUser; PrivateTimestamp Createtime; Private intState ;}

The resulting effect is as follows:

Add the following dependencies under the 1.pom.xml file, introduce the SPRING-BOOT-JPA jar package, and the MySQL driver

<Dependency>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-starter-data-jpa</Artifactid></Dependency><Dependency>    <groupId>Mysql</groupId>    <Artifactid>Mysql-connector-java</Artifactid></Dependency>

2. Configuring Spring DataSource and Hibernate dialects in/src/main/resources/application.properties

(Spring boot defaults to use Hibernate as the JPA implementation)

spring.datasource.url=jdbc:mysql://localhost:3306/test?usessl=falsespring.datasource.username=  Rootspring.datasource.password=Rootspring.datasource.tomcat.max-active=100  Spring.datasource.tomcat.max-idle=200spring.datasource.tomcat.initialSize=20  Spring.jpa.database-platform=org.hibernate.dialect.mysql5dialect

3. Define the entity and Repository (interface).

The Entity--account entity classes are as follows:

@Entity will be scanned and loaded by spring,

@Id annotations on the primary key

@Column name= "Call_phone" refers to the field name of the database that corresponds to the field, and does not need to be defined if the same. The database underline interval is considered the same as the Hump method in the code, such as database field Create_time equivalent to Createtime in the Java class, so @column annotations are not required.

@Data @entity Public classaccount{@IdPrivateString ID; PrivateString account; @Column (Name= "Call_phone")    PrivateString Phone; @Column (Name= "Nick_name")    PrivateString Nickname; PrivateString password; PrivateString Salt; Private intusertype; PrivateString CreateUser; PrivateTimestamp Createtime; Private intState ;}

Repository as follows:

@Repository  Public Interface extends Jparepository<account, string>{account    Findonebyaccount (String account);

4. Calling in other @component

@RestController @requestmapping ("/subsystem")  Public class subsystemauthorityservice{    @Autowired    accountrepository accountrepository;     = "/admin/info")    public  String getadmininfo (string currentaccount)    {         = Accountrepository.findonebyaccount (currentaccount);        SYSTEM.OUT.PRINTLN (account);         return account.tostring ();    }}

Spring boot access MySQL (JPA mode) The simplest configuration

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.