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


0 0
原创粉丝点击