在IDEA中创建hibernate及对hibernate进行CRUD操作

博主链接:https://www.cnblogs.com/xmai/p/8124383.html(自动根据已经存在的数据库表生成Hibernate实体与xml)

博主链接:https://www.cnblogs.com/yangyquin/p/5438248.html(Hibernate与struts2整合)

踩坑记录:

1、mysql-connector-java 包的版本一定要导对,否则会报错:java.sql.SQLException: Unknown system variable 'query_cache_size'

2、@Test测试是import org.junit.Test,而我导入另外一个类了,所以总是报错,这是对不够细心的惩罚,所以记得类也是要对应的

3、依照博客测试练习,一定要细心+认真,否则就会卡在博主的某一步上,浪费掉大量的时间

代码测试:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/rushi</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">Sys935269</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <mapping resource="com/practice/data/entities/OrdersEntity.hbm.xml"/>
        <mapping class="com.practice.data.entities.OrdersEntity"/>
        <mapping resource="com/practice/data/entities/EmployeeEntity.hbm.xml"/>
        <mapping class="com.practice.data.entities.EmployeeEntity"/>
        <mapping resource="com/practice/data/entities/JobEntity.hbm.xml"/>
        <mapping class="com.practice.data.entities.JobEntity"/>


        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

 

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

import org.junit.Before;
import org.junit.Test;


public class TestHibernate {
    Configuration configuration = null;
    SessionFactory sessionFactory = null;
    Session session = null;
    Transaction tx = null;

    @Before
    public void init() {
        configuration = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = configuration.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

    }

    /**
     * 添加
     */
    @Test
    public void insert() {
        JobEntity jobEntity = new JobEntity();
        jobEntity.setId(5);
        jobEntity.setJob(2017);
        jobEntity.setJobName("大数据");
        session.save(jobEntity);
    }

    /**
     * 查询
     */
    @Test
    public void select() {
        JobEntity jobEntity = session.get(JobEntity.class, new Integer(4));
        tx.commit();
        session.close();
        System.out.println("id号:" + jobEntity.getId() + ",job号" + jobEntity.getJob() + ",工作专业:" + jobEntity.getJobName());
    }

    /**
     * 查询
     */
    @Test
    public void delect() {
        JobEntity jobEntity = session.get(JobEntity.class, new Integer(5));
        session.delete(jobEntity);
        tx.commit();
        session.close();
    }

    /**
     * 修改
     * 修改之后用save和update方法都可以的
     */
    @Test
    public void update() {
        JobEntity jobEntity = session.get(JobEntity.class, new Integer(4));
//        jobEntity.setJobName("我是谁");
//        session.save(jobEntity);
//        tx.commit();
//        session.close();
        jobEntity.setJobName("改回来");
        session.update(jobEntity);
        tx.commit();
        session.close();
    }

}

 

posted @ 2019-06-02 13:46  绛珠仙草林黛玉  阅读(456)  评论(0编辑  收藏  举报