Hibernate学习1 - 入门基本使用、一对多映射、多对一映射、多对多映射的使用

x33g5p2x  于2022-06-27 转载在 其他  
字(18.6k)|赞(0)|评价(0)|浏览(523)

概述 = Hibernate实现JPA规范

使用 === 注意测试懒加载时别DeBug模式 = 因为Debug会调用对象内懒加载的属性从而发送了SQL

基本使用 ===

pom.xml

<?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>

    <groupId>top.linruchang</groupId>
    <artifactId>HibernateDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.0.0.Alpha7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

    </dependencies>

    <build>
        <!--特别注意:如果你是把Article.hbm.xml这些映射文件放到src下的beanentity目录,则需要定义这里,像我一样放到resources也不需要设置这一段-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <!-- 以/jndi/name绑定到JNDI的SessionFactory实例 -->
    <!--<session-factory name="java:hibernate/SessionFactory">-->
    <session-factory >

        <!--     ===============数据源配置属性===============     -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?useSSL=false&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai</property>

        <!--     ===============C3P0连接池配置===============    -->
        <!-- 连接池中最少有多少个数据库连接 -->
        <property name="hibernate.c3p0.min_size">30</property>
        <!-- 连接池中允许的最大数据库连接数 -->
        <property name="hibernate.c3p0.max_size">30</property>
        <!-- 最大的PreparedStatement的数量 -->
        <property name="hibernate.c3p0.max_statements">10</property>
        <!--数据库连接不够时,每次增加的连接数-->
        <property name="hibernate.c3p0.acquire_increment">10</property>
        <!--数据库连接不用时,多长时间释放该连接,以秒为单位-->
        <property name="hibernate.c3p0.idle_test_period">10000</property>
        <!--连接处理最大时间,如果超过这个时间,会抛出异常,以豪秒为单位-->
        <property name="hibernate.c3p0.timeout">5000</property>

        <!--     ===============数据库方言===============    -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--     ===============功能===============    -->
        <!--打印SQL-->
        <property name="show_sql">true</property>
        <!--格式化SQL语句-->
        <property name="format_sql">true</property>
        <!--是否自动生成数据库-->
        <property name="hibernate.hbm2ddl.auto"/>

        <!--     ===============注册实体关系映射文件===============    -->
        <mapping resource="mapping/SysUser.hbm.xml"></mapping>
        <mapping resource="mapping/Article.hbm.xml"></mapping>
        <!--将此映射文件直接放到跟Bean的源码目录包下的写法-->
        <!--<mapping resource="top/linruchang/entity/SysUser.hbm.xml"></mapping>-->
        <!--<mapping resource="top/linruchang/entity/Article.hbm.xml"></mapping>-->

    </session-factory>

</hibernate-configuration>

Article.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.Article" table="article">
        <id name="id" type="java.lang.String">
            <column name="id" ></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="title" type="java.lang.String">
            <column name="title"></column>
        </property>

        <property name="content" type="java.lang.String">
            <column name="content"></column>
        </property>

        <property name="likeNum" type="java.lang.Integer">
            <column name="like_num"></column>
        </property>

    </class>
</hibernate-mapping>

Article.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Article extends BaseEntity {

    private static final long serialVersionUID = -4714261187453073302L;

    private SysUser sysUser;

    /** 文章标题 */
    private String title;

    /** 文章内容 */
    private String content;

    private String abbreviationContent;

    /** 文章被查看次数 */
    private Integer checkNum;

    /** 当前文章被点赞数 */
    private Integer likeNum;

    /** 当前文章不喜欢数 */
    private Integer notLikeNum;

    /** 文章是否违规:0未违规 1违规 - 违规不可显示 */
    private Integer isViolation;

    /** 创建者 - 冗余字段 - user表的nickName */
    private String createBy;

    /**列表文章的图片显示 - 如果指定则使用指定的图片(功能未做),没有指定则使用文章第一张图片,在没有则使用文章中有图标则使用目录分裂的图片*/
    private String imgUrl;

    /**文章内部图片*/
    private String imgUrls;

    /**文章内容类型:1富文本 2Markdown 3留空*/
    private String type;

    /**文章状态;-1违规 0草稿 1发布且公开  2发布且私密*/
    private String status;

}

MyTest.java

public class MyTest {

    public static void main(String[] args) {

        Configuration configure = new Configuration().configure();

        //获取sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        //获取数据库连接session
        Session session = sessionFactory.openSession();

        Article article = session.find(Article.class, "0f67d9f1953c7ab6036ece36cb5e1133");
        System.out.println(article);

        Article saveArticle = Article.builder()
                .checkNum(20)
                .likeNum(100)
                .content("你好")
                .title("爱你似懂非懂舒服的").build();
        saveArticle.setId(UUID.fastUUID().toString(true));
        Object save = session.save(saveArticle);
        System.out.println(save);
        // System.out.println(saveArticle.getId());
        // System.out.println(save);

        //特别注意:这里必须手动提交事务
        session.beginTransaction().commit();
        session.close();
    }

}

一对多映射

一个用户对应多篇文章


SysUser.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 2095940921263481761L;

    Set<Article> articles;

    /** 用户昵称 - 如果没有设置昵称直接使用账户名显示 */
    private String nickName;

    /** 头像图片地址 */
    private String headUrl;

    /** 账户 */
    private String loginName;

    /** 密码 */
    private String password;

    /** 手机号 */
    private String phoneNumber;

    /** 邮箱 */
    private String email;

    /** 性别 */
    private Integer gender;

    /** 个性签名 */
    private String personalMotto;

    /** 最近登录时间 - 格式 - yyyyMMddHHmmss */
    private String lastLoginTime;

    /** 登录状态:0未登录 1单设备登陆  2多设备登陆 */
    private Integer loginStatus;

    /** 账户禁用状态:0账户可使用  1账户不可使用(封号) */
    private Integer disabledStatus;

}


Article.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Article extends BaseEntity {

    private static final long serialVersionUID = -4714261187453073302L;

    private SysUser sysUser;

    private String userId;

    /** 文章标题 */
    private String title;

    /** 文章内容 */
    private String content;

    private String abbreviationContent;

    /** 文章被查看次数 */
    private Integer checkNum;

    /** 当前文章被点赞数 */
    private Integer likeNum;

    /** 当前文章不喜欢数 */
    private Integer notLikeNum;

    /** 文章是否违规:0未违规 1违规 - 违规不可显示 */
    private Integer isViolation;

    /** 创建者 - 冗余字段 - user表的nickName */
    private String createBy;

    /**列表文章的图片显示 - 如果指定则使用指定的图片(功能未做),没有指定则使用文章第一张图片,在没有则使用文章中有图标则使用目录分裂的图片*/
    private String imgUrl;

    /**文章内部图片*/
    private String imgUrls;

    /**文章内容类型:1富文本 2Markdown 3留空*/
    private String type;

    /**文章状态;-1违规 0草稿 1发布且公开  2发布且私密*/
    private String status;

}


SysUser.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.SysUser" table="sys_user">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="headUrl" type="java.lang.String">
            <column name="head_url" ></column>
        </property>

        <property name="loginName" type="java.lang.String">
            <column name="login_name" ></column>
        </property>

        <set name="articles" table="article">
            <!-- Article表的外键列名-->
            <key column="user_id"></key>
            <one-to-many class="top.linruchang.entity.Article"></one-to-many>
        </set>

    </class>
</hibernate-mapping>


Article.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.Article" table="article">
        <id name="id" type="java.lang.String">
            <column name="id" ></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>
        
        <!--逻辑外键,即sysUser表的主键ID-->
        <property name="userId" type="java.lang.String">
            <column name="user_id"></column>
        </property>

        <property name="title" type="java.lang.String">
            <column name="title"></column>
        </property>

        <property name="content" type="java.lang.String">
            <column name="content"></column>
        </property>

        <property name="likeNum" type="java.lang.Integer">
            <column name="like_num"></column>
        </property>

        <!--<property name="sysUser" type="top.linruchang.entity.SysUser">-->
        <!--    <column name="like_num"></column>-->
        <!--</property>-->

    </class>
</hibernate-mapping>


Article.hbm.xml

@Test
    public void test1() {

        Configuration configure = new Configuration().configure();

        // 获取sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        // 获取数据库连接session
        Session session = sessionFactory.openSession();
        SysUser sysUser = session.find(SysUser.class, "6d72c93aa292cf2ca2e789919a5e7bdc");
        System.out.println(sysUser);

    }

多对一映射


MyOrder.java

@Data
@ToString(callSuper = true)
public class MyOrder extends BaseEntity{

    MyUser myUser;

    String name;
    String money;

}


MyUser.java

@Data
@ToString(callSuper = true)
public class MyUser extends BaseEntity{

    String name;
}


MyOrder.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.MyOrder" table="my_order">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>

        <property name="money" type="java.lang.String">
            <column name="money"></column>
        </property>

        <many-to-one name="myUser" column="user_id" class="top.linruchang.entity.MyUser"></many-to-one>

    </class>
</hibernate-mapping>


MyUser.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.MyUser" table="my_user">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>

    </class>
</hibernate-mapping>

查询


MyTest.java

public class MyTest {

    @Test
    public void test3() {

        Configuration configure = new Configuration().configure();

        //获取sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        //获取数据库连接session
        Session session = sessionFactory.openSession();
        MyOrder myOrder = session.find(MyOrder.class, "9ac327eda05d11ebb7a6b42e99ea3e61");

        MyUser myUser = myOrder.getMyUser();
        System.out.println(myOrder);

    }
}

插入

@Test
    public void test4() {

        Configuration configure = new Configuration().configure();

        //获取sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        //获取数据库连接session
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        MyOrder myOrder = new MyOrder();
        myOrder.setId(UUID.fastUUID().toString(true));
        myOrder.setName("热狗");
        myOrder.setMoney("5");

        MyUser myUser = new MyUser();
        myUser.setId(UUID.fastUUID().toString(true));
        myUser.setName("立法的方式地方=====");
        myOrder.setMyUser(myUser);
        
        //先插入外键表的数据
        Object insertUserId = session.save(myUser);
        System.out.println(insertUserId);
        //随后在插入主表的数据
        Object insertOrderId = session.save(myOrder);
        System.out.println(insertOrderId);

        transaction.commit();
        session.close();

    }

多对多映射 == 需要中间表


MyOrder2.java

@Getter
@Setter
public class MyOrder2 extends BaseEntity{

    String name;
    String money;

    Set<MyUser2> myUser2;

    @Override
    public String toString() {
        return "MyOrder2{" +
                "id='" + getId() + '\'' +
                "name='" + name + '\'' +
                ", money='" + money + '\'' +
                '}';
    }
}


MyUser2.java

@Getter
@Setter
public class MyUser2 extends BaseEntity{

    String name;

    Set<MyOrder2> myOrder2;

    @Override
    public String toString() {
        return "MyUser2{" +
                "id='" + getId() + '\'' +
                "name='" + name + '\'' +
                '}';
    }
}


UserOrderRel.java

@Getter
@Setter
public class UserOrderRel extends BaseEntity{
    String myUserId;
    String myOrderId;
}


MyOrder2.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.MyOrder2" table="my_order">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>

        <property name="money" type="java.lang.String">
            <column name="money"></column>
        </property>

        <set name="myUser2" table="user_order_rel">
            <key column="my_order_id" ></key>
            <many-to-many column="my_user_id" class="top.linruchang.entity.MyUser2"></many-to-many>
        </set>


    </class>
</hibernate-mapping>


MyUser2.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.MyUser2" table="my_user">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>

        <set name="myOrder2" table="user_order_rel">
            <key column="my_user_id" ></key>
            <many-to-many column="my_order_id" class="top.linruchang.entity.MyOrder2"></many-to-many>
        </set>

    </class>
</hibernate-mapping>


UserOrderRel.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="top.linruchang.entity.UserOrderRel" table="user_order_rel">
        <id name="id" type="java.lang.String">
            <column name="id"></column>
            <!--插入时,如果你没有设置ID,会帮你自动添加ID-->
            <generator class="uuid"></generator>
        </id>

        <property name="myUserId" type="java.lang.String">
            <column name="my_user_id"></column>
        </property>

        <property name="myOrderId" type="java.lang.String">
            <column name="my_order_id"></column>
        </property>

    </class>
</hibernate-mapping>


MyTest.java

public class MyTest {


    @Test
    public void test5() {

        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        MyOrder2 myOrder2 = session.find(MyOrder2.class, "4028b88178e60a910178e60a94c90001");
        System.out.println(myOrder2);
        System.out.println(myOrder2.getMyUser2());
        session.close();

    }


}

相关文章