hibernate之简单增删改查

一、主体框架

在这里插入图片描述

二、代码展示

①News实体类

package com.allen.entity;

import java.io.Serializable;

public class News implements Serializable {
    private Integer id;
    private String title;
    private String content;

    public News() {
    }

    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

②hibernate.cfg.xml配置文件

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

        <!-- 配置连接数据库的基本信息 -->
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///hibernate</property>

        <!-- hibernate 所使用的数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>

        <!-- 执行操作时是否在控制台打印 SQL -->
        <property name="show_sql">true</property>

        <!-- 表生成策略 -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 指定关联的 .hbm.xml 文件 -->
        <mapping resource="News.hbm.xml" />
        <mapping class="com.allen.entity.User"/>
        <mapping class="com.allen.entity.Classroom"/>
        <mapping class="com.allen.entity.Student"/>
    </session-factory>
</hibernate-configuration>

③News.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 package="com.allen.entity">
    <class name="News" table="NEWS" dynamic-insert="true">
        <!--dynamic-insert="true" 时表示插入对象时空的字段不插入 -->
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
            <!-- 指定主键的生成策略, native: 使用数据库本地方式 -->
        </id>
        <property name="title" not-null="true"
                  length="50" type="java.lang.String" column="TITLE">
        </property>
        <property name="content">
            <column name="CONTENT" sql-type="text"></column>
            <!--数据库改字段的名字和类型-->
        </property>
    </class>
</hibernate-mapping>

④AppTest测试类代码

package com.allen;

import static org.junit.Assert.assertTrue;

import com.allen.entity.News;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AppTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init() {
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }

    @After
    public void destroy() {
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    /**
     * 增加
     */
    @Test
    public void test01(){
        News news = new News("标题2", "正文2");
        session.save(news);
    }

    /**
     * 删除
     */
    @Test
    public void test02(){
        News news = session.get(News.class, 2);
        session.delete(news);
        System.out.println(news);
    }
    /**
     * 修改
     */
    @Test
    public void test03(){
        News news = new News("标题22", "正文22");
        news.setId(2);
        session.saveOrUpdate(news);
    }

    /**
     * 查询
     */
    @Test
    public void test04(){
        News news = session.get(News.class, 1);
        System.out.println(news);
    }
}

三、运行结果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

faramita_of_mine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值