Just two steps! A Spring Boot project has been built. This article uses Hibernate to connect to MySQL database on this basis.
1. Add a dependency to pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Copy the code
2. Application. properties Add the database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot? serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= trueCopy the code
If the database connection in the spring. The datasource. Url = JDBC: mysql: / / localhost: 3306 / spring_boot, because mysql version of the problem, may have the following error, add “at the back? ServerTimezone =GMT%2B8 “, set the time zone. No further action is required.
Driver = com.datasource. Driver-class-name = com.mysql.jdbc.driver . Said is com. Mysql. The JDBC Driver has been deprecated, want to use the new Driver com. Mysql. Cj. JDBC. Driver1, to com. Mysql. Cj.. JDBC Driver later everything was normal.
Loading class com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
3. Add an entity class
@entity indicates that this is an Entity class, @table (name= “user”) corresponds to the USE Table in the database, @ID represents the primary key, and @column (name= “Id”) indicates an Id attribute.
@GeneratedValue increates the primary key. If in doubt, see @GeneratedValue source code parsing.
package com.example.demo.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String passWord;
public User(a) {
super(a); }public User(String userName, String passWord) {
super(a);this.userName = userName;
this.passWord = passWord;
}
public Long getId(a) {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName(a) {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord(a) {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord; }}Copy the code
4. Add the Dao
Dao layer is mainly used to realize the increase, delete, check and change of database. The DAO simply inherits the JpaRepository class and can produce SQL automatically based on the method name. For example, findByUserName automatically produces a query method that takes userName.
package com.example.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.domain.User;
public interface UserRepository extends JpaRepository<User.Long> {
User findByUserName(String userName);
}
Copy the code
5. Add the Controller
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/getAllUser")
@ResponseBody
public List<User> findAll(a) {
List<User> list = new ArrayList<User>();
list = userRepository.findAll();
return list;
}
@RequestMapping("/getByUserName")
@ResponseBody
public User getByUserName(String userName) {
User user = userRepository.findByUserName(userName);
returnuser; }}Copy the code
Engineering structure drawing after adding files:
6. Create a database
New database mysql: / / localhost: 3306 / spring_boot, must one step. Hibernate creates tables automatically, but the database has to be built manually.
Use Navicat to create a local database, right-click above the connection name -> New database -> Fill in database information -> OK.
Insert two test data into the user table:
7. Test
Start the project. To test a request with Postman:
http://localhost:8080//user/getAllUser :
http://localhost:8080//user/getByUserName?userName=Turing :