SpringBoot整合SpringDataJpa

作者: deepwinter 分类: 后端 发布时间: 2019-06-19 21:57 点击量: 98 次阅读

1. 添加依赖

<!-- mysql驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springdata jpa依赖 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2. 配置文件(src/main/resources/application.properties

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/springboot
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

3. Entity

使用jpa的时候,一般先开发entity。因为如果配置文件配置了spring.jpa.hibernate.ddl-auto = update的话,jpa可以直接根据entity创建表;

示例:

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

@Entity(name="target")  
public class Target {
    @Id 
    @GeneratedValue  
    private int tid;
    @Column(name="tname",length=20)  
    private String tname;

    public int getTid() {
        return tid;
    }
    public void setTid(int tid) {
        this.tid = tid;
    }
    public String getTname() {
        return tname;
    }
    public void setTname(String tname) {
        this.tname = tname;
    }
    @Override
    public String toString() {
        return "Target [tid=" + tid + ", tname=" + tname + "]";
    }
}

4. 持久层

import org.springframework.data.repository.CrudRepository;
import com.xujie.pojo.Target;

public interface TargetRepository extends CrudRepository<Target,Integer>{

}

5. service

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xujie.pojo.Target;
import com.xujie.repository.TargetRepository;
import com.xujie.service.TargetService;

@Service
public class TargetServiceimpl implements TargetService {

    // @Resource是根据名称注入 @Autowired是根据类型注入
    @Resource
    private TargetRepository targetRepository;

    //添加
    @Override
    public void save(Target target) {
        this.targetRepository.save(target);
    }

    //删除
    @Override
    public void delete(Integer id) {
        this.targetRepository.delete(id);
    }

    //查询
    @Override
    public List<Target> findAll(){
        return (List<Target>) this.targetRepository.findAll();
    }
}

6. controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xujie.pojo.Target;
import com.xujie.service.TargetService;

@RestController
public class TargetController {

    @Autowired
    private TargetService targetService;

    //添加
    @GetMapping("/save")
    public void save() {
        Target target = new Target();
        target.setTname("减肥");
        this.targetService.save(target);
    }

    //删除
    @GetMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        this.targetService.delete(id);
    }

    //查询所有
    @GetMapping("/findAll")
    public List<Target> findAll() {
        return this.targetService.findAll();

    }
}

转自:https://www.cnblogs.com/xujie09/p/8466520.html

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注