您的位置:首页 > 其它

关于hibernate的详解,从搭建环境到客户管理系统第一

2017-04-28 15:11 555 查看
hibernate的搭建过程

1.1.导包
2.创建数据表
3.书写orm元数据(对象与表的映射配置文件)


<!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.hibernate.test.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
4.书写主配置文件
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">wdl03707552882</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/hibernate/test/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.建立测试类进行测试
public class Demo {
@Test
public void fun1(){
Configuration conf=new Configuration().configure();
SessionFactory sessionfactory=conf.buildSessionFactory();
Session session = sessionfactory.openSession();
Transaction tx=session.beginTransaction();
//--------------------------------------
Customer c=new Customer();
c.setCust_name("张三");

session.save(c);

//-------------------------------------------
tx.commit();
session.close();
sessionfactory.close();
}
}
6.配置文件详解:(.hbm.xml)
1)orm元数据详解:
<id name="cust_id" column="cust_id" >
<!-- generator:主键生成策略(明天讲) -->
<generator class="native"></generator>
</id>
<!-- property元素:除id之外的普通属性映射
name: 填写属性名
column(可选): 填写列名
type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
每个类型有三种填法: java类型|hibernate类型|数据库类型
not-null(可选):配置该属性(列)是否不能为空. 默认值:false
length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
-->
2)配置文件详解:(.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>
<!-- 
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">wdl03707552882</property>
<!-- 不同的数据库,sql语法有所区别,指定的方言可以让hibernate -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>

<!-- 配置是否自动更新表   update 为自动更新,validate为如果不存在表或者某个属性将会报错 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置映射文件 -->

<mapping resource="com/hibernate/test/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>



7
。hibernateAPI详解


1)Configuration
a|创建Configuration
Configuraion conf=new Configuration();
b加载配置
conf.configurer();
c.加载orm元数据(扩展,不做了解)
//读取制定orm指定元数据(扩展)这种方法比较老.所以不会在使用
//因为主配置文件已经加载.hbm.xml文件
//conf.addResource(resourceName);
2).创建sessionFactory
SessionFactory  factory=conf.buildSessionFactory();
sesionfactoyr应用
//5.获取session对象
//打开一个新的session对象
Session session=factory.openSession();
//获得一个与线程绑定的session对象
factory.getCurrentSession();

3)session对象
前提代码不变
// 1.创建。调用空参构造
Configuration conf = new Configuration();
// 读取制定配置文件,空参方法,加载src下的hibernate.cfg.xml文件
conf.configure();

// 读取制定orm指定元数据(扩展)这种方法比较老.所以不会在使用
// 因为主配置文件已经加载.hbm.xml文件
// conf.addResource(resourceName);

// 4.根据配置信息,创建SessionFactory对象
SessionFactory factory = conf.buildSessionFactory();
// 5.获取session对象
// 打开一个新的session对象
Session session = factory.openSession();
// 获得一个与线程绑定的session对象
// factory.getCurrentSession();
// session获取操作事务的Transaction对象
// 获取操作失误的tx对象
// Transaction tx=session.getTransaction();

// 开启事务并获取操作失误的tx对象(建议使用)
Transaction tx1 = session.beginTransaction();
//----------------------------------------

执行代码

//----------------------------------------
tx1.commit();// 提交事务
// tx1.rollback();//回滚事务
session.close();
factory.close();
3-1)增加数据
Customer c = new Customer();
c.setCust_name("物联网工程");
session.save(c);
// -----------------------------------------------

3-2)修改数据
// 由于我们设置的id的属性为Long类型,所以我们需要在id值后边加个l
// 我们要进行修改,我们先获得在修改,最后再更新
// 此时打印的数据库语句有两个一个是获取对象。另一个是更新对象
Customer customer = session.get(Customer.class, 1l);
customer.setCust_name("黑马");
session.update(customer);

3-1)删除数据
// -----------------------------------------------
// 由于我们设置的id的属性为Long类型,所以我们需要在id值后边加个l
// 我们要删除数据,分为两步第一步是获取对象,第二部是删除对象
// 此时打印的数据库语句有两个一个是获取对象。另一个是删除对象
Customer customer = session.get(Customer.class, 1l);
session.delete(customer);

3-1)查询数据
Customer customer = session.get(Customer.class, 1l);
System.out.println(customer);

Transaction对象使用
1)回去Transaction有两种方式
// 获取操作失误的tx对象
// Transaction tx=session.getTransaction();
// 开启事务并获取操作失误的tx对象(建议使用)
Transaction tx1 = session.beginTransaction();

tx1.commit()//提交事务




7。建立一个Hibenate类用于管理Session

public class HibnerateUtils {
private static SessionFactory factory;
static {
Configuration con = new Configuration();
con.configure();
factory = con.buildSessionFactory();
}

// 获得session对象===〉全新session
public static Session opensession() {
Configuration con = new Configuration();
con.configure();
Session session = factory.openSession();
return session;

}

// 获得session对象===〉获取与线程帮顶的session
public static Session getCurrentSession() {
Session session = factory.getCurrentSession();
return session;
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐