81,092
社区成员
发帖
与我相关
我的任务
分享
package com.wtu.Dao;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.wtu.Entity.PageBean;
import com.wtu.Util.QueryHelper;
// @Transactional注解可以被继承
// @Transactional注解对父类中声明的方法无效
@Transactional
@SuppressWarnings("unchecked")
@Repository
public abstract class DaoSupportImpl<T> extends HibernateDaoSupport implements DaoSupport<T> {
/*@Resource
public SessionFactory sessionFactory;*/
public Class<T> clazz;
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}
/*public SessionFactory getSessionFactory() {
return sessionFactory;
}*/
/*@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("sessionFactory..."+sessionFactory);
this.sessionFactory = sessionFactory;
}*/
public DaoSupportImpl() {
// 使用反射技术得到T的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
System.out.println("clazz ---> " + clazz);
// System.out.println("sessionFactory ---> " + sessionFactory);
}
/**
* 获取当前可用的Session
*
* @return
*/
/*protected Session getSession() {
Session session = sessionFactory.getCurrentSession();
if(session==null){
session = sessionFactory.openSession();
}
return session;
}*/
public void save(T entity) {
getSession().save(entity);
}
public void update(T entity) {
getSession().update(entity);
}
public void delete(Long id) {
Object obj = getById(id);
if (obj != null) {
getSession().delete(obj);
}
}
public T getById(Long id) {
if (id == null) {
return null;
} else {
return (T) getSession().get(clazz, id);
}
}
public List<T> getByIds(Long[] ids) {
if (ids == null || ids.length == 0) {
return Collections.EMPTY_LIST;
} else {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")//
.setParameterList("ids", ids)//
.list();
}
}
public List<T> findAll() {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName())//
.list();
}
// 公共的查询分页信息的方法
@Deprecated
public PageBean getPageBean(int pageNum, int pageSize, String hql, List<Object> parameters) {
System.out.println("-------> DaoSupportImpl.getPageBean()");
// 查询本页的数据列表
Query listQuery = getSession().createQuery(hql); // 创建查询对象
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
listQuery.setParameter(i, parameters.get(i));
}
}
listQuery.setFirstResult((pageNum - 1) * pageSize);
listQuery.setMaxResults(pageSize);
List list = listQuery.list(); // 执行查询
// 查询总记录数量
Query countQuery = getSession().createQuery("SELECT COUNT(*) " + hql);
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
countQuery.setParameter(i, parameters.get(i));
}
}
Long count = (Long) countQuery.uniqueResult(); // 执行查询
return new PageBean(pageNum, pageSize, count.intValue(), list);
}
// 公共的查询分页信息的方法(最终版)
public PageBean getPageBean(int pageNum, int pageSize, QueryHelper queryHelper) {
System.out.println("-------> DaoSupportImpl.getPageBean( int pageNum, int pageSize, QueryHelper queryHelper )");
// 参数列表
List<Object> parameters = queryHelper.getParameters();
// 查询本页的数据列表
Query listQuery = getSession().createQuery(queryHelper.getListQueryHql()); // 创建查询对象
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
listQuery.setParameter(i, parameters.get(i));
}
}
listQuery.setFirstResult((pageNum - 1) * pageSize);
listQuery.setMaxResults(pageSize);
List list = listQuery.list(); // 执行查询
// 查询总记录数量
Query countQuery = getSession().createQuery(queryHelper.getCountQueryHql());
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
countQuery.setParameter(i, parameters.get(i));
}
}
Long count = (Long) countQuery.uniqueResult(); // 执行查询
return new PageBean(pageNum, pageSize, count.intValue(), list);
}
}
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.wtu.*" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="maxActive" value="100"/> <property name="initialSize"
value="10"/> <property name="maxWait" value="60000"/> <property name="minIdle"
value="1"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/> -->
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<property name="packagesToScan">
<value>com.wtu.*</value>
</property>
<!-- 包扫描的方式加载注解类 -->
<!-- <property name="mappingDirectoryLocations">
<list>
<value>com.wtu.*</value>
</list>
</property> -->
</bean>
<!-- 事物配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="read*" read-only="true" />
<tx:method name="sync*" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="*save" propagation="REQUIRED"/>
<tx:method name="*delete" propagation="REQUIRED"/>
<tx:method name="*update" propagation="REQUIRED"/>
<tx:method name="*get" propagation="REQUIRED"/>
<tx:method name="*list" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="list*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config><aop:pointcut id="pointcut" expression="execution(* com.wtu.Dao..*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
public interface BrandDao extends DaoSupport<Brand> {
//根据品牌查询下面的商品
public List<Commodity> findAllCommodity(Brand brand);
}
@Repository
public class BrandDaoImpl extends DaoSupportImpl<Brand> implements BrandDao{
public List<Commodity> findAllCommodity(Brand brand) {
// TODO Auto-generated method stub
return brand.getCommodity();
}
}
@Entity
@Table(name="brand",catalog="mall")
public class Brand extends IdEntity{
//品牌
@Column(name="name")
private String name;
@Column(name="logoURL")
private String logoURL;
@Column(name="logoIcon")
private String logoIcon;
//品牌对应多个商品
@OneToMany(cascade={CascadeType.REMOVE},mappedBy="brand")
private List<Commodity> commodity;
public Brand() {
super();
}
public Brand(String name, String logoURL, String logoIcon) {
super();
this.name = name;
this.logoURL = logoURL;
this.logoIcon = logoIcon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogoURL() {
return logoURL;
}
public void setLogoURL(String logoURL) {
this.logoURL = logoURL;
}
public String getLogoIcon() {
return logoIcon;
}
public void setLogoIcon(String logoIcon) {
this.logoIcon = logoIcon;
}
public List<Commodity> getCommodity() {
return commodity;
}
public void setCommodity(List<Commodity> commodity) {
this.commodity = commodity;
}
}