ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

通过commons-DBUtils和commons-BeanUtils操作数据库以及根据返回集赋值给bean对象及其外键

2022-06-24 11:03:42  阅读:8  来源: 互联网

标签:pid commons 外键 gec bean org import com


通过commons-DBUtils和commons-BeanUtils操作数据库以及根据返回集赋值给bean对象及其外键

需要用到的jar包:image-20220624004206721

我在这里遇到一些问题,使用beanutils的beanutil.populate()方法,报缺少commons-collections.FastHashMap类,

原因解释:img

解决方法:

​ 根据官方描述,如果使用的时1.7.x版本或者1.8.x版本的话,下载下来就是内含三个主要jar包的,所以,导入直接就可使用。但是,由于我所使用的时1.9.x(我用的1.9.4版本)的,所以,下载下来只有一个jar包,因此,时缺少所依赖的collections的jar包的,我们根据官方提供的连接进行下载。另外,一定要注意beanutils和依赖包collections的版本对应问题。我们可以查看官网:找到dependencies依赖一栏,可以看到beanutils所依赖的两个jar包及其所对应的版本,我们点击对应链接进行下载即可(BeanUtils 1.9.4下载collections包的3.2.2版本)。(一定要下载指定版本!!!!)image-20220624084312054

官网网址https://commons.apache.org/proper/commons-beanutils/

collections3.2.2:https://commons.apache.org/proper/commons-collections/

logging1.2:https://commons.apache.org/proper/commons-logging/download_logging.cgi

daoImpl中的代码:

package com.gec.store.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.junit.Test;

import com.gec.store.bean.CartItem;
import com.gec.store.bean.Product;
import com.gec.store.dao.CartItemDao;
import com.gec.store.util.C3P0Util;
import com.gec.store.util.DBUtil;

/**
 * @author: Lemon E-mail:1027880379@qq.com
 * @version:
 * @Since: Created in 2022年6月24日 上午12:07:44
 * @Description:
 */
public class CartItemDaoImpl extends DBUtil<CartItem> implements CartItemDao {
	/**
	 * 购物车表中是否有uid、pid对应的商品记录
	 * 
	 * @throws SQLException
	 */
	@Override
	public CartItem hasCartItem(String uid, String pid) throws SQLException {
		String sql = "SELECT"
				+ "	c.id,"
				+ "	c.uid,"
				+ "	c.pid,"
				+ "	c.c_total AS total,"
				+ "	c.c_count AS count,"
				+ "	p.pname,"
				+ "	p.market_price,"
				+ "	p.shop_price,"
				+ "	p.pimage,"
				+ "	p.pdate,"
				+ "	p.is_hot,"
				+ "	p.pdesc,"
				+ "	p.pflag,"
				+ "	p.cid  "
				+ "FROM "
				+ "	cart c "
				+ "	JOIN product p ON c.pid = p.pid  "
				+ "WHERE "
				+ "	c.uid = ?"  
				+ "	AND c.pid = ?";
		QueryRunner runner = new QueryRunner(C3P0Util.getDs_poole());
		// 根据sql的列字段,返回一个map结果集,Map<字段名,属性>
		Map<String, Object> resultMap = runner.query(sql, new MapHandler(), uid, pid);
		if (resultMap == null) {
			return null;
		}
		try {
			// 首先把结果集中的购物车信息,填充到cartItem对象中,
			CartItem cartItem = new CartItem();
			BeanUtils.populate(cartItem, resultMap);
			// 从结果集中,把商品的信息,填充到product对象中,
			Product product = new Product();
			BeanUtils.populate(product, resultMap);

			// 将填充玩的product对象赋值给cartItem的外键
			cartItem.setProduct(product);
			return cartItem;
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
			return null;
		}
	}



}

标签:pid,commons,外键,gec,bean,org,import,com
来源: https://www.cnblogs.com/lemonaaa/p/16407979.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有