码迷,mamicode.com
首页 > 其他好文 > 详细

spingboot集成jpa

时间:2018-02-11 10:41:44      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:rgs   jdb   ppi   alt   tco   cal   ide   beans   etc   

1. pom.xml中添加依赖

    <!-- jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

2. application.propertis添加配置

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

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

3. 实体类User.java创建

package com.king.entity;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="m_user")
public class User{
    
    @Id
    @Column(name="ID")
    private String id;
    @Column(name="user_code")
    private String code;
    @Column(name="user_name")
    private String username;
    @Column(name="user_pwd")
    private String password;
    @Column(name="update_tmsp")
    private Date updateTime;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    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 Date getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    
    @Override
    public String toString() {
        return "User [id=" + id + ", code=" + code + ", username=" + username
                + ", password=" + password + ", updateTime=" + updateTime + "]";
    }
}

4. dao层创建UserDao.java继承JpaRepository

package com.king.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.king.entity.User;

public interface UserDao extends JpaRepository<User, String>{

}

5. service层创建查询方法,并装备dao中的方法

UserService接口

package com.king.service;

import java.util.List;
import com.king.entity.User;

public interface UserService {
    
    public User findOne(String id);
    public List<User> findAll();    
}

UserService实现类

package com.king.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.king.dao.UserDao;
import com.king.entity.User;
import com.king.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;
    
    @Override
    public User findOne(String id) {
        return userDao.findOne(id);
    }
    
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}

6. controller层查询

package com.king.controller;


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;

import com.king.entity.User;
import com.king.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/queryUser")
    public User getUser(String id){
        return userService.findOne(id);
    }

    @RequestMapping("/queryAll")
    public List<User> getAll(){
        return userService.findAll();
    }

}

7. App中配置扫描

package com.king.app;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@ComponentScan(basePackages={"com.king.controller","com.king.service"})
@EnableJpaRepositories(basePackages="com.king.dao")
@EntityScan(basePackages="com.king.entity")
@EnableAutoConfiguration
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }    
}

到此,所有的配置完成。

测试1:根据id查询单个对象,返回json对象

技术分享图片

测试2: 测试查询所有对象,返回json数组

技术分享图片

 

spingboot集成jpa

标签:rgs   jdb   ppi   alt   tco   cal   ide   beans   etc   

原文地址:https://www.cnblogs.com/30go/p/8440343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!