Spring boot: Using MySQL and JPA in spring boot

Source: Internet
Author: User

This article shows you how to use the MYSQ database in spring boot Web apps, and also shows the benefits of spring boot (as little code and configuration as possible). Data access Layer We will use spring data JPA and hibernate (one of the JPA implementations).

1.Maven pom.xml File

Add the following dependent files to your project

<Dependencies>
<Dependency>
<Groupid>org.springframework.boot</Groupid>
<Artifactid>spring-boot-starter-web</Artifactid>
</Dependency>
<Dependency>
<Groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<dependency>
<groupid>mysql</groupid>
<Artifactid>mysql-connector-java</artifactid>
</dependency>
</dependencies>
2. Attribute Profile Application.properties

Set up the data source and JPA configuration in Src/main/resources/application.properties.

Spring.datasource.url = jdbc:mysqL://localhosT:3306/test
Spring.datasource.username = root
Spring.datasource.password = root
Spring.datasource.driverClassName = Com.mysql.jdbc.Driver
# Specify the DBMS
Spring.jpa.database = MYSQL
# Show or not log for each SQL query
Spring.jpa.show-sql = True
# Hibernate DDL Auto (Create, create-drop, update)
Spring.jpa.hibernate.ddl-auto = Update
# Naming strategy
Spring.jpa.hibernate.naming-strategy = Org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)
Spring.jpa.properties.hibernate.dialect = Org.hibernate.dialect.MySQL5Dialect

All of the configuration is in the file above, no additional XML configuration and Java configuration are required.

The database configuration above, you need to change the address of your database and user name password.

Hibernate's ddl-auto=update configuration table name, database tables and columns are created automatically (based on familiarity with Java entities), where you can see more hibernate configurations.

3.User solids

Create a user entity with the user containing three attributes Id,email and name. The user entity corresponds to the users table of the MySQL database.

@Entity
@Table (name ="Users")
public class User {
//==============
//PRIVATE fields
//==============
//an autogenerated ID (unique for each user in the db)
@Id
@GeneratedValue (strategy = Generationtype.auto)
Private long ID;
//the user email
@NotNull
Private String email;
//the user name
@NotNull
Private String name;
//==============
//public METHODS
//==============
Public User () {}
Public user (long id) {
this.id = ID;
}
//Getter and Setter methods
//...
} //class User
Data access layer for 4.User entities Userdao

In this example, the Userdao is very simple, only need to inherit Crudrespositroy, Crudrespositroy has implemented Save,delete,deleteall,findone and FindAll.

(More magical when these methods are actually not implemented in Crudrespositroy, and by the name of the DAO method can also implement a new method)

@Transactional
crudrepository<long> {
Findbyemail(String email);
5. Test the Controller Usercontroller

Create a new query controller Usercontroller

1 @Controller2  Public classUsercontroller {3     4     5 @Autowired6 Userdao UserDao2;7       8       9@RequestMapping ("/get-by-email")Ten @ResponseBody One      Publicstring Getbyemail (string email) { A String userId; -User User =userdao2.findbyemail (email); -       if(User! =NULL) { theUserId =string.valueof (User.getid ()); -         return"The User ID is:" +userId; -       } -       return"User" + email + "is not exist."; +     } -}

You can use the browser to access the URL Http://127.0.0.1:8080/[email protected]

, you can get the user's ID (you can add a new record to the MySQL database first).

Spring boot: Using MySQL and JPA in spring boot

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.