关于当前搭建Spring Mvc和hibernate框架接口的总结
来源:互联网 发布:卡扎菲怎么死的 知乎 编辑:程序博客网 时间:2024/06/18 01:26
在hibernate中,进行了对jdbc的轻量级的封装,对数据库的操作可通过获取session然后进行一系列事务操作
在Spring中,则又提供了对hibernate进行了部分数据库操作的封装HibernateTemplate,这个类中包含了对hibernate事务操作的一系列开启事务 打开session等等,并且也提供了直接执行hql和criteria的操作的接口,当然对于特殊的需求,HibernateTemplate也可直接获取session进行操作
hibernate进行多表查询每个表中各取几个字段,也就是说查询出来的结果集没有一个实体类与之对应如何解决;
解决方案一,按照Object[]数据取出数据,然后自己组bean
解决方案二,对每个表的bean写构造函数,比如表一要查出field1,field2两个字段,那么有一个构造函数就是Bean(type1 filed1,type2
field2) ,然后在hql里面就可以直接生成这个bean了。以下为封装的模版
package com.cqut.dao.user;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import com.cqut.dao.base.DAOSupport;
import com.cqut.dao.user.customInterface.IUserDao;
import com.cqut.entity.user.User;
import com.cqut.util.StringUtil;
@Repository("userDao")
public class UserDao extends DAOSupport implements IUserDao {
@Override
public boolean save(User user) {
try{
this.template.save(user);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
@Override
public String saveEntityWithReturn(User user) {
return this.template.save(user).toString();
}
@Override
public boolean saveEntities(final User[] users) {
boolean result=(Boolean)this.template.execute(new HibernateCallback()
{
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
if(users!=null&&users.length>0){
try{
int length=users.length;
session.beginTransaction();
for(int i=0;i<length;i++){
session.save(users[i]);
if (i % 10 == 0) {
session.flush();
session.clear();
}
}
session.getTransaction().commit(); // 提交事物
return true;
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally {
session.close(); // 关闭Session
}
return false;
}
else{
return false;
}
}
});
return result;
}
@Override
public boolean updateByID(User user, String userID) {
user.setUserID(userID);
try{
this.template.update(user);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
@Override
public boolean updateByCondition(User user, String condition) {
String sql="update User set "+user.toValueString()+" where "+condition;
int count=this.template.bulkUpdate(sql);
if(count>0){
return true;
}
else{
return false;
}
}
@Override
public boolean updateByEntity(User user, User userCondition) {
String sql="update User set "+user.toValueString().replaceAll("AND", ",")+" where "+userCondition.toValueString();
int count=this.template.bulkUpdate(sql);
if(count>0){
return true;
}
else{
return false;
}
}
@Override
public boolean deleteByID(String userID) {
User user=new User();
user.setUserID(userID);
return this.deleteByEntity(user);
}
@Override
public boolean deleteByCondition(String condition) {
String sql="delete from User where "+condition;
int count=this.template.bulkUpdate(sql);
if(count>0){
return true;
}
else{
return false;
}
}
@Override
public boolean deleteByEntity(User user) {
try{
this.template.delete(user);
return true;
}catch(Exception e){
return false;
}
}
@Override
public User getUserByID(String userID) {
return this.template.get(User.class, userID);
}
@Override
public Map<String, Object> findUserByID(String[] properties, String userID) {
String[] property=StringUtil.analyseArray(properties);
String hql=null;
if(property[1]==""||property[1]==null){ //如果不存在别名
hql="select new Map("+property[0]+") from User where userID='"+userID+"'";
}
else{
hql="select new Map("+property[0]+") from "+property[1]+" where user.userID='"+userID+"'";
}
List<Map> maps=this.template.find(hql);
if(maps!=null&&maps.size()>0){
Map<String,Object> map=new HashMap<String,Object>();
return (Map<String, Object>)maps.get(0);
}
else{
return null;
}
}
@Override
public User getUserByCondition(String condition) {
String sql="from User where "+condition;
List<User> users=this.template.find(sql);
if(users!=null&&users.size()>0){
return users.get(0);
}
else{
return null;
}
}
@Override
public Map<String, Object> findUserByCondition(String[] properties,
String condition) {
String[] property=StringUtil.analyseArray(properties);
String hql=null;
if(property[1]==""||property[1]==null){ //如果不存在别名
hql="select new Map("+property[0]+" ) from User where "+condition;
}
else{
hql="select new Map("+property[0]+" ) from "+property[1]+" where "+condition;
}
List<Map> lists=this.template.find(hql);
if(lists!=null&&lists.size()>0){
return (Map<String, Object>)lists.get(0);
}
else{
return null;
}
}
@Override
public List<User> getUsersByCondition(String condition) {
String sql="from User where "+condition;
List<User> users=this.template.find(sql);
return users;
}
@Override
public List<Map<String, Object>> findUsersByCondition(String[] properties,
String condition) {
String[] property=StringUtil.analyseArray(properties);
String hql=null;
if(property[1]==""||property[1]==null){ //如果不存在别名
hql="select new Map( "+property[0]+" ) from User where "+condition;
}
else{
hql="select new Map( "+property[0]+" ) from "+property[1]+" where "+condition;
}
List<Map<String,Object>> lists=this.template.find(hql);
return lists;
}
@Override
public List<User> getUsersByEntity(User user) {
List<User> users=this.template.findByExample(user);
return users;
}
@Override
public List<Map<String, Object>> findUsersByEntity(String[] properties,
User user) {
String[] property=StringUtil.analyseArray(properties);
String hql=null;
if(property[1]==""||property[1]==null){ //如果不存在别名
hql="select new Map("+property[0]+") from User where "+user.toValueString();
}
else{
hql="select new Map( "+property[0]+" ) from "+property[1]+" where "+user.toValueString();
}
List<Map<String,Object>> lists=this.template.find(hql);
return lists;
}
}
0 0
- 关于当前搭建Spring Mvc和hibernate框架接口的总结
- spring mvc + hibernate 框架搭建
- Spring MVC+hibernate+Spring的框架搭建中遇到的问题总结
- 搭建Spring+Spring MVC+Hibernate开发框架
- Spring MVC框架的搭建
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- 搭建Spring Mvc+Spring+Hibernate开发框架详细说明
- 搭建Spring Mvc+Spring+Hibernate开发框架详细说明
- 用Maven搭建Spring+Spring MVC+Hibernate框架
- 【接口测试】-总结-关于spring框架的数据库连接配置
- spring mvc框架搭建和配置
- Spring MVC 框架搭建和详解
- spring+spring mvc+hibernate框架调试错误总结
- Spring mvc 框架搭建
- spring mvc 框架搭建
- Spring mvc框架搭建
- Spring Mvc 框架搭建
- spring MVC 框架搭建
- hdu1232-我的第一道并查集
- Java虚拟机工作原理详解
- Maven 2 for Eclipse配置全攻略(Windows篇)
- 自定义dialog的布局样式
- stdcall详解
- 关于当前搭建Spring Mvc和hibernate框架接口的总结
- PE文件结构详解
- 扩展欧几里得
- adt20以上代码混淆
- 求两个正整数的最大公约数
- 在iis中发布自己asp.net网站
- java.lang.IllegalStateException: commit already called
- 转载--数论总结
- Java NIO框架Netty教程(一) – Hello Netty