spring boot jpa access mysql

spring boot access mysql
Second way: spring data jpa

Why use jpa:
for the first time using Spring JPA, I felt this thing is simply an artifact, almost do not need to write about what code database access CURD a basic function came out.

How to use:
the introduction of dependence in pom file

<!--jpa -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.1.6.RELEASE</version>
</dependency>

<!-- mysql驱动 -->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
</dependency>

Configuration database:

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# JPA 相关配置
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

JPA configuration

# In SrpingBoot version 2.0, Hibernate create a data table, the default database storage engine choice is MyISAM (before is InnoDB). This parameter is the time in the construction of the table, the default storage engine InnoDB switched to use.

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

Configuring print out SQL statements executed in the log information.

spring.jpa.show-sql=true

Each time you run the program, will not form a new table, the data in the table is not empty, it will only update

spring.jpa.hibernate.ddl-auto=update

Creating Entity Dao Service Controller three files:

 entity:
@Entity
@Table(name = "user")
public class JpaUser {

@Id
@GeneratedValue
private Long id;

private String name;

private int age;

private String sex;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

}

Dao
write a simple CRUD interface inheritance JpaRepository can be used directly

@Repository
public interface JpaUserDao extends JpaRepository<JpaUser,Long> {

}

Service

@Service
public class JpaUserService {

@Autowired
JpaUserDao userDao;

public JpaUser add(JpaUser user){
  return   userDao.save(user);
}

public JpaUser update(JpaUser user){
    return   userDao.save(user);
}

public JpaUser getById(long id){
    return   userDao.findById(id).get();
}

}

Controller

@RestController
@RequestMapping("/jpaUser")
public class JpaUserController {

@Autowired
JpaUserService userService;

@RequestMapping(value = "/add",method = RequestMethod.POST)
public Object addUser(@RequestBody JpaUser user){

    return userService.add(user);
}

@RequestMapping(value = "/update",method = RequestMethod.PUT)
public Object updateUser(@RequestBody JpaUser user){

    return userService.update(user);
}

@RequestMapping(value = "/find",method = RequestMethod.GET)
public Object updateUser(long id){
    return userService.getById(id);
}

}

Access to complete the boot test

jpa configuration instructions:

spring.jpa.hibernate.ddl-auto = create ---- each time you run the program, will not form a new table, the table will have a drop table recreate the table

spring.jpa.hibernate.ddl-auto = create-drop ---- the end of each program will clear the table

spring.jpa.hibernate.ddl-auto = update ---- each time you run the program, will not form a new table, the data in the table is not empty, it will only update the table structure

spring.jpa.hibernate.ddl-auto = validate ---- check data field types and the database program run are the same, being given a different

Guess you like

Origin blog.51cto.com/5013162/2403086