JAVA基础SSH简单整合,第1张

JAVA基础SSH简单整合,第2张

/*首先配置Spring */
Xml代码的applicationContext.xml文件[/Br/][/Br/][/Br/][/Br/][/Br/][/Br/][/Br/][/Br/][/Br/][/Br/]org . hibernate . dialect . SQL server dialect[/Br/]true[/Br/]true[/Br/]25[/Br/]50
import com . your company . POJO . tuser;
公共接口Iuser {
公共void save(Tuser t);
public void更新(Tuser t);
public Java . util . list get all();
public Tuser getOne(整数id);
public void delete(Tuser t);
public void delete(整数id);
} Java代码
Spring是面向接口的编程,所以定义了一个DAO接口包com你的公司;interdao导入com . your company . POJO . tuser;公共接口Iuser { public void save(Tuser t);公共void更新(Tuser t);public Java . util . list get all();public Tuser getOne(整数id);public void delete(Tuser t);public void delete(整数id);} Spring是面向接口的编程,所以定义一个DAO接口
package com . your company . interdao;
import com . your company . POJO . tuser;
公共接口Iuser {
公共void save(Tuser t);
public void更新(Tuser t);
public Java . util . list get all();
public Tuser getOne(整数id);
public void delete(Tuser t);
public void delete(整数id);
}
Java代码
实现了上面的接口
package com . your company . Dao;
导入Java . SQL . SQL exception;
导入Java . util . list;
导入org . hibernate . hibernate exception;
导入org . hibernate . query;
导入org . hibernate . session;
导入org . spring framework . ORM . hibernate 3 . hibernate callback;
导入org . spring framework . ORM . hibernate 3 . support . hibernatedaosupport;
import com . your company . inter Dao . iuser;
import com . your company . POJO . tuser;
公共类UserManager扩展HibernateDaoSupport实现Iuser {
public void delete(Tuser t){
this . gethibernatetemplate()。删除(t);
}
public void delete(final Integer id){
gethibernate template()。execute find(new hibernate callback(){
public Object doInHibernate(Session Session)
抛出HibernateException,SQLException {
Query Query = Session . create Query(" delete from Tuser t where t . id =?");
query.setInteger(0,id);
query . execute update();
返回null
}
});
} @ Override
public List get all(){
return gethibernate template()。execute find(new hibernate callback(){
public Object doInHibernate(Session Session)
抛出HibernateException,SQLException {
Query Query = Session . create Query(" from Tuser ");
return query . list();
}
});
}
@ Override
public Tuser getOne(Integer id){
Tuser t =(Tuser)this . gethibernate template()。get(Tuser.class,id);
return t;
}
@ Override
public void save(Tuser t){
this . gethibernate template()。保存(t);
}
@ Override
public void update(Tuser t){
this . gethibernate template()。saveourupdate(t);
}
}
实现上面的接口
package com . your company . Dao;
导入Java . SQL . SQL exception;
导入Java . util . list;
导入org . hibernate . hibernate exception;
导入org . hibernate . query;
导入org . hibernate . session;
导入org . spring framework . ORM . hibernate 3 . hibernate callback;
导入org . spring framework . ORM . hibernate 3 . support . hibernatedaosupport;
import com . your company . inter Dao . iuser;
import com . your company . POJO . tuser;公共类UserManager扩展HibernateDaoSupport实现Iuser {
public void delete(Tuser t){
this . gethibernatetemplate()。删除(t);
}
public void delete(final Integer id){
gethibernate template()。execute find(new hibernate callback(){
public Object doInHibernate(Session Session)
抛出HibernateException,SQLException {
Query Query = Session . create Query(" delete from Tuser t where t . id =?");
query.setInteger(0,id);
query . execute update();
返回null
}
});
}
@ Override
public List get all(){
return gethibernate template()。execute find(new hibernate callback(){
public Object doInHibernate(Session Session)
抛出HibernateException,SQLException {
Query Query = Session . create Query(" from Tuser ");
return query . list();
}
});
}
@ Override
public Tuser getOne(Integer id){
Tuser t =(Tuser)this . gethibernate template()。get(Tuser.class,id);
return t;
}
@ Override
public void save(Tuser t){
this . gethibernate template()。保存(t);
}
@ Override
public void update(Tuser t){
this . gethibernate template()。saveourupdate(t);
}
}
Xml代码
如何读取Spring配置文件
1 .将StrutsConfig.xml文件中的

作为插件读取。2.阅读

org . spring framework . WEB . context . context loader listener

ContextConfigLocation

/we b-INF/classes/application context . XML

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » JAVA基础SSH简单整合

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情