赞
踩
目录
此处的基本概念来至于:https://www.cnblogs.com/yunche/p/10279324.html
JPA(Java Persistence API)用于对象持久化的 API,是 Java EE 5.0 平台标准的 ORM 规范,使得应用程序以统一的方式访问持久层。
JDBC 也是一种规范和接口,不过 JDBC 是面向 SQL 的,使用起来比较繁琐。所以就有了 ORM 框架,建立了 Java 对象与数据库表之间的映射关系,可以通过直接操作对象来实现持久化,简化了操作的繁杂度。而 JPA 就是 ORM 框架的规范,值得一提的是 Hibernate 是符合 JPA 规范的,而 MyBatis 却不符合,因为 MyBatis 还是需要写 SQL 的。
下面来说明下具体使用:
此处的数据如下:
程序运行截图如下:
先贴下源码再说明:
程序结构如下:
ProductCategory.java
- package sqltabledemo.demo.dataobject;
-
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
-
- @Entity
- public class ProductCategory {
-
- @Id
- @GeneratedValue
- private Integer categoryId;
-
- private String categoryName;
-
- private Integer categoryType;
-
- public Integer getCategoryId() {
- return categoryId;
- }
-
- public void setCategoryId(Integer categoryId) {
- this.categoryId = categoryId;
- }
-
- public String getCategoryName() {
- return categoryName;
- }
-
- public void setCategoryName(String categoryName) {
- this.categoryName = categoryName;
- }
-
- public Integer getCategoryType() {
- return categoryType;
- }
-
- public void setCategoryType(Integer categoryType) {
- this.categoryType = categoryType;
- }
-
- @Override
- public String toString() {
- return "ProductCategory{" +
- "categoryId=" + categoryId +
- ", categoryName='" + categoryName + '\'' +
- ", categoryType=" + categoryType +
- '}';
- }
- }
ProductCategoryRepository.java
- package sqltabledemo.demo.repository;
-
- import org.springframework.data.jpa.repository.JpaRepository;
- import sqltabledemo.demo.dataobject.ProductCategory;
-
- public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
-
- }
-
application.yml
- spring:
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- username: root
- password: 123456
- url: jdbc:mysql://192.168.164.148/sell?characterEncoding=utf-8&useSSL=false
- jpa:
- show-sql: true
ProductCategoryRepositoryTest.java
- package sqltabledemo.demo.repository;
-
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import sqltabledemo.demo.dataobject.ProductCategory;
-
-
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class ProductCategoryRepositoryTest {
-
- @Autowired
- private ProductCategoryRepository repository;
-
- @Test
- public void findOneTest(){
-
- ProductCategory productCategory = repository.findOne(1);
- System.out.println(productCategory);
- }
- }
maven依赖如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.21.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>sqlTableDemo</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
-
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
下面来简单说明下:
ProductCategory这个类:
这里要对比下数据库:
从这里可以发现,java就像情人一样,把能做的都做好了。不向C++,要天天哄着,真是烦。
这里表名和字段名如果都采用骆驼峰命名法,即可完成映射,无需自己配置。
下面来演示下,手动进行映射!
如果我要把这个表映射成其他的表:
程序运行截图如下:
修改如下:
源码如下:
- package sqltabledemo.demo.dataobject;
-
- import javax.persistence.*;
-
- @Entity
- @Table(name = "seller_info")
- public class ProductCategory {
-
- @Id
- @GeneratedValue
- @Column(name = "id")
- private Integer categoryId;
-
- @Column(name = "username")
- private String categoryName;
-
- @Column(name = "openid")
- private Integer categoryType;
-
- public Integer getCategoryId() {
- return categoryId;
- }
-
- public void setCategoryId(Integer categoryId) {
- this.categoryId = categoryId;
- }
-
- public String getCategoryName() {
- return categoryName;
- }
-
- public void setCategoryName(String categoryName) {
- this.categoryName = categoryName;
- }
-
- public Integer getCategoryType() {
- return categoryType;
- }
-
- public void setCategoryType(Integer categoryType) {
- this.categoryType = categoryType;
- }
-
- @Override
- public String toString() {
- return "ProductCategory{" +
- "categoryId=" + categoryId +
- ", categoryName='" + categoryName + '\'' +
- ", categoryType=" + categoryType +
- '}';
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。