一、主体框架
二、代码展示
①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);
}
}
三、运行结果展示