修改默认查询结果(querylist())不以Object[]数组形式返回以Set形式返回但是因为Set里是不允许有重复的元素所以:username和password的值不能相同只需将hql改为String hql = select new set(namepasswd) from Users;
修改默认查询结果(querylist())不以Object[]数组形式返回以自定义类型返回
自定义类
Java代码
package comdomain;
public class MyUser {
private String username;
private String password;
//因为String hql = select new comdomainMyUser(namepasswd) from Users;所以必须要有接受个参数的构造函数
public MyUser(String usernameString password){
thisusername = username;
thispassword = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
Java代码
package comdomain;
public class MyUser {
private String username;
private String password;
//因为String hql = select new comdomainMyUser(namepasswd) from Users;所以必须要有接受个参数的构造函数
public MyUser(String usernameString password){
thisusername = username;
thispassword = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
Java代码
package comdomain;
public class MyUser {
private String username;
private String password;
//因为String hql = select new comdomainMyUser(namepasswd) from Users;所以必须要有接受个参数的构造函数
public MyUser(String usernameString password){
thisusername = username;
thispassword = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
Java代码
//通过querylist()出来的list里存放的不再是默认的Object数组了而是自定义的类MyUser必须加包名String hql = from Users;中的Users类也是必须加包名的但是因为再Usershbmxml里<hibernatemapping autoimport=true> autoimport默认值为true(所以autoimport属性也可以不写)自动导入了
String hql = select new comdomainMyUser(namepasswd) from Users;
Query query = sessioncreateQuery(hql);
//默认查询出来的list里存放的是一个Object数组但是在这里list里存放的不再是默认的Object数组了而是MyUser对象了
List<MyUser> myUsers = querylist();
for(MyUser myUser : myUsers){
String name = myUsergetUsername();
String passwd = myUsergetPassword();
Systemoutprintln(name + : + passwd);
}
/**
输出结果为
name : password
name : password
name : password
*/
Java代码
//通过querylist()出来的list里存放的不再是默认的Object数组了而是自定义的类MyUser必须加包名String hql = from Users;中的Users类也是必须加包名的但是因为再Usershbmxml里<hibernatemapping autoimport=true> autoimport默认值为true(所以autoimport属性也可以不写)自动导入了
String hql = select new comdomainMyUser(namepasswd) from Users;
Query query = sessioncreateQuery(hql);
//默认查询出来的list里存放的是一个Object数组但是在这里list里存放的不再是默认的Object数组了而是MyUser对象了
List<MyUser> myUsers = querylist();
for(MyUser myUser : myUsers){
String name = myUsergetUsername();
String passwd = myUsergetPassword();
Systemoutprintln(name + : + passwd);
}
/**
输出结果为
name : password
name : password
name : password
*/
Java代码
//通过querylist()出来的list里存放的不再是默认的Object数组了而是自定义的类MyUser必须加包名String hql = from Users;中的Users类也是必须加包名的但是因为再Usershbmxml里<hibernatemapping autoimport=true> autoimport默认值为true(所以autoimport属性也可以不写)自动导入了
String hql = select new comdomainMyUser(namepasswd) from Users;
Query query = sessioncreateQuery(hql);
//默认查询出来的list里存放的是一个Object数组但是在这里list里存放的不再是默认的Object数组了而是MyUser对象了
List<MyUser> myUsers = querylist();
for(MyUser myUser : myUsers){
String name = myUsergetUsername();
String passwd = myUsergetPassword();
Systemoutprintln(name + : + passwd);
}
/**
输出结果为
name : password
name : password
name : password
*/
条件查询
Java代码
//条件查询参数索引值从开始索引位置通过setStringsetParameter设置参数
String hql = from Users where name=? and passwd=?;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString( name);
// querysetString( password);
//第种方式
querysetParameter( nameHibernateSTRING);
querysetParameter( passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}
Java代码
//条件查询参数索引值从开始索引位置通过setStringsetParameter设置参数
String hql = from Users where name=? and passwd=?;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString( name);
// querysetString( password);
//第种方式
querysetParameter( nameHibernateSTRING);
querysetParameter( passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}
Java代码
//条件查询参数索引值从开始索引位置通过setStringsetParameter设置参数
String hql = from Users where name=? and passwd=?;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString( name);
// querysetString( password);
//第种方式
querysetParameter( nameHibernateSTRING);
querysetParameter( passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}
Java代码
//条件查询自定义索引名(参数名):username:password通过setStringsetParameter设置参数
String hql = from Users where name=:username and passwd=:password;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString(username name);
// querysetString(password password);
//第种方式第个参数确定类型
querysetParameter(username nameHibernateSTRING);
querysetParameter(password passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}
Java代码
//条件查询自定义索引名(参数名):username:password通过setStringsetParameter设置参数
String hql = from Users where name=:username and passwd=:password;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString(username name);
// querysetString(password password);
//第种方式第个参数确定类型
querysetParameter(username nameHibernateSTRING);
querysetParameter(password passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}
Java代码
//条件查询自定义索引名(参数名):username:password通过setStringsetParameter设置参数
String hql = from Users where name=:username and passwd=:password;
Query query = sessioncreateQuery(hql);
//第种方式
// querysetString(username name);
// querysetString(password password);
//第种方式第个参数确定类型
querysetParameter(username nameHibernateSTRING);
querysetParameter(password passwordHibernateSTRING);
List<Users> list = querylist();
for(Users users : list){
Systemoutprintln(usersgetId());
}