Java笔记-使用jpa连接mysql数据库
目錄
?
?
基本概念
代碼與實例
?
基本概念
此處的基本概念來至于:https://www.cnblogs.com/yunche/p/10279324.html
?
JPA(Java Persistence API)用于對象持久化的 API,是 Java EE 5.0 平臺標準的 ORM 規(guī)范,使得應用程序以統(tǒng)一的方式訪問持久層。
JDBC 也是一種規(guī)范和接口,不過 JDBC 是面向 SQL 的,使用起來比較繁瑣。所以就有了 ORM 框架,建立了 Java 對象與數(shù)據(jù)庫表之間的映射關系,可以通過直接操作對象來實現(xiàn)持久化,簡化了操作的繁雜度。而 JPA 就是 ORM 框架的規(guī)范,值得一提的是 Hibernate 是符合 JPA 規(guī)范的,而 MyBatis 卻不符合,因為 MyBatis 還是需要寫 SQL 的。
?
?
?
代碼與實例
下面來說明下具體使用:
此處的數(shù)據(jù)如下:
程序運行截圖如下:
先貼下源碼再說明:
程序結(jié)構(gòu)如下:
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@GeneratedValueprivate 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;}@Overridepublic 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.Driverusername: rootpassword: 123456url: jdbc:mysql://192.168.164.148/sell?characterEncoding=utf-8&useSSL=falsejpa:show-sql: trueProductCategoryRepositoryTest.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 {@Autowiredprivate ProductCategoryRepository repository;@Testpublic 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這個類:
這里要對比下數(shù)據(jù)庫:
從這里可以發(fā)現(xiàn),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;}@Overridepublic String toString() {return "ProductCategory{" +"categoryId=" + categoryId +", categoryName='" + categoryName + '\'' +", categoryType=" + categoryType +'}';} }?
總結(jié)
以上是生活随笔為你收集整理的Java笔记-使用jpa连接mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于CSDN官方对博文点赞漏洞的处理(C
- 下一篇: SQL工作笔记-达梦存储过程及时间触发器