您的位置:首页 > 其它

Hibernate初学之CURD

2013-03-05 20:59 113 查看
Hibernate初学之CURD

以前学过JDBC的增删改查,感觉挺方便的,用hibernate框架则会更加简便,

关于hibernate的增删改查模块,我感觉用代码解释再合适不过了;

首先是导包问题:应用hibernate需要导十个包:



这些包在大部分是在hibernate资源 里面,这个可以到官网上下载;

接下来是配置文件:

这些都讲过了:hibernate.cfg.xml

<hibernate-configuration>

<session-factory name="foo">

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql:///test</property>

<property name="connection.username">root</property>

<property name="connection.password">root</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="connection.password">root</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

<mapping resource="com/hbsi/domain/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

这个配置文件必须放到src里面,

还需要一个java类与表的连接的配置文件:命名格式一般都为类名.hbm.xml;

<hibernate-mapping package="com.hbsi.domain">

<class name="User" table="user">

<id name="id">

<generator class = "native" />

</id>

<property name="name" />

<property name="birthday" />

</class>

</hibernate-mapping>

这个配置文件必须放到相关类的同一个包下;

这样配置文件就写完了;

需要简单的写一个类,具有相关的属性;并实现set和get方法必须有一个默认的构造参数;

还需要一个工具类:

private static SessionFactory sessionFactory ;

static{

Configuration conn = new Configuration().configure();

sessionFactory = conn.buildSessionFactory();

}

public static SessionFactory getSessionFactory(){

return sessionFactory ;

}

public static Session getSession(){

return sessionFactory.openSession();

}

然后开始写:属于规范类型的

//查找所有用户

public List<User> findAll() {

Session s=null;

try{

s = DBManager.getSession();

Query query = s.createQuery("from User");

List<User> lists = query.list();

return lists;

}finally{

if(s!=null){

s.close();

}

}

}

//根据id进行查找

public User fingUserById(Integer id) {

Session s=null;

try{

s = DBManager.getSession();

User user = (User) s.get(User.class, id);

return user;

}finally{

if(s!=null){

s.close();

}

}

}

//删除用户

public void remove(User entity) {

Session s = null;

try{

s.beginTransaction();

s.delete(entity);

s.beginTransaction().commit();

}finally{

if(s!=null){

s.close();

}

}

}

//添加一个用户

public void saveUser(User entity) {

Session s =null;

try{

s = DBManager.getSession();

s.beginTransaction();

s.save(entity);

s.beginTransaction().commit();

}finally{

if(s!=null){

s.close();

}

}

}

//更新一个用户

public void updateUser(User entity) {

Session s =null;

try{

s = DBManager.getSession();

s.beginTransaction();

s.update(entity);

s.beginTransaction().commit();

}finally{

if(s!=null){

s.close();

}

}

}

这就是Hibernate的CURD;

最后测试一下:

public static void main(String[] args) {

// TODO Auto-generated method stub

User user = new User();

user.setName("ss");

user.setBirthday(new Date());

UserDao dao = new UserDaoImpl();

dao.saveUser(user);

dao.findAll();

/*dao.updateUser(user);

dao.remove(user);

dao.fingUserById(3);*/

}

本文出自 “我为自己代言” 博客,请务必保留此出处http://fighter2013.blog.51cto.com/6204387/1147907
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: