作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA

Custom Tab


添加相关依赖

添加spring-boot-starter-jdbc依赖:

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


添加mysql连接类和连接池类:

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


配置数据源,在application.properties文件配置:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8    
    username: root    password: 123456

  jpa:
    hibernate:
      ddl-auto: update  # 第一次简表create  后面用update
    show-sql: true


注意,如果通过jpa在数据库中建表,将jpa.hibernate,ddl-auto改为create,建完表之后,要改为update,要不然每次重启工程会删除表并新建。

创建实体类

通过@Entity 表明是一个映射的实体类, @Id表明id, @GeneratedValue 字段自动生成

@Entitypublic class Account {
    @Id
    @GeneratedValue
    private int id ;    
    private String name ;    
    private double money;

...  省略getter setter
}


Dao层

数据访问层,通过编写一个继承自 JpaRepository 的接口就能完成数据访问,其中包含了几本的单表查询的方法,非常的方便。值得注意的是,这个Account 对象名,而不是具体的表名,另外Interger是主键的类型,一般为Integer或者Long

public interface AccountDao  extends JpaRepository<Account,Integer> {}


Web层

在这个栗子中我简略了service层的书写,在实际开发中,不可省略。新写一个controller,写几个restful api来测试数据的访问。

@RestController
@RequestMapping("/account")public class AccountController {

    @Autowired
    AccountDao accountDao;

    @RequestMapping(value = "/list", method = RequestMethod.GET)    
    public List<Account> getAccounts() {        
    return accountDao.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)   
     public Account getAccountById(@PathVariable("id") int id) {        
     return accountDao.findOne(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)    
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", 
    required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        Account account1 = accountDao.saveAndFlush(account);        
        return account1.toString();

    }

    @RequestMapping(value = "", method = RequestMethod.POST)    
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        Account account1 = accountDao.save(account);        
        return account1.toString();

    }


}


通过postman请求测试,代码已经全部通过测试。

源码下载:https://github.com/forezp/SpringBootLearning

参考资料

accessing-data-jpa

SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程

SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql


SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql

SpringBoot非官方教程 | 第六篇:springboot整合mybatis

SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

SpringBoot非官方教程 | 第八篇:springboot整合mongodb

SpringBoot非官方教程 | 第九篇: springboot整合Redis

SpringBoot非官方教程 | 第十篇: 用spring Restdocs创建API文档

SpringBoot非官方教程 | 第十一篇:springboot集成swagger2,构建优雅的Restful API

SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

SpringBoot非官方教程 | 第十三篇:springboot集成spring cache

SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列

SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ

SpringBoot非官方教程 | 第十六篇:用restTemplate消费服务

SpringBoot非官方教程 | 第十七篇:上传文件

SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)

SpringBoot非官方教程 | 第十九篇: 验证表单信息

SpringBoot非官方教程 | 第二十篇: 处理表单提交

SpringBoot非官方教程 | 第二十一篇: springboot集成JMS

SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

SpringBoot非官方教程 | 第二十三篇: 异步方法

SpringBoot非官方教程 | 第二十四篇: springboot整合docker

SpringBoot非官方教程 | 第二十五篇:2小时学会springboot

SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

优秀文章推荐:




转载自:http://blog.csdn.net/forezp/article/details/70545038

Home