(初学Hibernate)Hibernate对数据库的增删改查)

(初学Hibernate)Hibernate对数据库的增删改查)Hibernate所需jar包可以在Hibernate官网下载
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
最后倒计时结束后下载框才会提示出来

现在开始放代码了

Hibernate配置文件hibernate.cfg.xml

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

<hibernate-configuration>
	<session-factory>
		<!-- 设置字符编码集 -->
		<property name="connection.useUnicode">true</property>
		<property name="connection.characterEncoding">UTF-8</property>
		<!-- 方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 设置地址 -->
		<property name="connection.url">jdbc:mysql://localhost:3306/task</property>
		<!-- 用户名 -->
		<property name="connection.username">root</property>
		<!-- 密码 -->
		<property name="connection.password">root</property>
		<!-- 驱动 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 显示sql语句 -->
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<!-- 关联映射文件 -->
		<mapping resource="com/entity/News.hbm.xml" />

	</session-factory>
</hibernate-configuration>

这里需要注意关联映射文件以及设置的地址中对应的数据库名称

创建实体类News.java

package com.entity;
public class News {
	private int id;
	private String title;
	private String content;
	public int getId() {
		return id;
	}
	public void setId(int 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;
	}
	
}

配置映射文件News.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping>
	<class name="com.entity.News" table="news">
		<id name="id" column="id" type="integer">
			<generator class="native"></generator>
		</id>
		<property name="title" column="title" type="string" />
		<property name="content" column="content" type="string" />
	</class>
</hibernate-mapping>

类测试NewsDao

package com.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.entity.News;

public class NewsDao {
	static Configuration cfg = null; // 读取配置文件的对象
	static SessionFactory sf = null; // 创建会话工厂的对象
	static Session session = null;// 打开session
	static Transaction t = null; // 开启事务

	static {
		cfg = new Configuration().configure();
		StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
		sf = cfg.buildSessionFactory(ssr);
		session = sf.openSession();
		t = session.beginTransaction();
	}

	// 添加数据
	public static void insert(News n) {

			try {
				session.save(n);
				System.out.println("insert datas:success!");
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println("insert datas:error!");
			} 
			end();
	}

	// 修改数据
	public static void update(News n){

		try {
			session.update(n);
			System.out.println("update datas:success!");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("update datas:error!");
		} 
		end();
		
	}
	
	//根据id删除数据
	public static void delete(News n){

		try {
			session.delete(n);
			System.out.println("delete datas:success!");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("delete datas:error!");
		} 
		end();
	}
	
	
	//根据id查询数据
	public static News select(int id){
		News n;
		try {
			n = (News) session.get(News.class, id);
			return n;
		} catch (Exception e) {
			
			System.out.println("delete datas:error!");
			return n = null;
		} finally {
			end();
		}
		
		
	}
	
	// 关闭资源
	public static void end(){
		t.commit();
		sf.close();
	}

}

新人第一次发布,不好的地方希望谅解

猜你喜欢

转载自blog.csdn.net/baozi141990/article/details/85000266