对象增删改查与SQL查询操作方法
对象增删改查与sql查询操作方法。
packagedao;
importjava.util.list;
importorg.hibernate.query;
importorg.hibernate.session;
importorg.hibernate.transaction;
importorg.junit.test;
importdomain.user;
importutils.hibernateutils;
publicclassuserdao{
//增加用户
@test
publicvoidadduser(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
useruser=newuser();//瞬时对象
user.setname("小王");
user.setsex("女");
user.setage(33);
user.setheight(180);
user.sethobby("做饭");
session.save(user);//持久对象
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//查询用户
@test
publicvoidfinduser(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
useruser=session.get(user.class,4);//持久对象
system.out.println(user);
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//修改用户
@test
publicvoidupdateuser(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
useruser=session.get(user.class,4);
user.setage(25);
user.sethobby("吃饭");
session.update(user);//持久对象
system.out.println(user);
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//删除用户
@test
publicvoiddeleteuser(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
useruser=session.get(user.class,6);
session.delete(user);
system.out.println(user);
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//简单查询
@test
publicvoidqueryuser(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
queryquery=session.createquery("fromuser");
listusers=query.list();
for(useruser:users){
system.out.println(user);
}
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//属性查询
@test
publicvoidqueryusername(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
queryquery=session.createquery("selectu.namefromuseru");
listnames=query.list();
for(stringuser:names){
system.out.println(user);
}
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//实例化查询
@test
publicvoidqueryuserexample(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
queryquery=session.createquery("fromuserwhereage=21");
listusers=query.list();
for(useruser:users){
system.out.println(user);
}
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//统计查询
@test
publicvoidqueryusergroup(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
queryquery=session.createquery("selectavg(u.age)fromuseru");
numberavgage=(number)query.uniqueresult();
system.out.println("平均年龄为:"+avgage);
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
//子查询
@test
publicvoidqueryuserin(){
sessionsession=null;
transactionts=null;
try{
session=hibernateutils.getsession();
ts=session.begintransaction();
queryquery=session.createquery("fromuseruwhereu.age>(selectavg(age)fromuser)");
useruser=null;
listlist=query.list();
for(inti=0;iuser=(user)list.get(i);
system.out.println(i+"姓名:"+user.getname()+"——年龄:"+user.getage());
}
ts.commit();
}catch(exceptione){
if(ts!=null)
{
ts.rollback();
}
e.printstacktrace();
}
}
}