项目场景:
在QueryRunner封装bean时出现
java.sql.SQLException: Cannot create backstage.bean.Estate: Class org.apache.commons.dbutils.BeanProcessor can not access a member of class backstage.bean.Estate with modifiers "" Query: select eid,`name`,introduction intro,pic,about from estate Parameters: []
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:351)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:212)
at backstage.dao.BaseDao.getBeanList(BaseDao.java:103)
at backstage.dao.impl.IEstateDaoImpl.getIndexEstate(IEstateDaoImpl.java:23)
at test.EstateTest.testEstateDao(EstateTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:436)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:170)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:166)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:113)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:112)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:120)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:120)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:120)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:120)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
问题描述:
困扰很久的问题
根据错误意思,问题是访问权限
项目结构

/WebTest/src/backstage/bean/Estate.java
package backstage.bean;
public class Estate {
private Integer eid;
private String name;
private String intro;
private String pic;
private String about;
Estate() {
name = null;
pic = "/WebTest/WebContent/img/index/hasiProject/kittens.jpg";
}
public Estate(Integer eid, String name, String intro, String pic, String about) {
super();
this.eid = eid;
this.name = name;
this.intro = intro;
this.pic = pic;
this.about = about;
}
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
@Override
public String toString() {
return "Estate [eid=" + eid + ", name=" + name + ", intro=" + intro + ", pic=" + pic + ", about=" + about + "]";
}
}
/WebTest/src/backstage/dao/BaseDao.java
package backstage.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import backstage.util.DBConnectUtil;
/**
* 定义一个用来被继承的对数据库进行基本操作的Dao
*
* @author YL
*
* @param <T>
*/
public class BaseDao<T> {
private QueryRunner queryRunner = new QueryRunner();
//定义一个变量来接收泛型的类型
private Class<T> type;
// 获取T的Class对象,获取泛型的类型,泛型是在被子类继承时才确定
@SuppressWarnings("unchecked")
public BaseDao() {
//获取子类的类型
@SuppressWarnings("rawtypes")
Class clazz = this.getClass();
//获取父类的类型
//getGenericSuperclass()用来获取当前类的父类的类型
//ParameterizedType表示的是带泛型的类型
ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass();
//获取具体的泛型类型 getActualTypeArguments获取具体的泛型的类型
//这个方法会返回一个Type的数组
Type[] types = parameterizedType.getActualTypeArguments();
//获取具体的泛型的类型·
this.type = (Class<T>) types[0];
}
/**
* 通用的增删改操作
*
* @param sql
* insert
* delete
* update
* @param params
* @return
*/
public int update(String sql, Object... params) {
// 获取连接
// backstage.util.DBConnectUtil
Connection connection = DBConnectUtil.getConn();
int count = 0;
try {
count = queryRunner.update(connection, sql, params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectUtil.free(connection);
}
return count;
}
/**
* 获取一个对象
*
* @param sql
* @param params
* @return
*/
public T getBean(String sql, Object... params) {
// 获取连接
Connection connection = DBConnectUtil.getConn();
T t = null;
try {
t = queryRunner.query(connection, sql, new BeanHandler<T>(type),
params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectUtil.free(connection);
}
return t;
}
/**
* 获取所有对象
* @param sql
* @param params
* @return
*/
public List<T> getBeanList(String sql, Object... params) {
// 获取连接
Connection connection = DBConnectUtil.getConn();
List<T> list = null;
try {
list = queryRunner.query(connection, sql, new BeanListHandler<T>(type), params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectUtil.free(connection);
}
return list;
}
}
/WebTest/src/backstage/dao/impl/IEstateDaoImpl.java
package backstage.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import backstage.bean.Estate;
import backstage.dao.BaseDao;
import backstage.dao.IEstateDao;
import backstage.util.DBConnectUtil;
public class IEstateDaoImpl extends BaseDao<Estate> implements IEstateDao {
// private Connection conn = DBConnectUtil.getConn();
@Override
public List<Estate> getIndexEstate() {
List<Estate> list = null;
String sql = "select eid,`name`,introduction intro,pic,about from estate";
// 使用BaseDao解耦合
list = this.getBeanList(sql);
// PreparedStatement pstmt = null;
// try {
// pstmt = conn.prepareStatement(sql);
// ResultSet rs = pstmt.executeQuery();
// list = new ArrayList<Estate>();
// while(rs.next()) {
// int eid = rs.getInt(1);
// String name = rs.getNString(2);
// String intro = rs.getNString(3);
// String pic = rs.getNString(4);
// String about = rs.getNString(5);
// Estate estate = new Estate(eid,name,intro,pic,about);
// list.add(estate);
// }
// pstmt.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
return list;
}
}
/WebTest/src/test/EstateTest.java 测试类
package test;
import java.util.List;
import org.junit.jupiter.api.Test;
import backstage.bean.Estate;
import backstage.dao.IEstateDao;
import backstage.dao.impl.IEstateDaoImpl;
class EstateTest {
@Test
void testEstateDao() {
// fail("Not yet implemented");
IEstateDao iEstateDao = new IEstateDaoImpl();
List<Estate> list = iEstateDao.getIndexEstate();
for (Estate estate : list) {
System.out.println(estate);
}
}
}
原因分析:
bean的构造函数没有设置为public
导致QueryRunner无法创建
结论:
在使用QueryRunner封装bean时注意
使用QueryRunner数据库字段与bean字段不一致时sql要使用别名
注意bean的构造函数和类本身都要为public
本文分析了使用QueryRunner封装Bean时出现的访问权限问题,详细介绍了错误原因及解决办法,强调了Bean构造函数和类的公共访问权限的重要性。
529

被折叠的 条评论
为什么被折叠?



