hibernate学习记录1
来源:互联网 发布:pokemon go 坐标软件 编辑:程序博客网 时间:2024/05/21 18:47
hibernate.cfg.xml文件
<!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">oracle.jdbc.driver.OracleDriver</property>
<!--配置链接数据库的url、username、password-->
<!--该路径指连接本地的1521端口号-->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">a123</property>
<!-- 配置数据库使用的方言 -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- 使不存在的表自动创建,在测试时使用。。。 -->
<property name="hbm2ddl.auto">create</property>
<!-- 在程序运行时,是否在控制台打印sql语句,在开发时指定为TRUE,方便测试。。。 -->
<property name="show_sql">true</property>
<!-- 告诉配置文件所连接的对象,指定持久化类的映射文件 -->
<mapping resource="hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
----------------------------------------------------------------------------------------------------------------------------------------
User.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="hibernate.domain">
<!-- 把user类与user表的关系映射 -->
<class name="UserTest" table="UserTest" >
<id name="userId" column="user_id" type="java.lang.Integer">
<generator class="native">
<param name="sequence">user_sequence</param>
</generator>
</id>
<property name="userName" column="user_name" type="java.lang.String"/>
<property name="birthday" column="birthday" type="java.util.Date"/>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------------------------------------------------------------
在oracle中创建sequence
create sequence user_sequence
increment by 1
start with 1000
nomaxvalue
nocycle
cache 10;
----------------------------------------------------------------------------------------------------------------------------------------
编写测试文件
public class UserTest {
private int userId;
private String userName;
private Date birthday;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Base {
public static void main(String[] args) {
//通过Configuration获得一个SessionFactory对象
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//打开一个session
Session s = sf.openSession();
//开始事物
Transaction ts = s.beginTransaction();
UserTest user = new UserTest();
user.setUserName ("李四");
user.setBirthday(new Date());
s.save(user);
//提交事物
ts.commit();
s.close();
System.out.println("end");
}
}
----------------------------------------------------------------------------------------------------------------------------------------
后台运行结果
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select user_sequence.nextval from dual
Hibernate: insert into UserTest (user_name, birthday, user_id) values (?, ?, ?)
end
- hibernate学习记录1
- Hibernate学习记录1
- Hibernate学习记录1 基本概念
- Hibernate学习记录
- hibernate学习记录
- hibernate缓存学习记录
- hibernate学习记录2
- hibernate学习记录3
- hibernate学习记录5
- hibernate学习记录6
- hibernate学习记录7
- hibernate学习记录8
- Hibernate 学习记录
- Hibernate 学习记录 2
- Hibernate 学习记录 3
- Hibernate 学习记录 4
- Hibernate学习记录2
- 关于Hibernate学习记录
- 文件检测函数 ferror feof
- Loadrunner=Could not reserve enough space for object heap
- IT技术型人才的职业规划方向(全面的参考)早准备早收获
- 基于消息的异步套接字
- 关于occlusion的综述
- hibernate学习记录1
- java位运算符和位运算表达式
- git 学习笔记
- 反向代理服务器的工作原理
- IOS实例 弹出框中添加多选
- PHP中的 抽象类(abstract class)和 接口(interface)
- sql server2005中的try与CATCH
- oracle邮件提醒
- C++类内静态函数和静态成员的小例子