JAVAEE学习日志(3) SSH框架 Hibernate使用

使用数据库:Oracle,平台Eclipse 4.14 jdk 1.8

指定表格所属的schema(数据库根据schema进行管理)

1.xxx.hbm.xml (hibernate mapping xml)
< hibernate-mapping package=“hibernate>
< class name=“Bean类” table=“table名” schema=”指定的schema”>
😗 ************ 😗 < !-- 数据库表格对应实体的字段-- >
< /class>
< /hibernate-mapping >
2.hibernate.cfg.xml(configure xml)
< property name=“hibernate.default-schema”>< /property>
3.@Table(name=“table”,schema=“指定的schema”)

hibernate反向创建表

xxxxx.cfg.xml
< property name=“hbm2ddl.auto">XXXX< /property>
XXXX{create , update}
create:覆盖新表
update:若无,创建。若有,对旧表进行更新

< property name=“hibernate.show_sql”>true< /property>

于日志中显示执行的h/s ql语句

cfg.xml的< mapping resource=”xxxx“/ >

指定用于映射数据库表格的文件

  1. hbm.xml
    < mapping resource=“xxx.hbm.xml”>
    < hibernate-mapping package=“hibernate>
    < class name=“Bean类” table=“table名” schema=”指定的schema”>

     < id name="id" type=”int“>
     	< column name="编号">
     		< generator class="sequence">
     			< param class="sequence_name_in_use">AUTOID< /param>    
     			< !--  hibernate5.x版本及以后 -->
     		< /generator>
     	< /column>
     < /id>
    
    
     < property name="name" type="java.lang.String">
     	< column name="姓名">
     < /property >
     :* ************ :* < !-- 数据库表格对应实体的字段-- >
    

    < /class>
    < /hibernate-mapping >

  2. PO (Persistence Object)
    < mapping class=“bean类”/>
    JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
    TABLE:使用一个特定的数据库表格来保存主键。
    SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。 Oracle
    IDENTITY:主键由数据库自动生成(主要是自动增长型) MySQL
    AUTO:主键由程序控制。

@Entity
@Table(name="***",schema=“xxx”)
public class Bean{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
//members
//constructers
//setter getter
}

SessionFactory的初始化

configures settings from hibernate.cfg.xml

  1. Hibernate 4.2/4.3
    (configuration→buildSessionFactory)

    // A SessionFactory is set up once for an application
    sessionFactory = new Configuration()
    .configure()
    .buildSessionFactory();

  2. Hibernate 5.0/5.1/5.2 (StandardServiceRegistryBuilder→MetadataSources→SessionFactory)

支持上述方法,hibernate向后兼容

// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
		.configure()
		.build();
try {
	sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
    //destroy it manually.
	StandardServiceRegistryBuilder.destroy( registry );
}
  1. SSH框架合并 springContext.xml(可自定义命名)
    c3p0+LocalSessionFactoryBean
    demo as the following
<beans 
****************>
	<context:component-scan base-package="org.sxf.com.rj174211">
	</context:component-scan>      
	 <context:property-placeholder location="classpath:db.properties"/>       
	<bean id ="dataSource" 	class="com.mchange.v2.c3p0.
	ComboPooledDataSource">       	
		<property name="user" 		value="${jdbc.user}">
		</property>       	
		<property name="password" 		value="${jdbc.password}">
		</property>       	
		<property name="jdbcUrl" 		value="${jdbc.jdbcUrl}">
		</property>       	
		<property name="driverClass" 		value="${jdbc.driverClass}">
		</property>       	
		<property name="initialPoolSize"					value="${jdbc.initialPoolSize}">
		</property>       	
		<property name="maxPoolSize" 			value="${jdbc.maxPoolSize}">
		</property>       
	</bean>       
	<bean id="sessionFactory" 			class="org.springframework.orm.hibernate5.
	LocalSessionFactoryBean">       	
		<property name="dataSource" 		ref="dataSource">	
		</property>       	
		<property name="configLocation" 		value="classpath:hibernate.cfg.xml">
		</property> 
	</bean>
</beans>

创建DAO需要使用的Session的方法

save(Object)
update(Object)
delete(Object)
get(class,id)
createQuery(“FROM Table”).list(); //可以额外添加select子句
createQuery(“FROM Table”).uniqueResult();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值