• JPA - Hibernate


    JPA本身是一种规范,它的本质是一种ORM规范(不是ORM框架,因为JPA并未提供ORM实现,只是制定了规范)因为JPA是一种规范,所以,只是提供了一些相关的接口,但是接口并不能直接使用,JPA底层需要某种JPA实现,JPA现在就是Hibernate功能的一个子集

    Hibernate入门

    POM

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>com.lbgroupId>
    7. <artifactId>jpa-hibernateartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.target>1.8maven.compiler.target>
    11. <maven.compiler.source>1.8maven.compiler.source>
    12. properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>junitgroupId>
    16. <artifactId>junitartifactId>
    17. <version>4.13version>
    18. <scope>testscope>
    19. dependency>
    20. <dependency>
    21. <groupId>org.hibernategroupId>
    22. <artifactId>hibernate-entitymanagerartifactId>
    23. <version>5.4.32.Finalversion>
    24. dependency>
    25. <dependency>
    26. <groupId>mysqlgroupId>
    27. <artifactId>mysql-connector-javaartifactId>
    28. <version>5.1.46version>
    29. dependency>
    30. <dependency>
    31. <groupId>org.projectlombokgroupId>
    32. <artifactId>lombokartifactId>
    33. <version>1.16.10version>
    34. dependency>
    35. dependencies>
    36. <build>
    37. <plugins>
    38. <plugin>
    39. <groupId>org.apache.maven.pluginsgroupId>
    40. <artifactId>maven-compiler-pluginartifactId>
    41. <configuration>
    42. <source>1.8source>
    43. <target>1.8target>
    44. configuration>
    45. plugin>
    46. plugins>
    47. build>
    48. project>

    hibernate.cfg.xml

    1. hibernate-configuration PUBLIC
    2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    4. <hibernate-configuration>
    5. <session-factory>
    6. <property name="hbm2ddl.auto">updateproperty>
    7. <property name="show_sql">trueproperty>
    8. <property name="format_sql">trueproperty>
    9. <property name="dialect">org.hibernate.dialect.MySQL55Dialectproperty>
    10. <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
    11. <property name="connection.url">jdbc:mysql://localhost:3306/springdata_jpa?useUnicode=true&characterEncoding=UTF8&useSSL=falseproperty>
    12. <property name="connection.username">rootproperty>
    13. <property name="connection.password">rootproperty>
    14. <property name="connection.pool_size">5property>
    15. <property name="jdbc.fetch_size">50property>
    16. <property name="jdbc.batch_size">30property>
    17. <property name="current_session_context_class">threadproperty>
    18. <mapping class="com.lb.pojo.Customer" />
    19. session-factory>
    20. hibernate-configuration>

    注意dialect方言的选择, 会影响是否可以成功创建表,或者表的引擎的选择。

    POJO

    1. @Data
    2. @Entity //作为hibernate实体类
    3. @Table(name = "tb_customer") //映射的表名
    4. public class Customer {
    5. /**
    6. * @Id 声明主键的配置
    7. * @GeneratedValue 配置主键的生成策略
    8. * strategy
    9. * GenerationType.IDENTITY : 自增 mysql
    10. * 底层数据库必须支持自动增长。对Id自增
    11. * GenerationType.SEQUENCE : 序列,oracle
    12. * 定数据库必须支持序列
    13. * GenerationType.TABLE : JPA提供的一种机制,通过一张数据库表的形式帮助我们完成主键生成
    14. * GenerationType.AUTO : 由程序自动的帮助主键生成策略
    15. * @Column 配置属性和字段的映射关系
    16. * name : 数据库字段名称
    17. */
    18. @Id
    19. @GeneratedValue(strategy = GenerationType.IDENTITY)
    20. @Column(name = "cust_id")
    21. private Long custId;
    22. @Column(name = "cust_name")
    23. private String custName;
    24. @Column(name = "cust_address")
    25. private String custAddress;
    26. }

    测试

    1. public class HibernateTest {
    2. private SessionFactory sessionFactory;
    3. @Before
    4. public void init() {
    5. StandardServiceRegistry registry =
    6. new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    7. sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
    8. }
    9. @Test
    10. public void test01(){
    11. try(Session session = sessionFactory.openSession()){
    12. Transaction tx = session.beginTransaction();
    13. Customer customer = new Customer();
    14. customer.setCustName("李四");
    15. session.save(customer);
    16. tx.commit();
    17. }
    18. }
    19. }

    注意当Customer对应的表在数据库中不存在时,会执行创建表的操作,在进行save操作

    log:

    1. Hibernate:
    2. create table tb_customer (
    3. cust_id bigint not null auto_increment,
    4. cust_address varchar(255),
    5. cust_name varchar(255),
    6. primary key (cust_id)
    7. ) engine=InnoDB
    8. Hibernate:
    9. insert
    10. into
    11. tb_customer
    12. (cust_address, cust_name)
    13. values
    14. (?, ?)
    1. public class HibernateTest {
    2. private SessionFactory sessionFactory;
    3. @Before
    4. public void init() {
    5. StandardServiceRegistry registry =
    6. new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    7. sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
    8. }
    9. @Test
    10. public void test_save() {
    11. try (Session session = sessionFactory.openSession()) {
    12. Transaction tx = session.beginTransaction();
    13. Customer customer = new Customer();
    14. customer.setCustName("李四");
    15. session.save(customer);
    16. tx.commit();
    17. }
    18. }
    19. @Test
    20. public void test_find() {
    21. try (Session session = sessionFactory.openSession()) {
    22. Transaction tx = session.beginTransaction();
    23. Customer customer = session.find(Customer.class, 1L);
    24. System.out.println(customer);
    25. tx.commit();
    26. }
    27. }
    28. @Test
    29. public void test_load() {
    30. try (Session session = sessionFactory.openSession()) {
    31. Transaction tx = session.beginTransaction();
    32. //load懒加载,使用时才会执行查询
    33. Customer customer = session.load(Customer.class, 1L);
    34. System.out.println("========================");
    35. System.out.println(customer);
    36. tx.commit();
    37. }
    38. }
    39. @Test
    40. public void test_update() {
    41. try (Session session = sessionFactory.openSession()) {
    42. Transaction tx = session.beginTransaction();
    43. Customer customer = new Customer();
    44. customer.setCustId(1L);
    45. customer.setCustName("李四2");
    46. //有id修改,没有id插入,或者直接使用update
    47. session.saveOrUpdate(customer);
    48. tx.commit();
    49. }
    50. }
    51. @Test
    52. public void tes
  • 相关阅读:
    数据结构学习——第一章了解数据结构
    Higg FEM的一些小技巧
    量化交易全流程(一)
    c++笔记—— AutoBuffer类(opencv)
    【python】自动化工具Selenium与playwright去除webdriver检测
    自我成长自学必备网站,终生学习平台
    ElasticStack中的filebeat
    VR虚拟展厅——商业领域中不可或缺的工具
    工业相机和镜头参数和选型
    openjudge 1.5.2 财务管理
  • 原文地址:https://blog.csdn.net/qq_33753147/article/details/126933224