频道栏目
首页 > 程序开发 > 软件开发 > 其他 > 正文
Hibernate中应用dao设计模式进行数据库的增删改查操作
2016-12-05 09:36:32         来源:一览众山小  
收藏   我要投稿

1:User(数据持久层)

package com.hibernate1.entity;


import java.io.Serializable;


public class User implements Serializable {


   private Integer id;
   private String loginName;
   private String loginPwd;
   private String realName;
   private String address;
   private String phone;
   private String email;
   public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPwd() {
return loginPwd;
}
public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", loginName=" + loginName + ", loginPwd="
+ loginPwd + ", realName=" + realName + ", address=" + address
+ ", phone=" + phone + ", email=" + email + "]";
}


}

2:DAO(数据访问层)

(1:)UserDAOImpl(定义接口)

package com.dao;


public interface UserDAO {
   public void add(Object object);
   public void delete(Object object);
   public Object get(Class clas,Integer id);
   public void update(Object object);
   
}

(2:)HibernateDAO(进行数据库的增删改查)

package com.dao;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibernateDAO {
// 1:下面的方法默认加载hibernate.cfg.xml文件
private Configuration configuare = null;
// 2:创建sessionFactory
private SessionFactory sessionFactory = null;
// 3:创建session
private Session session = null;
// 4:开始事务,Hibernate必须显式的手动提交事务
private Transaction transaction = null;
public void init() {
configuare = new Configuration().configure();
sessionFactory = configuare.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
public void destory() {
// 7:关闭session
session.close();
// 8:关闭sessionFactory
sessionFactory.close();
}
/**
* 添加对象到数据表
* @param object
*/
public void add(Object object)
  {
 try
 {
 init();
 session.save(object);
 transaction.commit();
 }catch(Exception e)
 {
e.printStackTrace();
 }finally
 {
 destory();
 }
  }
  /**
* 查询从数据表到对象
* @param object
*/
public Object get(Class clas,Integer id)
{
Object object=null;
    try
    {
  init();
  object=session.get(clas,id);
  transaction.commit();
    }catch(Exception e)
    {
  e.printStackTrace();
    }finally
     {
  destory();
      }
   return object;
} 
/**
 * 从数据表删除数据
 * @param object
 */
  public void delete(Object object){
   try
   {
  init();
  session.delete(object);
  transaction.commit();
   }catch(Exception e)
   {
  e.printStackTrace();
   }finally
    {
  destory();
     }
  }
  /**
   * 从数据表修改数据
   * @param object
   */
    public void update(Object object){
     try
     {
    init();
    session.update(object);
    transaction.commit();
     }catch(Exception e)
     {
    e.printStackTrace();
     }finally
      {
    destory();
       }
    }
}

(3:)UserDAOImpl (j接口的实现类)

package com.dao;


public class UserDAOImpl extends HibernateDAO implements UserDAO {


@Override
public void add(Object object)
{
super.add(object);
}
@Override
public void delete(Object object)
{
super.delete(object);
}
@Override
public void update(Object object)
{
super.update(object);
}
@Override
public Object get(Class clas,Integer id)
{
return super.get(clas, id);
}

}

3:HibTest(业务逻辑层)

package com.biz;


import com.dao.UserDAO;
import com.dao.UserDAOImpl;
import com.hibernate1.entity.User;


/**
 * 业务逻辑层
 * @author Administrator
 *
 */
public class HibTest {
  public static void main(String[] args) {
 HibTest hibTest=new HibTest();
 //hibTest.addUser();
 //hibTest.deleteUser(new Integer(5));
// hibTest.updateUser(new Integer(4));
 hibTest.queryUser(new Integer(1));
}
    //添加数据
  public void addUser() {
User user = new User();
user.setLoginName("小飞");
user.setLoginPwd("md5234");
user.setRealName("李四");
user.setAddress("少林派");
user.setPhone("151");
user.setEmail("135@163.com");
UserDAO userdao=new UserDAOImpl();
userdao.add(user);
  }
  //删除数据
  public void deleteUser(Integer id)
  {
 UserDAO userdao=new UserDAOImpl();
 User user=(User) userdao.get(User.class,id);
 userdao.delete(user);
  }
  //修改数据
  public void updateUser(Integer id)
  {
 UserDAO userdao=new UserDAOImpl();
 User user=(User) userdao.get(User.class,id);
 user.setLoginPwd("密码变了");
 userdao.update(user);
  }
  //查询数据
  public void queryUser(Integer id)
  {
 UserDAO userdao=new UserDAOImpl();
 User user=(User) userdao.get(User.class,id);
 System.out.println(user);
  }
}

4:hibernate.cfg.xml(配置文件)

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://www.hibernate.org/dtd/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://127.0.0.1:3306/mydatabase</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/hibernate1/entity/User.hbm.xml"/>
<mapping resource="com/hibernate1/entity/Book.hbm.xml"/>
</session-factory>
</hibernate-configuration>

5:User.hbm.xml(映射文件)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
        <!--对象映射文件-->
    <class name="com.hibernate1.entity.User" table="users" catalog="mydatabase">
       <id name="id" type="java.lang.Integer">
            <column name="id"/>
            <!--指定主键的生成方式  -->
            <generator class="native"></generator>
       </id>
       <property name="loginName" type="java.lang.String">
        <column name="login_name" length="20"/>
       </property>
        <property name="loginPwd" type="java.lang.String">
        <column name="login_pwd" length="20"/>
       </property>
        <property name="realName" type="java.lang.String">
        <column name="real_name" length="20"/>
       </property>
        <property name="address" type="java.lang.String">
        <column name="Address"/>
       </property>
        <property name="phone" type="java.lang.String">
        <column name="Phone"/>
       </property>
         <property name="email" type="java.lang.String">
        <column name="Email"/>
       </property>
    </class>
</hibernate-mapping>
点击复制链接 与好友分享!回本站首页
上一篇:后序遍历 非递归方法 (利用辅助节点r)
下一篇:二叉树查找x的祖先
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站