Hibernate配置以及使用Hibernate实现用户添加

使用hibernate的三个准备,七个步骤。

三个准备

准备1:导入相关的包

导入Hibernate库(jar包,本人使用包版本是5.2.10) 

下载网站: http://sourceforge.net/projects/hibernate/files/

相关类包

打开myeclipse新建一个javaproject

导入所需要的jar包

准备2:添加配置文件

(1)选择org.hibernate.dialect.MySQLInnoDBDialect

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

(2)连接数据库驱动

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

(3)配置url

选择DB Browser

 

控制栏出现DB Browser 右键新建New一个,选择Driver template,

Connection URL修改成  :    jdbc:mysql://localhost:3306/hellohibernate,

 打开mysql输入密码,进行操作

mysql -uroot -p;

create database hello hibernate;

add JARs选中连接mysql数据库驱动文件夹选中mysql-connect or-java-5.1.6-bin  打开;

 

Driver classname选择come. mysql. jdbc. Driver

 

Test Driver 输入密码—>OK;

 

url创建完成

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate</property>  

(4)设置username

 <property name="hibernate.connection.username">root</property> 

(5)设置password

<property name="hibernate.connection.password">123456</property>

准备 3:添加实体类和映射文件(User.him.xml)

(1)创建new class 创建User.java 设置package为cn.hrbust.pojo

(2)在user.java中设置属性和方法

package cn.hrbust.pojo;
 
import java.sql.Date;
 
public class User {
    private int id;   //id唯一标识一个对象
    private String name;
    private String gender;
    int age;
    Date birthday;             //此处选中 import 'Date'( java.sql)导包  import java.sql.Date;
}
 
   

(3)设置实体对象的接口:选择Source,Generate Getters and Setters,Select All->OK, 产生读写接口。

 

(4)打开mysql 建表

use hellohibernate;

create table T_USER(id int not null auto_increment, name varchar(30), gender varchar(30), age int, birthday date,primary key(id));

desc T_USER;

(5)找到Simple.hbm复制,粘贴到跟实体类(O)在同一个包里,

 

 (6)选择Refactor,Rename,更改Simple.hbm.xml名字为User.hbm.xml.

(7) hibernate.cfg.xml此处更改为

 

(8)更改User.hbm.xml

​
<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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="cn.hrbust.pojo.User" table="T_USER">
        <id name="id"  column="id">   
            <generator class="native"/>  
        </id>
        <property name="name"/>
        <property name="gender"/>
        <property name="age" /> 
        <property name="birthday" />
     
    </class>
    
 
</hibernate-mapping>
 
​

七个步骤

七个步骤:1.Configuration->2.创建Session Fctory->3.打开Session->4.开始一个事务->5.持久化操作save/update/delete/get->6.提交事务->7.关闭Session

创建manageUser.java,导包选择,创建对象,捕获异常

package cn.hrbust.dao;

import java.sql.Date;

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

import cn.hrbust.pojo.User;

public class manageUser {
	 public static void main(String[] args){
		 //TODO Auto-generated method stub
			Configuration cfg = null;
			 SessionFactory sf = null;
			 Session session = null;
			 Transaction ts = null;
		 User u = new User();
		 u.setName("云可心");
		 u.setGender("女");
		 u.setAge(21);
		 u.setBirthday(Date.valueOf("2000-07-11"));
		 try {
			cfg = new Configuration().configure();
			sf = cfg.buildSessionFactory();
			 session = sf.openSession();
			  ts = session.beginTransaction();
			 session.save(u);
			 ts.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ts != null){
				ts.rollback();
			}
		}finally{
			session.close();
			sf.close();
		}
		 
		 
	 }
}

 查找,Hibernate配置以及使用Hibernate实现用户添加完成。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值