hibernate——helloworld
来源:互联网 发布:成都余香科技知乎 编辑:程序博客网 时间:2024/06/06 20:33
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "[User: id="+id+",name="+name+"]";
}
}
====================================
package cn.icss.a_helloworld;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sessionFactory;
static{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");//读取指定的主配置文件
sessionFactory = cfg.buildSessionFactory();//根据配置生成session工厂
}
@Test
public void testSave(){
User user = new User();
user.setName("zhangsan");
//保存?
Session session = sessionFactory.openSession();//打开一个新的session
Transaction tx = session.beginTransaction();//开始事务
session.save(user);
tx.commit();//提交事务
session.close();//关闭session,释放资源
}
@Test
public void testGet(){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
User user = (User) session.get(User.class, 2);//获取
System.out.println(user);
tx.commit();
session.close();
}
}
=========================================
<!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 name="foo">
<!-- 配置数据库信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql:///hibernate_20140422</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 其他配置 -->
<!-- 显示生成的sql语句 -->
<property name="show_sql">true</property>
<!-- 导入映射文件 -->
<mapping resource="cn/icss/a_helloworld/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
=================================
<?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="cn.icss.a_helloworld">
<class name="User" table="t_user">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" type="string" column="name" />
</class>
</hibernate-mapping>
- hibernate——helloworld
- Hibernate—>HelloWorld
- hibernate+maven——helloworld
- hibernate初探——艰辛的helloworld
- hibernate4.x(一)——hibernate入门之helloworld
- hibernate helloworld
- Helloworld!Hibernate
- hibernate-helloworld
- Hibernate-HelloWorld
- Hibernate-helloWorld
- Hibernate学习总结(一)——Intellij IDEA下新建Hibernate HelloWorld
- Hibernate HelloWorld-13 hibernate 二级缓存
- 2.1 Hibernate的HelloWorld
- hibernate的helloworld
- Hibernate's HelloWorld
- Hibernate HelloWorld(第一步)
- Hibernate的HelloWorld
- Hibernate HelloWorld(一)
- 搭建python下web (tornado + Dgango)
- 把HTML表单提交的数据转化成XML文件
- C++ List 双向链表
- 软件测试管理中预防bug的重要性及bug分析
- <label>标签for属性的妙用
- hibernate——helloworld
- Week9练习
- Luke:Lucene索引查看工具
- Leetcode: Binary Tree Level Order Traversal II 理解分析
- (2)CSS指定长度的三种方式
- 死锁
- 获取SDCard的目录路径功能
- week 9
- C++ Vector 向量容器