- 论坛徽章:
- 0
|
刚学习J2EE不久,在struts+oracle+hibernate+myeclipse的环境下自己学了一段,大概3周时间,掌握了一些皮毛,其中一些要点如下:
1、如何用myeclipse提供的根据实现关系-对象的映射
(1)数据表必须有主键,否则需要修改映射文件
(2)最好用DAO形式
(3)用映射文件创建POJO
2、编码问题
(1)eclipse中的JSP文件乱码,第一行加上
(2)form读入时乱码,见自己的百度BLOG文章
http://hi.baidu.com/biguoting/blog/item/a40c8c1080a31201203f2e44.html
3、类型转换
借鉴资料,创建类型转换的类
4、直接使用hibernateDAO操作数据库
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
//Add a new admin
session.save(studentinfo);
//session.flush();
tx.commit ();
}catch(HibernateException e){
throw e;
}finally{
//if (tx!=null) {
//tx.rollback();
//}
HibernateSessionFactory.closeSession();
}
5、用hibernate调用存储过程
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
CallableStatement ps=null;
Connection conn=null;
conn=session.connection();
String procedure = "{call ADD_STUDENTINFO(?,?,?) }";
try{
ps=conn.prepareCall(procedure);
ps.setString(1, studentinfo.getSname());
ps.setDate(2, studentinfo.getBirthday());
ps.setLong(3, studentinfo.getSex());
ps.execute();
tx.commit();
}
catch(java.sql.SQLException err)
{
System.out.print("error occured in op !");
}
finally
{
HibernateSessionFactory.closeSession();
}
还有一些技巧,大部分是自己尝试的,先写这么多吧~~~~
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4461/showart_189021.html |
|