用hibernate query.list()方法返回一个数据库对象集合对象 users 遍历这个

用hibernate query.list()方法返回一个数据库对象集合对象 users 遍历这个,第1张

List<User> users=new arraryList<User>();

User user=usersget(0);

这样直接能获取集合中第一个对象

用反射来做:

/

JSON 转 POJO

@param map

@param pojo

@return

@throws Exception

/

public static Object JsonToPojo(JSONObject json, Object pojo) throws Exception {

// 获得对象的类型

Class classType = pojogetClass();

String[] key=JSONObjectgetNames(json);

for (int i = 0; i < keylength; i++) {

try{

Field field = classTypegetDeclaredField(key[i]);

fieldsetAccessible(true);

fieldset(pojo, jsonget(key[i]));

}

catch(Throwable e)

{

}

}

return pojo;

}

上面是我以前写的一个方法,把JSON转成POJO,,你可以参考一下

如果直接修改当中某个属性  sql直接update *** 作

如果存在比较复杂的逻辑判断,sql查出多条数据应该用集合接收吧,集合的遍历

List<DeveloperBox> list = queryAllByCache(isAll);

for(DeveloperBox d:list){

    //

}

public static void main(String[] args) {

//初始化数据

List<String> allList = new ArrayList<String>();

allListadd("学生A、语文、90分、第一学期");

allListadd("学生B、语文、80分、第一学期");

allListadd("学生A、英语、90分、第一学期");

allListadd("学生A、数学、90分、第一学期");

allListadd("学生B、英语、80分、第一学期");

allListadd("学生A、语文、90分、第二学期");

allListadd("学生B、数学、80分、第一学期");

allListadd("学生B、英语、80分、第二学期");

allListadd("学生A、数学、90分、第二学期");

allListadd("学生B、语文、80分、第二学期");

//定义姓名、学期、科目 数组

List<String> stuList = new ArrayList<String>();

List<String> subjList = new ArrayList<String>();

List<String> termList = new ArrayList<String>();

boolean stuContained = false;

boolean subjContained = false;

boolean termContained = false;

//从allList中 加载相上述三个类别,如果固定的话,可以手动指定

for (String string : allList) {

String[] strs = stringsplit("、");

stuContained = false;

subjContained = false;

termContained = false;

if(stuListsize()<=0) stuContained = false;

else{

for (String string2 : stuList) {

if(string2equals(strs[0])){

stuContained = true;

break;

}

}

}

if(!stuContained)

stuListadd(strs[0]);

if(subjListsize()<=0) subjContained = false;

else{

for (String string2 : subjList) {

if(string2equals(strs[1])){

subjContained = true;

break;

}

}

}

if(!subjContained)

subjListadd(strs[1]);

if(termListsize()<=0) termContained = false;

else{

for (String string2 : termList) {

if(string2equals(strs[3])){

termContained = true;

break;

}

}

}

if(!termContained)

termListadd(strs[3]);

}

//构建结果数组

List<String> results = new ArrayList<String>();

String temp = "";

for (String string : stuList) {

temp ="姓名:"+string;

for (String string2 : termList) {

temp += ",学期:"+string2;

int score = 0;

for (String string3 : subjList) {

temp+= ","+string3+":";

for (String string4 : allList) {

String[] temps = string4split("、");

if(temps[0]equals(string) && temps[1]equals(string3)&& temps[3]equals(string2)){

temp+= temps[2];

score += IntegerparseInt(temps[2]substring(0, temps[2]length()-1));

}

}

}

String[] temp2 = tempsplit(",");

temp += ",总分:" + score +"分";

resultsadd(temp);

temp ="姓名:"+string;

}

}

//输出结果

for (String string : results) {

Systemoutprintln(string);

}

}

只是一种方案,又很多情况不确定,如果list中的数据可以作为javabean设置会方便些,如果数据是在数据库中的,那么使用sql语句会方便很多(上面的做法实际上是模拟了sql语句)

select f1 from table1的结果集做为查询条件循环查询。

如:

set @a =select f1 from table1

foreach (@a)

{

select  from table2

where f2=@a

}

sql 语句是对数据库进行 *** 作的一种语言。 结构化查询语言(Structured Query Language)简称SQL,结构化查询语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。

给你一段代码你就会用了!

package customdao;

import javasqlConnection;

import javasqlResultSet;

import javasqlStatement;

import javautilArrayList;

import javautilList;

import customstrutsformSellerForm;

import customstrutsformCustomForm;

import customdaoConnectionManager;

public class SellerDao {

/针对seller表/

//1,查询

public static List findAll(Connection conn){

Statement stmt = null;

ResultSet rs = null;

List list = new ArrayList();

try{

conn = ConnectionManagergetConnection();

stmt = conncreateStatement();

rs = stmtexecuteQuery("select from seller where seller_power='2'");

while(rsnext()){

SellerForm sf = new SellerForm();

sfsetSeller_name(rsgetString("seller_name"));

sfsetSeller_psw(rsgetString("seller_psw"));

sfsetSeller_power(rsgetString("seller_power"));

sfsetSeller_id(rsgetString("seller_id"));

listadd(sf);

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagercloseConnection(conn);

}

return list;

}

public static List findId(String id){

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

List list = new ArrayList();

try{

stmt = conncreateStatement();

rs = stmtexecuteQuery("select from seller where seller_id='"+id+"'");

while(rsnext()){

SellerForm sf = new SellerForm();

sfsetSeller_id(rsgetString("seller_id"));

sfsetSeller_name(rsgetString("seller_name"));

sfsetSeller_power(rsgetString("seller_power"));

sfsetSeller_psw(rsgetString("seller_psw"));

listadd(sf);

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return list;

}

//2,删除

public static int delete(String id){

Connection conn = null;

Statement stmt = null;

int flag = 0;

try{

conn = ConnectionManagergetConnection();

stmt = conncreateStatement();

flag = stmtexecuteUpdate("delete seller where seller_id='"+id+"'");

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return flag;

}

//3,修改

public static int update(String id,String name,String psw){

Connection conn = null;

Statement stmt = null;

int n = 0;

try{

stmt = conncreateStatement();

n = stmtexecuteUpdate("update seller set seller_name='"+name+"',seller_psw='"+psw+"' where seller_id="+id);

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return n;

}

//4,插入

public static int insert(String name,String psw){

Connection conn = null;

Statement stmt = null;

int i = 0;

try{

stmt = conncreateStatement();

i = stmtexecuteUpdate("insert into seller (select max(seller_id)+1 ,"+name+","+psw+",'2' from seller)");

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return i;

}

//处理登录这个模块

public static String login(String name,String psw,Connection conn){

Statement stmt = null;

ResultSet rs = null;

String power = "";

try{

stmt = conncreateStatement();

rs = stmtexecuteQuery("select from seller where seller_name='"+name+"' and seller_psw='"+psw+"'");

//如果查询出姓名密码来了就获取权限值

if(rsnext()){

power = rsgetString("seller_power");

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return power;

}

/针对custom表/

//1,查询

public static List findAllCustom(Connection conn){

Statement stmt = null;

ResultSet rs= null;

String sql1 = "";

String sql2 = "";

List list = new ArrayList();

try{

stmt=conncreateStatement();

sql1 = "select seller_name,customer_name,customer_sex,customer_email,customer_phone ";

sql2 = "from customer,seller where customerseller_id=sellerseller_id and customer_state='1'";

rs = stmtexecuteQuery(sql1+sql2);

while(rsnext()){

CustomForm c = new CustomForm();

csetSeller_name(rsgetString("seller_name"));

csetCustomer_name(rsgetString("customer_name"));

csetCustomer_sex(rsgetString("customer_sex"));

csetCustomer_email(rsgetString("customer_email"));

csetCustomer_phone(rsgetString("customer_phone"));

listadd(c);

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return list;

}

public static List dispaterSeller(Connection conn){

Statement stmt = null;

ResultSet rs = null;

List list = new ArrayList();

try{

stmt = conncreateStatement();

String sql = "select from seller where seller_power='2'";

rs = stmtexecuteQuery(sql);

while(rsnext()){

SellerForm sf = new SellerForm();

sfsetSeller_name(rsgetString("seller_name"));

sfsetSeller_id(rsgetString("seller_id"));

listadd(sf);

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return list;

}

public static List dispaterCustom(Connection conn){

Statement stmt = null;

ResultSet rs = null;

List list = new ArrayList();

try{

stmt = conncreateStatement();

String sql = "select from customer where customer_state='0'";

rs = stmtexecuteQuery(sql);

while(rsnext()){

CustomForm cf = new CustomForm();

cfsetCustomer_name(rsgetString("customer_name"));

cfsetCustomer_id(rsgetString("customer_id"));

listadd(cf);

}

}catch(Exception e){

eprintStackTrace();

}finally{

ConnectionManagergetConnection();

}

return list;

}

public static void dispater(String sid,String cid,Connection conn,int count,int i){

Statement stmt = null;

try{

stmt = conncreateStatement();

stmtexecuteUpdate("update customer set seller_id='"+sid+"',customer_state='1' where customer_id ="+cid);

}catch(Exception e){

eprintStackTrace();

}finally{

if(count==i){

ConnectionManagergetConnection();

}

}

}

}

以上就是关于用hibernate query.list()方法返回一个数据库对象集合对象 users 遍历这个全部的内容,包括:用hibernate query.list()方法返回一个数据库对象集合对象 users 遍历这个、如何遍历list集合,然去取出里面的内容,将其封装到javabean对象中、数据库数据遍历等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10153756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存