当前位置:   article > 正文

Java笔记-使用jpa连接mysql数据库_jpa mysql

jpa mysql

目录

 

 

基本概念

代码与实例


 

基本概念

此处的基本概念来至于: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

  1. package sqltabledemo.demo.dataobject;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. @Entity
  7. public class ProductCategory {
  8. @Id
  9. @GeneratedValue
  10. private Integer categoryId;
  11. private String categoryName;
  12. private Integer categoryType;
  13. public Integer getCategoryId() {
  14. return categoryId;
  15. }
  16. public void setCategoryId(Integer categoryId) {
  17. this.categoryId = categoryId;
  18. }
  19. public String getCategoryName() {
  20. return categoryName;
  21. }
  22. public void setCategoryName(String categoryName) {
  23. this.categoryName = categoryName;
  24. }
  25. public Integer getCategoryType() {
  26. return categoryType;
  27. }
  28. public void setCategoryType(Integer categoryType) {
  29. this.categoryType = categoryType;
  30. }
  31. @Override
  32. public String toString() {
  33. return "ProductCategory{" +
  34. "categoryId=" + categoryId +
  35. ", categoryName='" + categoryName + '\'' +
  36. ", categoryType=" + categoryType +
  37. '}';
  38. }
  39. }

ProductCategoryRepository.java

  1. package sqltabledemo.demo.repository;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import sqltabledemo.demo.dataobject.ProductCategory;
  4. public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
  5. }

application.yml

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. username: root
  5. password: 123456
  6. url: jdbc:mysql://192.168.164.148/sell?characterEncoding=utf-8&useSSL=false
  7. jpa:
  8. show-sql: true

ProductCategoryRepositoryTest.java

  1. package sqltabledemo.demo.repository;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import sqltabledemo.demo.dataobject.ProductCategory;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class ProductCategoryRepositoryTest {
  11. @Autowired
  12. private ProductCategoryRepository repository;
  13. @Test
  14. public void findOneTest(){
  15. ProductCategory productCategory = repository.findOne(1);
  16. System.out.println(productCategory);
  17. }
  18. }

maven依赖如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.21.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>sqlTableDemo</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-data-jpa</artifactId>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>

下面来简单说明下:

ProductCategory这个类:

这里要对比下数据库:

从这里可以发现,java就像情人一样,把能做的都做好了。不向C++,要天天哄着,真是烦。

这里表名和字段名如果都采用骆驼峰命名法,即可完成映射,无需自己配置。

下面来演示下,手动进行映射!

如果我要把这个表映射成其他的表:

程序运行截图如下:

修改如下:

源码如下:

  1. package sqltabledemo.demo.dataobject;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name = "seller_info")
  5. public class ProductCategory {
  6. @Id
  7. @GeneratedValue
  8. @Column(name = "id")
  9. private Integer categoryId;
  10. @Column(name = "username")
  11. private String categoryName;
  12. @Column(name = "openid")
  13. private Integer categoryType;
  14. public Integer getCategoryId() {
  15. return categoryId;
  16. }
  17. public void setCategoryId(Integer categoryId) {
  18. this.categoryId = categoryId;
  19. }
  20. public String getCategoryName() {
  21. return categoryName;
  22. }
  23. public void setCategoryName(String categoryName) {
  24. this.categoryName = categoryName;
  25. }
  26. public Integer getCategoryType() {
  27. return categoryType;
  28. }
  29. public void setCategoryType(Integer categoryType) {
  30. this.categoryType = categoryType;
  31. }
  32. @Override
  33. public String toString() {
  34. return "ProductCategory{" +
  35. "categoryId=" + categoryId +
  36. ", categoryName='" + categoryName + '\'' +
  37. ", categoryType=" + categoryType +
  38. '}';
  39. }
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/954829
推荐阅读
相关标签
  

闽ICP备14008679号