在javaEE中,java类的属性通过getter和setter来定义,get(或set)方法去除get(set)后,首字母小写即为Java类的属性。操作java类的属性有一个工具包,BeanUtils,其中的setProperty()方法是通过javaBean中的set方法赋值的。
使用BeanUtils需要加入两个jar包,
1.测试BeanUtils中的SetProperty()方法。
@Test
public void testSetProperty() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
Object object = new Student();
System.out.println(object);
BeanUtils.setProperty(object,"type",2434);
System.out.println(object);
System.out.println(BeanUtils.getProperty(object,"type"));
}
运行后:
①创建一个Student对象student,未赋值之前属性为空;
②BeanUtils.setProperty(object,"type",2434); 为student赋值,属性为对应的Student类get(或set)方法去除get(set)后再把首字母小写;
③赋值后使用BeanUtils.getProperty(object,"type"); 获得属性值。
DAO:Data Access Object,是访问数据信息的类,包含了对数据的CRUD(create,read,update,delete),而不包含任何业务相关的信息,更容易实现功能的模块化,有利于代码的维护和升级。
1.update,包括插入,删除,更新操作
public void update(String sql,Object ... args){
Connection connection=null;
PreparedStatement preparedstatement=null;
ResultSet resultset=null;
try{
connection=JDBCTools.getConnection();
preparedstatement=connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedstatement.setObject(i+1, args[i]);
}
preparedstatement.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCTools.release(resultset,preparedstatement, connection);
}
}
测试方法:
@Test
public void testUpdate() {String sql="INSERT INTO EXAMSTUDENT(FlowId,TYPE,IdCard,ExamCard,StudentName,Location,Grade)"+ "VALUES(?,?,?,?,?,?,?)";dao.update(sql,1,2,"23313","2321","Li","大连",313);}
2.查询多条记录,返回对应的查询对象的集合
public <T> List<T> getForList(Class<T> clazz,String sql,Object ... args){
List<T> list=new ArrayList<>();
Connection connection=null;
PreparedStatement preparedstatement=null;
ResultSet resultset=null;
try{
//1.得到结果集resultset
connection=JDBCTools.getConnection();
preparedstatement=connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedstatement.setObject(i+1, args[i]);
}
resultset=preparedstatement.executeQuery();
//2.处理结果集,将得到的ResultSet结果集中的别名和列值存入到Map中,得到Map的List
List<Map<String,Object>> listMap = handleResultSetToMapList(resultset);
//3.将Map的List转为clazz对应的List,其中Map的key即为clazz对应的属性名,Map的value为clazz对应的属性值
list=transferMapListToBeanList(clazz,listMap);
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCTools.release(resultset,preparedstatement, connection);
}
return list;
}
2.1 得到结果集
2.2 将ResultSet结果集转换为MapList的方法:handleResultSetToMapList(resultset)
private List<Map<String, Object>> handleResultSetToMapList(ResultSet resultset)
throws Exception, SQLException {
List<Map<String,Object>> list=new ArrayList<>();
//得到resultset中的列名
List<String> columnLabels=getColumnLabels(resultset);
Map<String,Object> map=null;
while(resultset.next()){
map=new HashMap<>();
//得到SQL查询的列数
int count=columnLabels.size();
for(String columnLabel:columnLabels){
Object columnValue=resultset.getObject(columnLabel);
//将别名,列值存入到Map中
map.put(columnLabel,columnValue);
}
list.add(map);
}
return list;
}
其中得到resultset中的列名的方法:getColumnLabels(resultset)
private List<String> getColumnLabels(ResultSet resultset) throws Exception{
List<String> labels=new ArrayList<String>();
ResultSetMetaData rsmd=resultset.getMetaData();
int count=rsmd.getColumnCount();
while(resultset.next()){
for(int i=0;i<count;i++){
labels.add(rsmd.getColumnLabel(i+1));
}
}
return labels;
}
2.3 将MapList转换为javaBean属性对应的List:transferMapListToBeanList(clazz,listMap)
private <T> List<T> transferMapListToBeanList(Class<T> clazz,List<Map<String, Object>> listMap) throws InstantiationException, IllegalAccessException, InvocationTargetException {
List<T> result=new ArrayList<>();
T bean=null;
if(listMap.size()>0){
//listMap中存的是多条记录的列名和列值,遍历listMap
for(Map<String,Object> map1:listMap){
bean=clazz.newInstance();
//遍历map1
for(Map.Entry<String, Object> entry:map1.entrySet()){
String propertyName=entry.getKey();
Object propertyValue=entry.getValue();
BeanUtils.setProperty(bean,propertyName,propertyValue);
}
result.add(bean);
}
}
return result;
}
测试方法:
@Test
public void testGetForList() {
String sql="SELECT FlowID flowId,TYPE type,IDCard idCard,ExamCard examCard,"
+ "StudentName studentName,Location location,Grade grade FROM EXAMSTUDENT";
List<Student> student=dao.getForList(Student.class,sql);
System.out.println(student);
}
3.查询一条记录,返回对应的对象
public <T> T get(Class<T> clazz,String sql,Object ... args){
List<T> result=getForList(clazz,sql,args);
if(result.size()>0){
return result.get(0);
}
return null;
}
4.返回某条记录的某一个字段的值或一个统计的值(一共有多少条记录等)
public <E> E getForValue(String sql,Object ... args){
Connection connection=null;
PreparedStatement preparedstatement=null;
ResultSet resultset=null;
try{
connection=JDBCTools.getConnection();
preparedstatement=connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedstatement.setObject(i+1, args[i]);
}
resultset=preparedstatement.executeQuery();
if(resultset.next()){
return (E)resultset.getObject(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCTools.release(resultset,preparedstatement, connection);
}
return null;
}
测试方法:
@Test
public void testGetForValue() {
String sql="SELECT ExamCard FROM EXAMSTUDENT WHERE FlowId=?";
String ExamCard=dao.getForValue(sql,1);
System.out.println(ExamCard);
}
wx搜索“程序员考拉”,专注java领域,一个伴你成长的公众号!
网友评论