只有在dao层能得到数据,传不到Biz层,如果在Biz层 buyinfoDAO=new BuyinfoDAOImpl();就没问题了,但就没有依赖注入的意义了
java代码:
applicationContext.xml配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<list><value>classpath:hibernate.cfg.xml</value></list>
</property>
</bean>
<!-- 配置Hibernate的事务管理器 -->
<!-- 使用HibernateTransactionManager类实现基于Hibernate的事务管理器 -->
<bean id="TxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事物通知,需要指定一个事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="TxManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义那些方法应用这些规则 -->
<aop:pointcut id="serviceMethod" expression="execution(* com.jxc.biz.*.*(..))" />
<!-- 将应用通知与应用规则方法组合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
<!-- 配置用户处理DAO -->
<bean id="buyinfoDAO" class="com.jxc.dao.impl.BuyinfoDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置用户业务处理Biz -->
<bean id="buyinfoBiz" class="com.jxc.biz.impl.BuyinfoBizImpl">
<property name="buyinfoDAO" ref="buyinfoDAO"></property>
</bean>
<!-- 配置用户处理Action -->
<bean id="buyinfoaction" class="com.jxc.action.BuyinfoAction">
<property name="buyinfoBiz" ref="buyinfoBiz"></property>
</bean>
</beans>
dao实现类
package com.jxc.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.jxc.bean.Buyinfo;
import com.jxc.dao.BuyinfoDAO;
import com.jxc.util.HibernateSessionFactory;
public class BuyinfoDAOImpl extends HibernateDaoSupport implements BuyinfoDAO {
Session session=null;
public List<Buyinfo> select() {
session=HibernateSessionFactory.getSession();
String hql="from Buyinfo";
Query query = session.createQuery(hql);
List list=query.list();
/* List<Buyinfo> list=this.getHibernateTemplate().find("from Buyinfo");*/用这种方法得不到值
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
BuyinfoDAOImpl buyinfoDAOImpl=new BuyinfoDAOImpl();
List<Buyinfo> list=buyinfoDAOImpl.select();
System.out.println(list.size());
}
}
biz层:
package com.jxc.biz.impl;
import java.util.List;
import com.jxc.bean.Buyinfo;
import com.jxc.biz.BuyinfoBiz;
import com.jxc.dao.BuyinfoDAO;
import com.jxc.dao.impl.BuyinfoDAOImpl;
public class BuyinfoBizImpl implements BuyinfoBiz {
private BuyinfoDAO buyinfoDAO ;
public void setBuyinfoDAO(BuyinfoDAO buyinfoDAO) {
this.buyinfoDAO = buyinfoDAO;
}
/**
* 查询进货记录
*/
public List select() {
//buyinfoDAO=new BuyinfoDAOImpl();
List<Buyinfo> list=null;
try {
list = this.buyinfoDAO.select();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args){
BuyinfoBizImpl buy = new BuyinfoBizImpl();
List list=buy.select();
System.out.println(list.size());
}
}
package com.jxc.action;
import java.util.List;
import java.util.Map;
import com.jxc.bean.Buyinfo;
import com.jxc.biz.BuyinfoBiz;
import com.jxc.biz.impl.BuyinfoBizImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BuyinfoAction extends ActionSupport {
private Buyinfo buy=null;
private BuyinfoBiz buyinfoBiz;
public Buyinfo getBuy() {
return buy;
}
public void setBuy(Buyinfo buy) {
this.buy = buy;
}
public void setBuyinfoBiz(BuyinfoBiz buyinfoBiz) {
this.buyinfoBiz = buyinfoBiz;
}
@SuppressWarnings("unchecked")
public String select(){
Map session = ActionContext.getContext().getSession();
BuyinfoBizImpl b=new BuyinfoBizImpl();
List list=null;
try {
list = b.select();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.put("buyinfo", list);
return "select";
}
}