applicationContext.xml
Dao 注入 SessionFactory
<!-- dao -->
<bean name="stockDao" class="com.tz.dao.impl.StockDaoImpl" scope="prototype" >
<!-- <property name="dataSource" ref="dataSource" /> Jdbc
ServieImpl extends JdbcDaoSuppor ...
getJdbcTemplate()...
-->
<!-- <property name="sessionFactory" ref="sessionFactory" />
serviceImpl extends HibernateDaoSupport ...
getHibernateTemplate()...
-->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
//HibernateDaoSupport为dao注入SessionFactory/dataSoure
public class UserDaoImpl extends HibernateDaoSupport/jdbcDaoSouppert implements UserDao {
@Override
public User getByUserCode(String userCode) {
//hql
/*return getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql = "from User where userCode = ?";
Query query = session.createQuery(hql);
query.setParameter(0, userCode);
User user = (User) query.uniqueResult();
return user;
}
});*/
//hql离线查询
DetachedCriteria forClass = DetachedCriteria.forClass(User.class);
forClass.add(Restrictions.eq("userCode", userCode));
List<User> userList = (List<User>) getHibernateTemplate().findByCriteria(forClass);
if (userList!=null && userList.size() > 0) {
return userList.get(0);
}else {
return null;
}
}
//增
@Override
public int stockAdd(Stock stock) {
//extends HibernateDaoSupport
Serializable save = getHibernateTemplate().save(stock);
int parseInt = Integer.parseInt(save.toString());
int i = 1/0;
return parseInt;
//extends JdbcDaoSouppert
String sql = "insert into tz_stock(rptWarehouseName,rptWarehouseman,rptMobilePhone,rptAddress,rptRemarks) values(?,?,?,?,?)";
int update = getJdbcTemplate().update(sql, stock.getRptWarehouseName(),stock.getRptWarehouseman(),stock.getRptMobilePhone(),
stock.getRptAddress(),stock.getRptRemarks());
return update;
}
//改
@Override
public int userMail(final String userCode) {
return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
@Override
public Integer doInHibernate(Session session) throws HibernateException {
String hql = "update User set userState = 1 where userCode = ?";
Query query = session.createQuery(hql);
query.setParameter(0, userCode);
return query.executeUpdate();
}
});
}
// 查
@Override
public List<Stock> stockList() {
//extends HibernateDaoSupport
//List<Stock> list = (List<Stock>) getHibernateTemplate().find("from Stock where stockId = ?", 6);
List<Stock> list = (List<Stock>) getHibernateTemplate().find("from Stock ");
return list;
-----------------------------------------
//extends JdbcDaoSouppert
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Stock.class);
List<Stock> stocklist = criteria.list();
return stocklist;
----------------------------
//extends JdbcDaoSouppert
//rowMap查询 注意返回时,是直接retur方法 不要强转
String sql = "select stockId,rptWarehouseman from tz_stock";
return getJdbcTemplate().query(sql, new RowMapper<Stock>() {
@Override
public Stock mapRow(ResultSet rs, int arg1) throws SQLException {
Stock stock = new Stock();
stock.setStockId(rs.getInt("stockId"));
stock.setRptWarehouseman(rs.getString("rptWarehouseman"));;
return stock;
}
});
}
}