hibernate的第一个程序

#建表语句
create database hibernate;
use hibernate;
create table user(
id int primary key,
name varchar(30) not null default '',
des varchar(100) not null default '')charset utf8;

 User.hbn.xml数据表字段与javaBean的属性对应关系

<?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>
    <class name="com.henau.demo1.User" table="user">
        <id name="id" type="int">
            <column name="id"></column>
            <generator class="increment"></generator>
        </id>
        <property name="name" type="string">
            <column name="name"></column>
        </property>
        <property name="des" type="string">
            <column name="des" ></column>
        </property>
    </class>
</hibernate-mapping>

  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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
	     <property name="hibernate.connection.username">root</property>
	     <property name="hibernate.connection.password">111111</property>
	     
	     <!-- 配置数据库的方言,通过方言,让计算机知道连接的是哪种数据库 -->
	     <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
	     <!--在控制台上输出sql语句 -->
	     <property name="hibernate.show_sql">true</property>
	     <!--格式化sqlk语句 -->
	     <property name="hibernate.format_sql">false</property>
	     <!-- 加载映射文件 -->
	     <mapping resource="com/henau/demo1/User.hbn.xml"/>
    </session-factory>
</hibernate-configuration>

  javaBean类 User.java

package com.henau.demo1;

public class User {
	private int id;
	private String name;
	private String des;
	public User(){}
	public User(int id,String name,String des){
		this.id=id;
		this.name=name;
		this.des=des;
	}
	
	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 getDes() {
		return des;
	}
	public void setDes(String des) {
		this.des = des;
	}
}

 操作类Demo1.java向数据库中插入数据

package com.henau.demo1;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class Demo1 {
	static Configuration config;
	static{
		config=new Configuration();
		config.configure();//加载配置文件
		
	}
	public static void main(String[] args) {
		SessionFactory sf=config.buildSessionFactory();
		Session ss=sf.openSession();
		Transaction tx=ss.beginTransaction();//开启事务
		//实例化javaBean对象
		User user=new User();
		user.setName("张三");
		user.setDes("好人");
		ss.save(user);
		tx.commit();//提交事务
		ss.close();
	}
}

  

posted @ 2015-11-26 14:34  飘逸110  阅读(128)  评论(0编辑  收藏  举报