26, springboot-- integration JPA

1. Create a project

 

 

 

 

 

2, the relevant configuration of the data source and JPA in application.yml

the Spring: 
  the DataSource: 
    url: jdbc: MySQL: // localhost: 3306 / springbootjpa serverTimezone = GMT? 
    username: root 
    password: 123 
    Driver-class-name: com.mysql.cj.jdbc.Driver 
  JPA: 
    Hibernate: 
      DDL-Auto: update # update or create the table 
    show-sql: true # sql console display

3, good writing and mapping an entity class a good table

Import the javax.persistence *. ; 

/ ** 
 * Use JPA annotations configuring the mapping 
 * / 

@Entity          // tell this is a JPA entity class (class maps and data sheets) 
@Table (name = "tbl_user")        // specified and which data correspondence table; if the attribute name is omitted, the default is the first letter of the name of the table class name lowercase
 public  class the User { @Id      

    // specify that this is the primary key
     @GeneratedValue (strategy = GenerationType.IDENTITY)      // increment policy
     Private Integer ID ; the @Column (name

     = "last_name", length = 50)      // specify that this is a list of
     private String lastName; the @Column                      

    // name attribute property name is omitted, the default
     private String email;

    public Integer getId() {
        return id;
    }
........
}

4, write Dao interface entity classes corresponding to the data table (the Repository)

 

 

 CrudRepository basic CRUD functions; PagingAndSortingRepository there are paging and sorting;

The inheritance graph, we only need to write the repository will either have to realize JpaRepository CRUD, there are paging and sorting functions

Package com.atguigu.springboot.repository; 

Import com.atguigu.springboot.entity.User;
 Import org.springframework.data.jpa.repository.JpaRepository; 

// inherited JpaRepository to perform operations on the database 
public  interface UserRepository the extends JpaRepository <the User , Integer> { 


}

5, when we first started the project, let jpa automatically help us build the configuration table

  Before starting the project has not table:

  

 

   Table is automatically created after successful startup items:

 

   

 

 6, create a controller calling UserRepository (here only to demonstrate the Repository feature, so do not create a service layer)

 

 Note springboot2.x in Repository of findOne methods and springboot1.x not the same, Baidu can understand

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/{id}")
    public Optional<User> getUser(@PathVariable("id") Integer id){
        User u = new User();
        u.setId(id);
        Example<User> example = Example.of(u);
        Optional<User> one = userRepository.findOne(example);
        return one;
    }

    @GetMapping("/user")
    public User insertUser(User user){
        User u = userRepository.save(user);
        return u;
    }
}

Insert the test:

 

 Query test:

Guess you like

Origin www.cnblogs.com/lyh233/p/12553664.html