ServiceImpl extends HibernateDaoSupport 查询语句

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;
			}
			
		});
		
	}

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值