重点内容
1.count():统计选择对象的个数。
2.avg():计算选择属性的平均值。
3 .max():找出属性值得最大值。
4.min():找出属性值得最小值。
5.sum():计算出属性值得总和。
1.count():统计选择对象的个数。
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " select count(stu.address) from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
Object count = query.uniqueResult();
System.out.println("不同的地址总数=" + count);
}
2.avg():计算选择属性的平均值。
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " select avg(stu.sid) from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
Object aver = query.list();
System.out.println("1+2..+10的平均值为=" + aver);
}
3 .max():找出属性值得最大值。
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " select max(stu.sid) from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
Object aver = query.list();
System.out.println("1到10的最大值=" + aver);
}
4.min():找出属性值得最小值。
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " select min(stu.sid) from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
Object aver = query.list();
System.out.println("1到10的最小值=" + aver);
}
5.sum():计算出属性值得总和。
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " select sum(stu.sid) from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
Object aver = query.list();
System.out.println("1+2...+10的和=" + aver);
}
项目源码:
package cn.codekong.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity //表示该类是一个持久化类
@Table(name="test" ,schema="hibernate") //表示Students类映射到数据库中的对应的表名为t_StuInfo
public class Students{
@Id //声明主键
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int sid;
//@Column(name="姓名") //一般不使用,应为属性名对应的就是数据库中的字段名
private String sname;
private String gender;
private Date birthday;
private String major;
//
private String address;
public Students() {
}
public Students(String sname, Date birthday, String address) {
super();
this.sname = sname;
this.birthday = birthday;
this.address = address;
}
public Students(int sid, String sname, String gender, Date birthday, String major, String address) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.major = major;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", major=" + major + ", address=" + address + "]";
}
}
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.codekong.entity.Students;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
// 创建服务注册对象
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
// 创建会话工厂对象
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
// 会话对象
session = sessionFactory.openSession();
// 开启事物
transaction = session.beginTransaction();
}
@After
public void destory() {
// 提交事物
transaction.commit();
// 关闭会话
session.close();
// 关闭会话工厂
sessionFactory.close();
}
/*
* @Test // 向数据库中添加数据 public void testSaveStudents() { // 生成学生对象 Students
* student1 = new Students(1,"凯耐","男",new Date(),"计算机","长沙"); Students
* student2 = new Students(2,"赵四","男",new Date(),"小品","沈阳"); Students
* student3 = new Students(3,"范冰冰","女",new Date(),"表演系","长沙"); Students
* student4 = new Students(4,"周杰伦","男",new Date(),"歌手","*"); Students
* student5 = new Students(5,"张三","男",new Date(),"会计","武汉"); Students
* student6 = new Students(6,"小丽","女",new Date(),"金融","成都"); Students
* student7 = new Students(7,"程国君","男",new Date(),"计算机","北京"); Students
* student8 = new Students(8,"刘洋","女",new Date(),"土木","上海"); Students
* student9 = new Students(9,"赵磊","男",new Date(),"外贸","深圳"); Students
* student10 = new Students(10,"李刚","男",new Date(),"机械工程","株洲");
* session.save(student1); session.save(student2); session.save(student3);
* session.save(student4); session.save(student5); session.save(student6);
* session.save(student7); session.save(student8); session.save(student9);
* session.save(student10);
*
*
*
* }
*/
@Test
public void TestHqlQuery() {
// 1.创建hql语句
String hql = " from Students stu";
// 2.创建Query对象
Query query = session.createQuery(hql);
// 3.返回查询结果
// 3.返回查询结果,结果的类型由你查询的列数决定
List<Students> studentslist = query.list();
// 4.遍历结果集
for (Students student:studentslist) {
//get()方法中的数字以字符串的形式
System.out.println("姓名=" +student.getSname() );
System.out.println("性别=" + student.getBirthday());
System.out.println("专业=" + student.getAddress());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<!-- 配置方言是非常重要!!!我这里配置的为MySQL5Dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 配置C3P0连接池 -->
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.timeout"></property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 配置映射文件 -->
<!--<property name="hibernate.current_session_context_class">thread</property>-->
<mapping class="cn.codekong.entity.Students"/>
</session-factory>
</hibernate-configuration>