6月6日 SSH 周三

时间:2022-07-22 14:37:29
<?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.jdbc.Driver</property><!-- 驱动 -->
        <property name="connection.url">jdbc:mysql:///cms09</property><!-- 连接 -->
        <property name="connection.username">root</property><!-- 用户名 -->
        <property name="connection.password">root</property><!-- 密码 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 方言 -->

        <property name="hbm2ddl.auto">update</property><!-- 自动创建表 -->
        <property name="show_sql">true</property><!-- 打印sql语句 -->
        <property name="format_sql">true</property><!-- sql格式化 -->

        <mapping resource="com/dto/Cat.hbm.xml"/><!-- 映射 -->
    </session-factory>
</hibernate-configuration>
<?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.dto.Cat" table="d_cat">
        <id name="id" column="cid" type="java.lang.Integer">
            <generator class="native"></generator>
        </id>
        <property name="name" column="cname" type="java.lang.String"/>
        <property name="sex" column="csex" type="java.lang.String"/>
        <property name="age" column="cage" type="java.lang.Integer"/>
    </class>
</hibernate-mapping>
package com.dto;

public class Cat {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    public Cat() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Cat(Integer id, String name, String sex, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Cat [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
                + age + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }


}
package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class HibernateUtils {

    private static SessionFactory sessionFactory;

    static {
        Configuration config = new Configuration().configure();
        sessionFactory = config.buildSessionFactory();
    }

    public static Session getSession(){

        Session session = sessionFactory.openSession();

        return session;
    }
}
package com.demo;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.dto.Cat;
import com.util.HibernateUtils;

public class Demo {
    @Test
    public void test_show(){
        Session session = HibernateUtils.getSession();
        String hql=" from Cat ";
        List<Cat> list = session.find(hql);
        for (Cat c : list) {
            System.out.println(c);
        }
    }

    @Test
    public void test_save(){
        Session session = HibernateUtils.getSession();
        Cat c = new Cat(null,"小红","女",20);
        Transaction transaction = session.beginTransaction();
        session.save(c);
        transaction.commit();
    }

    @Test 
    public void test_update(){
        Session session = HibernateUtils.getSession();
        Cat c = new Cat(2,"小黑","男",20);
        Transaction transaction = session.beginTransaction();
        session.update(c);
        transaction.commit();
    }

    @Test
    public void test_delete(){
        Session session = HibernateUtils.getSession();
        Cat c = new Cat(4,"","",0);
        Transaction transaction = session.beginTransaction();
        session.delete(c);
        transaction.commit();
    }

    @Test
    public void test_saveOrUpdate(){
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Cat c = new Cat(null,"花花","女",20);
        session.saveOrUpdate(c);
        transaction.commit();
    }

    @Test
    public void test_get(){
        Session session = HibernateUtils.getSession();
        Cat c = (Cat) session.get(Cat.class, 2);
        session.close();
        System.out.println(c);
    }

    @Test
    public void test_load(){
        Session session = HibernateUtils.getSession();
        Cat c = (Cat) session.load(Cat.class, 3);
        System.out.println(c);
        session.close();
    }

}
package com.demo;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.dto.Cat;
import com.util.HibernateUtils;

public class Code {
    @Test
    public void test_list(){
        Session session = HibernateUtils.getSession();
        String hql =" from Cat ";
        Query query = session.createQuery(hql);
        List<Cat> list = query.list();
        for (Cat c : list) {
            System.out.println(c);
        }
    }

    @Test
    public void test_count(){
        Session session = HibernateUtils.getSession();
        String hql =" select count(*) from Cat ";
        Query query = session.createQuery(hql );
        String s = query.uniqueResult().toString();
        Integer count = Integer.valueOf(s);
        System.out.println(count);

    }

    @Test
    public void test_oneObject(){
        Session session = HibernateUtils.getSession();
        String hql = " from Cat where id = 2 ";
        Query query = session.createQuery(hql);
        Cat c = (Cat) query.uniqueResult();
        System.out.println(c);
    }

    @Test
    public void test_executeUpdate(){
        Session session = HibernateUtils.getSession();
        String hql = " update Cat set name='安安',sex='女',age=22 where id = 2 ";
        Query query = session.createQuery(hql);
        Transaction transaction = session.beginTransaction();
        int i = query.executeUpdate();
        transaction.commit();
        System.out.println(i);
    }
}

It does not matter how slowly you go as long as you do not stop.
走的慢不重要,重要的是你不停止。
投稿人小米同学。有时会发现身边的人比自己优秀好多,心里会惊慌。但是与其慌乱,不如控制自己的节奏,慢慢来,慢一点没关系,但千万不要停止努力。