编写
Hibernate
DAO
实现类
tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud._01_basic_way.dao.UserDAO
,在功能验证的基础上添加一定程度的参数检查和预处理,并统一进行异常和收尾处理:
……
public class UserDAO implements I_UserDAO
{
private SessionFactory sf;
……
public UserDAO(SessionFactory sf)
{
this.sf = sf;
}
……
public int create(User user) throws SQLException
{
if (null == user)
{
return -1;
}
Session session = null;
try
{
session = sf.openSession();
Transaction ts = session.beginTransaction();
Serializable srRtn = session.save(user);
ts.commit();
int intJdbcCode = NumberToolE.o2i(srRtn, 0);
return intJdbcCode;
}
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public User findById(Integer itgUserId) throws SQLException
{
SqlSession session = null;
try
{
session = sf.openSession();
User user = (User) session.get(User.class, itgUserId);
return user;
}
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public List<User> listByName(String strUserName) throws SQLException
{
Session session = null;
try
{
String strHql =
"from User where name="
+ (null == strUserName ? null
: "'" + strUserName + "'")
+ " order by name, id";
System.out.println("HQL:\n\t" + strHql);
session = session = sf.openSession();
Query query = session.createQuery(strHql);
@SuppressWarnings("unchecked")
List<User> lstUsers = query.list();
return lstUsers;
}
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public List<User> listAll() throws SQLException
{
Session session = null;
try
{
session = sf.openSession();
Query query = session.getNamedQuery(
"tfw.dash_01._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll");
@SuppressWarnings("unchecked")
List<User> lstUsers = query.list();
return lstUsers;
}
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public List<User> listByRange(int intStartRowIdx, int intSelectRowCount)
throws SQLException
{
Session session = null;
try
{
session = session = sf.openSession();
Query query = session.getNamedQuery(
"tfw.dash_01._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll2");
query.setFirstResult(intStartRowIdx);
query.setMaxResults(intSelectRowCount);
@SuppressWarnings("unchecked")
List<User> lstUsers = query.list();
return lstUsers;
}
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public int update(User user) throws SQLException
{
if (null == user)
{
return -1;
}
……
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public int discardById(Integer itgUserId) throws SQLException
{
……
catch (Exception e)
{
throw toSQLException(e);
}
finally
{
finallyOperation(session);
}
}
……
public String toString()
{
……
}
……
private SQLException toSQLException(Exception e)
{
if (null == e)
{
return new SQLException("Unknown database operation exception!");
}
else if (e instanceof SQLException)
{
return (SQLException) e;
}
else
{
return new SQLException(e);
}
}
……
private void finallyOperation(SqlSession session)
{
if (null != session)
{
try
{
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
说明
/
注意事项:
- “toSQLException(……);”、“finallyOperation(……);”:
各数据操作方法采用统一的异常处理和收尾处理。