java · 2024-08-31 0

jakarta 使用

一、jakarta 使用

1.pom

<dependencies>
    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        <version>2.2.3</version>
    </dependency>

    <dependency>
        <groupId>jakarta.transaction</groupId>
        <artifactId>jakarta.transaction-api</artifactId>
        <version>1.3.3</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.10.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.29</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.28</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

2.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

        <class>com.example.entity.User</class>

        <properties>
            <!-- connection 配置见参数 AvailableSettings.java -->
            <!--<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>-->
            <!--<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/temp"/>-->
            <!--<property name="hibernate.connection.username" value="root"/>-->
            <!--<property name="hibernate.connection.password" value="123456"/>-->

            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/temp"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="123456"/>

            <!--<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>-->
            <!--<property name="jakarta.persistence.jdbc.user" value="root"/>-->
            <!--<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/temp"/>-->
            <!--<property name="jakarta.persistence.jdbc.password" value="123456"/>-->

            <!-- pool 配置参数见 DriverManagerConnectionProviderImpl.java -->
            <property name="hibernate.connection.initial_pool_size" value="5"/>
            <property name="hibernate.connection.min_pool_size" value="5"/>
            <property name="hibernate.connection.pool_size" value="10"/>
            <property name="hibernate.connection.pool_validation_interval" value="20"/>

            <!-- 应用见 JdbcServicesImpl.java,配置参数见 AvailableSettings.java -->
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>

            <property name="hibernate.cache.use_second_level_cache" value="false"/>

            <!-- 应用见 ,配置参数见 AvailableSettings.java -->
            <!-- create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失 -->
            <!-- create-drop:启动hibernate时,自动创建表,程序关闭时,自动把相应的表都删除。所以程序结束时,表和数据也不会再存在 -->
            <!-- update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列 -->
            <!-- validate:加载hibernate时,验证创建数据库表结构 -->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>

    </persistence-unit>

</persistence>

3.java

@Setter
@Getter
@Entity(name = "User")
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
}

4.测试类

@Test
public void test1() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    try {
        User user = new User();
        user.setName("zhangsan");
        entityManager.persist(user);
        transaction.commit();
        System.out.println("- - - success - - -");
    } catch (Exception e) {
        transaction.rollback();
        System.err.println("- - - fail - - -");
    } finally {
        entityManager.close();
        entityManagerFactory.close();
    }
}