springboot . 配置jpa使用

1.maven引入配置

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Boot JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- MYSQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

2.prpperties配置

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ncdg
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jpa
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop

3.写entity.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


/**
 *
 * @author lili
 */
@Entity

public class User {
    @Id
    @GeneratedValue
     private long id;
    private String username;
    private String password;
    private String account;
    private String position;
    private String levels;
    private Long userId;

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the account
     */
    public String getAccount() {
        return account;
    }

    /**
     * @param account the account to set
     */
    public void setAccount(String account) {
        this.account = account;
    }

    /**
     * @return the position
     */
    public String getPosition() {
        return position;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(String position) {
        this.position = position;
    }

    /**
     * @return the levels
     */
    public String getLevels() {
        return levels;
    }

    /**
     * @param levels the levels to set
     */
    public void setLevels(String levels) {
        this.levels = levels;
    }

    /**
     * @return the userId
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    
    
};

4.写接口repository接口



import com.mycompany.ncdg.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 *
 * @author lili
 */
public interface UserRepository extends JpaRepository<User, Long> {

    @Query("from User u where u.username=:username")
    User findUser(@Param("username") String username);

}

5.写controller

import com.mycompany.ncdg.entity.User;
import com.mycompany.ncdg.repository.UserRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author lili
 */
@RestController
public class UserController {
    
    @Autowired
    UserRepository userRepository;
    @RequestMapping("/userall")
    public List<User> all(){
        return userRepository.findAll();
    };
}

6.启动springboot的主类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @author lili
 */
@SpringBootApplication
public class App {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SpringApplication.run(App.class, args);
    }
    
}

7.启动成功后 ,访问localhost:8080/userall   得到数据格式如下

[{"id":1,"username":"超级管理员","password":"123456","account":"admin","position":"1","levels":"1","userId":1}]

猜你喜欢

转载自my.oschina.net/u/2428630/blog/1612116