达梦数据库通过hibernate连接的基本程序配置

//hibernate.cfg.xml




	
		
		dm.jdbc.driver.DmDriver
		
		jdbc:dm://localhost:5236
		
		SYSDBA
		
		SYSDBA
		
		org.hibernate.dialect.DmDialect
		
	     
	

//User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package="dmhibernate">
 <class name="User" table="dmuser"> 
<id name="id" column="id"> </id> 
<property name="name" /> 
<property name="password" />
</class> </hibernate-mapping>
//User.java
ackage dmhibernate;

public class User {
	private int id;
	private String name;
	private String password;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

```c
//主程序

import java.util.List; 
import java.io.Serializable;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateTest {
	Configuration cfg = new Configuration().configure();
	SessionFactory factory = cfg.buildSessionFactory();
	Session session = null;

	//创建表
	public void createTable() {
		Configuration cfr = new Configuration().configure();
		SchemaExport export = new SchemaExport(cfr);
		export.create(true, true);// 创建表结构,第一个为True就是把DDL语句输出到控制台,第二个为True就是根据持久类和映射文件先执行删除再执行创建操作
	}

    //插入数据
	public void insertTable(User users) {

		try {
			session = factory.openSession();
			session.beginTransaction();
			session.save(users);
			session.beginTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}
	
    //根据主键id删除数据 
	public void deleteTable(Serializable id) {

		try {
			session = factory.openSession();
			session.beginTransaction();
			User news = (User) session.get(User.class, id);
			session.delete(news);
			session.beginTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}
	//更新所有字段数据
    public void updateTable(User users) {

		try {
			session = factory.openSession();
			session.beginTransaction();
			User news = (User) session.get(User.class, id);
			session.update(news);
			session.beginTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}
	
   //更新指定字段数据
    public void updateTableByColumn(int id, String name) {

		try {
			session = factory.openSession();
			session.beginTransaction();
			User users = (User) session.get(User.class, id);//根据id查找记录
			users.setName(name);//只修改指定列的记录
			session.save(users);
			session.beginTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}
	//查询
	public List queryTable(String sql) {
			session = factory.openSession();
			session.beginTransaction();
			Query query = session.createQuery(sql);//例如:sql="from User",User为类名
			return query.list(); 
	}

}

你可能感兴趣的:(达梦数据库通过hibernate连接的基本程序配置)