http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.2.5.ga/hibernate-3.2.5.ga.zip //hibernate lib 下载
需要
1.hibernate3.jar
2./lib/*.jar 所有的依赖包
hibernate 的三种状态以及三种状态的关系
hiberate curd的一些关系
hibernate.cfg.xml //hibernate 配置
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///easier</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="hbm/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml //mapper 映射
<?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.easier.hibernate.domain">
<!-- 表名如果是关键字
1.改表名
2.加反引号 (数字键1前面那个键)
-->
<class name="User" table="`users`" >
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="native"/>
</id>
<!-- 字段如果是关键字
1.Colume改字段名
2.也加反引号 (数字键1前面那个键)
-->
<property name="name" length="10" not-null="true" type="java.lang.String"/>
<property name="birthday" not-null="true" type="java.util.Date" />
</class>
</hibernate-mapping>
user.java //domain
/**
*
*/
package cn.easier.hibernate.domain;
import java.io.Serializable;
import java.util.Date;
/**
* @author
*
*/
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Date birthday;
public User() {
}
/**
* @param id
* @param name
* @param birthday
*/
public User(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
UserTest.java //junit 测试类
package cn.easier.hibernate.junit;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.easier.hibernate.domain.User;
/**
*
*/
/**
* @author
*
*/
public class UserTest {
// 如果hibernate.cfg.xml不在classes或bin目录下,configure()里面就要写文件路径的全名称;默认就是在classes或bin目录下;
private static Configuration cf = new Configuration().configure();
private static SessionFactory sf = null;
private static Session session = null;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
sf = cf.buildSessionFactory();
}
@Test
public void save() {
User user = new User();
user.setName("zhangsan");
user.setBirthday(new Date());
session = sf.openSession();
Transaction ts = session.getTransaction();
ts.begin();
session.save(user);
ts.commit();
session.close();
sf.close();
}
@Test
public void delete() {
Integer delete_id = 3;
session = sf.openSession();
Transaction ts = session.beginTransaction();
// 第一种
User user = new User("aa", new Date()); // user 的其它属性一定要赋值,不然会报错
user.setId(delete_id);
session.delete(user);
// 第二种
// Query query = session.createQuery("delete User u where u.id=:id");
// delete User u where u.id=:id :id[命名参数]表示起个别名叫id
// query.setInteger("id", delete_id); 赋值
// Query query = session.createQuery("delete User u where u.id=?");
// query.setParameter(0, delete_id); //索引是从0开始
// query.executeUpdate();
ts.commit();
session.close();
sf.close();
}
@Test
public void update() {
Integer query_id = 1;
session = sf.openSession();
User user = new User("lisi1", new Date());
user.setId(query_id);
Transaction ts = session.beginTransaction();
session.update(user);
ts.commit();
session.close();
sf.close();
}
@Test
public void get() {
session = sf.openSession();
Class<User> clazz = User.class;
User user = (User) session.get(clazz, 1);
if (user != null) {
System.out.println("name:" + user.getName());
}
session.close();
sf.close();
}
@Test public void hqlGet(){
session = sf.openSession();
Query query=session.createQuery("select count(*) from User");
/*org.hibernate.hql.QueryExecutionRequestException:
* Not supported for select queries [select count(*) from cn.easier.hibernate.domain.User]
* query.executeUpdate() 只能执行update 或delete
* */
System.out.println("count:" + query.uniqueResult());
session.close();
sf.close();
}
@Test public void criteria(){
session = sf.openSession();
Criteria c=session.createCriteria(User.class);
//c.add(Restrictions.idEq(1)); 查询id=1的数据
System.out.println("count:" + c.setProjection(Projections.rowCount()).uniqueResult());
session.close();
sf.close();
}
}
- 大小: 12.6 KB
- 大小: 59.4 KB
- 大小: 81.6 KB
- 大小: 86 KB
分享到:
相关推荐
hibernate helloword annotation version
一个小练习,Hibernate HelloWord,仅供初学者使用。 使用的是:hibernate-release-4.3.11.Final
hibernate初学者适用 stuts2,spring,爱好学习java的同学 从程序员升级到工程师大多数象我这样对软件有浓厚兴趣的人学习印度的软件开发管理方法
NULL 博文链接:https://wangmuchang.iteye.com/blog/1724554
每个程序的开始:helloword
使用Eclipse创建Maven项目,并利用springMvc创建web项目,并实现Helloword
helloword
android helloword
mybatis的helloword程序代码,mybatis的helloword程序代码
HelloWord文件
HelloWord示例
NULL 博文链接:https://hbiao68.iteye.com/blog/1546980
Python打印helloword
VC++.NET编程实例(HelloWord)
开发环境工具 1、Flex Builder3。 2、myeclipse6.5。 3、Java Development Kit(JDK),版本1.5以上(包括...4、BlazeDS3.2。(下载地址: http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/) 5、Tomcat6.0。
鸿蒙系统的helloWord
NULL 博文链接:https://wanglihu.iteye.com/blog/1405585
非常好
Android环境搭配及简单HelloWord 做为入门者不得不看的教程