springboot(三) jpa的使用

x33g5p2x  于2021-12-17 转载在 其他  
字(4.0k)|赞(0)|评价(0)|浏览(575)

简介:

Spring Boot Jpa 是 Spring 基于 ORM 框架、Jpa 规范的基础上封装的一套 Jpa 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用 Spring Data Jpa 可以极大提高开发效率!

springboot集成springdata jpa:

       数据库表:

CREATE TABLE `teacher` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(34) COLLATE utf8_bin DEFAULT NULL,
  `age` int(34) DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

       新建项目:springboot-jpa,打开pom.xml文件加入相关依赖

<dependencies>
        <!--springdata jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
    </dependencies>

       配置application.properties,内容如下:

#数据库配置
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456

#jpa配置
spring.jpa.database=mysql
#是否显示sql
spring.jpa.show-sql=true
# create       每次运行该程序,没有表会新建表,表内有数据会清空
# create-drop  每次程序结束的时候会清空表
# update       每次运行程序,没有表会新建表,表内有数据不会清空,只会更新
# validate     运行程序会校验数据与数据库的字段类型是否相同,不同会报错
spring.jpa.hibernate.ddl-auto=update

#指定日期格式 yyyy-MM-dd HH:mm:ss
spring.jackson.date-format: yyyy-MM-dd HH:mm:ss
#mvc序列化时候时区选择
spring.jackson.time-zone: GMT+8

       实体类:Teacher.java

@Data
@Entity
//@Table(name="teacher")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    @Column(name = "name")
    private String name;
    
    private Integer age;
    
    private Date createTime;
}

      说明:@Data 注解是我们lombok插件的,让我们节省了get、set、无参构造方法等的编写

                @Entity  注解指名这是一个实体Bean

                @Table  注解,系统默认采用类名作为映射表的表名

                @GeneratedValue  主键的生成方式,这里是自增

                @Id  注解指声明主键

                @Column  注解定义了将成员属性映射到关系表中的哪一列和该列的结构信息

    注:不写@Table、@Column默认当前字段名为关系表的映射,更多注解详情请自行查询资料   

        编写TeacherReposity.java:

public interface TeacherReposity extends JpaRepository<Teacher, Integer> {
}

        编写TeacherService.java

public interface TeacherService {
    List<Teacher> selectAll();
    void add(Teacher teacher);
    void update(Teacher teacher);
    void delete(int id);
}

      编写TeacherServiceImpl.java

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherReposity reposity;

    @Override
    public List<Teacher> selectAll() {
        return reposity.findAll();
    }

    @Override
    public void add(Teacher teacher) {
        reposity.save(teacher);
    }

    @Override
    public void update(Teacher teacher) {
        reposity.saveAndFlush(teacher);
    }

    @Override
    public void delete(int id) {
        reposity.deleteById(id);
    }
}

       编写TeacherController.java

@RestController
public class TeacherController {
    @Resource
    private TeacherService service;

    @GetMapping("/list")
    public List<Teacher> list() {
        return service.selectAll();
    }

    @PostMapping("/add")
    public void add(Teacher teacher) {
        service.add(teacher);
    }

    @PutMapping("/update")
    public void update(Teacher teacher) {
        service.update(teacher);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        service.delete(id);
    }

}

源码地址https://gitee.com/xu0123/springboot2

idea开发工具继承lombok插件教程请看:
https://blog.csdn.net/xu12387/article/details/88331549,文章中有涉及具体使用教程

相关文章