分别使用Criteria与HQL进行分组、时间排序查询

分别使用Criteria与HQL进行分组、时间排序查询

Criteria

 public static void main(String[] args) {
        //实例化对象
        UserEntity userEntity = new UserEntity();

        //配置
        Configuration configuration = new Configuration().configure();

        //创建会话工厂
        SessionFactory sessionFactory = configuration.buildSessionFactory();

        //创建会话
        Session session = sessionFactory.openSession();

        //创建事务
        Transaction transaction = session.beginTransaction();

        //创建criteriaBuilder
        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();

        //创建CriteriaQuery
        CriteriaQuery<UserEntity> query = criteriaBuilder.createQuery(UserEntity.class);

        //定义root
        Root<UserEntity> root = query.from(UserEntity.class);

        //排序
        Order order = criteriaBuilder.desc(root.get("time"));

        List<UserEntity> list = session.createQuery(query.groupBy(root.get("userName")).orderBy(order)).getResultList();

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUserName());
        }
        transaction.commit();


    }

hql


```java
public class hqlTest {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();

        SessionFactory sessionFactory = configuration.buildSessionFactory();

        Session session = sessionFactory.openSession();

        Transaction transaction =session.beginTransaction();

        UserEntity userEntity = new UserEntity();

        Query<UserEntity> query = session.createQuery(" from UserEntity u group by userName order by time");

        List<UserEntity> list = query.getResultList();

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUserName());
        }
    }
}

 hibernate.hbm.xml

```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/huashy?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!--配置方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--配置sql控制台显示-->
        <property name="hibernate.show_sql">true</property>
        <!--配置格式-->
        <property name="format_sql">true</property>

        <mapping resource="com/hbm/user.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

user.hbm.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.entity.UserEntity" table="usera">
        <id name="id" column="id" type="java.lang.Integer"/>
        <property name="userName" column="user_name" type="java.lang.String"/>
        <property name="userPassword" column="user_password" type="java.lang.String"/>
        <property name="time" column="time" type="java.lang.String"/>
    </class>
</hibernate-mapping>

实体类

public class UserEntity {


    public UserEntity(){

    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    private String time;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    private int id;
    private String userName;

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    private String userPassword;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值