好了,废话不多说了,接着今天早上我们的示例接着做完.
五,现在我们开始做DAO层的接口,不用多说了吧,我还是使用自己管理SessionFactory的方式来写,没办法了,已经习惯了.
1、IBaseDAO.java
package
org.login.dao;
import org.hibernate.Session;
public interface IBaseDAO {
public Session getSession();
}
import org.hibernate.Session;
public interface IBaseDAO {
public Session getSession();
}
2、再写IBaseDAO的实现类:BaseDAO.java
package
org.login.dao.Impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.login.dao.IBaseDAO;
public class BaseDAO implements IBaseDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this .sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.openSession();
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.login.dao.IBaseDAO;
public class BaseDAO implements IBaseDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this .sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.openSession();
}
}
3、我们现在做实体的映射
Users.java:
package
org.login.vo;
@SuppressWarnings( " serial " )
public class Users implements java.io.Serializable {
// Fields
private Integer id;
private String username;
private String password;
// Constructors
/** default constructor */
public Users() {
}
/** full constructor */
public Users(String username, String password) {
this .username = username;
this .password = password;
}
// Property accessors
public Integer getId() {
return this .id;
}
public void setId(Integer id) {
this .id = id;
}
public String getUsername() {
return this .username;
}
public void setUsername(String username) {
this .username = username;
}
public String getPassword() {
return this .password;
}
public void setPassword(String password) {
this .password = password;
}
}
@SuppressWarnings( " serial " )
public class Users implements java.io.Serializable {
// Fields
private Integer id;
private String username;
private String password;
// Constructors
/** default constructor */
public Users() {
}
/** full constructor */
public Users(String username, String password) {
this .username = username;
this .password = password;
}
// Property accessors
public Integer getId() {
return this .id;
}
public void setId(Integer id) {
this .id = id;
}
public String getUsername() {
return this .username;
}
public void setUsername(String username) {
this .username = username;
}
public String getPassword() {
return this .password;
}
public void setPassword(String password) {
this .password = password;
}
}
<?
xml version="1.0" encoding="utf-8"
?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
< hibernate-mapping >
< class name ="org.login.vo.Users" table ="users" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="username" type ="java.lang.String" >
< column name ="username" length ="50" not-null ="true" />
</ property >
< property name ="password" type ="java.lang.String" >
< column name ="password" length ="50" not-null ="true" />
</ property >
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
< hibernate-mapping >
< class name ="org.login.vo.Users" table ="users" >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="native" />
</ id >
< property name ="username" type ="java.lang.String" >
< column name ="username" length ="50" not-null ="true" />
</ property >
< property name ="password" type ="java.lang.String" >
< column name ="password" length ="50" not-null ="true" />
</ property >
</ class >
</ hibernate-mapping >
4、我们现在写实体操作接口与实现:
IUsersDAO.java
package
org.login.dao;
import java.util.List;
import org.login.vo.Users;
@SuppressWarnings( " unchecked " )
public interface IUsersDAO {
public boolean checkLogin(Users user);
public boolean saveUser(Users user);
public boolean deleteUser(Integer id);
public boolean updateUser(Users user);
public List < Users > findAllUsers();
public Users getUserById(Integer id);
}
import java.util.List;
import org.login.vo.Users;
@SuppressWarnings( " unchecked " )
public interface IUsersDAO {
public boolean checkLogin(Users user);
public boolean saveUser(Users user);
public boolean deleteUser(Integer id);
public boolean updateUser(Users user);
public List < Users > findAllUsers();
public Users getUserById(Integer id);
}
UsersDAO.java
package
org.login.dao.Impl;
import java.util.List;
import org.hibernate.Transaction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.login.dao.IUsersDAO;
import org.login.vo.Users;
@SuppressWarnings( " unchecked " )
public class UsersDAO extends BaseDAO implements IUsersDAO {
@Override
public boolean checkLogin(Users user) {
Session session = null ;
session = getSession();
String hql = " from Users u where u.username = ? and u.password = ? " ;
Query query = session.createQuery(hql);
query.setParameter( 0 , user.getUsername());
query.setParameter( 1 , user.getPassword());
List list = query.list();
session.close();
if (list.size() > 0 ) {
return true ;
}
return false ;
}
@Override
public boolean deleteUser(Integer id) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
Users user = (Users)session.get(Users. class , id);
session.delete(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public boolean saveUser(Users user) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
session.save(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public boolean updateUser(Users user) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
session.update(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public List < Users > findAllUsers() {
List < Users > users = null ;
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
String hsql = " from Users u " ;
Query query = session.createQuery(hsql);
users = query.list();
tx.commit();
return users;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return users;
}
@Override
public Users getUserById(Integer id) {
Session session = null ;
session = getSession();
String hql = " from Users u where u.id = ? " ;
Query query = session.createQuery(hql);
query.setParameter( 0 , id);
List list = query.list();
Users user = null ;
if (list.size() > 0 ) {
user = (Users)list.get( 0 );
}
session.close();
return user;
}
}
import java.util.List;
import org.hibernate.Transaction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.login.dao.IUsersDAO;
import org.login.vo.Users;
@SuppressWarnings( " unchecked " )
public class UsersDAO extends BaseDAO implements IUsersDAO {
@Override
public boolean checkLogin(Users user) {
Session session = null ;
session = getSession();
String hql = " from Users u where u.username = ? and u.password = ? " ;
Query query = session.createQuery(hql);
query.setParameter( 0 , user.getUsername());
query.setParameter( 1 , user.getPassword());
List list = query.list();
session.close();
if (list.size() > 0 ) {
return true ;
}
return false ;
}
@Override
public boolean deleteUser(Integer id) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
Users user = (Users)session.get(Users. class , id);
session.delete(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public boolean saveUser(Users user) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
session.save(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public boolean updateUser(Users user) {
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
session.update(user);
tx.commit();
return true ;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return false ;
}
@Override
public List < Users > findAllUsers() {
List < Users > users = null ;
Transaction tx = null ;
Session session = null ;
try {
session = getSession();
tx = session.beginTransaction();
String hsql = " from Users u " ;
Query query = session.createQuery(hsql);
users = query.list();
tx.commit();
return users;
} catch (Exception e) {
if (tx != null ) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}
return users;
}
@Override
public Users getUserById(Integer id) {
Session session = null ;
session = getSession();
String hql = " from Users u where u.id = ? " ;
Query query = session.createQuery(hql);
query.setParameter( 0 , id);
List list = query.list();
Users user = null ;
if (list.size() > 0 ) {
user = (Users)list.get( 0 );
}
session.close();
return user;
}
}
好了,我们这一篇就到这里啦,下一篇请大家继续关注我的博客连载,大家也可以先下载示例代码先看看哈,如果有看不懂的给我留言。
下载地址:
Struts2.0+Spring2.0+Hibernate3.1登录及增删改查的完整演示示例下载地址:http://download.csdn.net/source/326113
以前的一个版本:Struts1.2+Spring1.2+HIbernate3.1整合登录示例示例:http://download.csdn.net/source/323772