springboot fifth day --- SpringBoot use spring data jpa

introduction: 

For a long period of time, to achieve application data access layer has been a lot of trouble. Must write too much boilerplate code to perform simple queries and to perform paging and audit. Spring Data JPA aims to significantly improve data access layer by reducing the amount of work actually required. As a developer, you write repository interfaces, including custom finder method, Spring will automatically provide an implementation.

Spring Data JPA aims to significantly improve data access layer by reducing the amount of work actually required. As a developer, you write repository interfaces, including custom finder method, Spring will automatically provide an implementation.

 

 Code written:

1. Configuration dependent:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xhn</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <-! FreeMarker template engine ->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <! - Integration with mybatis springboot Integration ->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <! - test junit ->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <-! Redis cache ->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
        <! - The mapper interface mapper mapper mapping file in dependence on the next packet mapper required ->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.resources configuration application.properties:

#DB Configation
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

#JPA Configation
spring.jpa.database=MySQL
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

If the connection is not on the database, and that this change configuration

#DB Configration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc: MySQL: // localhost: 3306 / your own database to true & characterEncoding = = useUnicode UTF-8 & useSSL = false & serverTimezone = GMT? 
spring.datasource.username = root
spring.datasource.password=root

#JAPConfigration
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

3. Set the test controller:

package com.xhn.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
    @RequestMapping("/hello")
    public Map sayHell0(){
        Map map=new HashMap();
        map.put ( "json data 01", "I love the Java" );
         return the Map;
    }
}

Renderings:

 

 Into the title:

1. Create a database and add data:

2. Create a corresponding pojo objects:

package com.xhn.entity;

import javax.persistence.*;

// mark such as an entity class 
@Entity
 // tag database table corresponding to 
the @Table (name = "User" )
 public  class the User {
     // set increment primary key id 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;
   private String username;
   private String password;
   private String name;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public User(Integer id, String username, String password, String name) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
    }

    public User() {
        super();
    }
}

Note: Some notes on the corresponding need exceptionally good configuration

 

 

3. Create a corresponding controller interfaces:

package com.xhn.controller;

import com.xhn.dao.UserDao;
import com.xhn.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    // query all data 
    @RequestMapping ( "/ List" )
     public List <the User> getUserList () {
         return userDao.findAll ();
    }
}

4. dao corresponding layers:

package com.xhn.dao;


import com.xhn.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Integer> {

}

Test the code:

 

 

 

 

 Json widget corresponding to the main Bo: the JSON-handle_0.5.6.crx , the web can be self Baidu

 

Guess you like

Origin www.cnblogs.com/xinghaonan/p/11799854.html