Hibernate通过createSQLQuery( )方法实现增删改查

在这里插入代码片

二、
二、hibernate核心配置文件: hibernate.cfg.xml
[java] view plain copy

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置hibernate核心配置文件 -->
<hibernate-configuration>
    <!-- 配置hibernate数据源连接 -->
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="hibernate.connection.username">wzf</property>
        <property name="hibernate.connection.password">1234</property>
        <!-- 配置数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- 配置sql打印、格式化 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 配置hibernate映射文件位置 -->
        <mapping resource="com/gomai/pojo/student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
三、hrbernate的映射文件

[java] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置hibernate映射文件 -->
<hibernate-mapping>
    <class name="com.gomai.pojo.Student" table="student">
        <!-- 配置主键生成策略 -->
        <id name="student_id" column="STUID">
            <generator class="sequence">
                <param name="sequence">SQ_STUDENT</param>
            </generator>
        </id>
        <!-- 配置表与属性 -->
        <property name="student_name" column="stuname"></property>
        <property name="student_age" column="stuage"></property>
        <property name="student_sex" column="stusex"></property>
        <property name="student_no" column="stuno"></property>
    </class>
</hibernate-mapping>
四、测试类(该类包括增删改查四个方法的实现,下面依次介绍)
新增:
[java] view plain copy

/**
 * 添加:通过序列生成主键自增
     */
@Test
public void insertStu(){
    Configuration configure = new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sessionFactory = configure.buildSessionFactory();
    Session session = sessionFactory.openSession();
    //开启事务
    Transaction tr = session.beginTransaction();
    int i = session.createSQLQuery("insert into student values(SQ_STUDENT.nextval,?,?,?,?)")
        .setParameter(0, 2)
        .setParameter(1, "露娜")
        .setParameter(2, 23)
        .setParameter(3, "女")
        .setParameter(4, 1003)
        .executeUpdate();
    System.out.println(i);
    try {
        tr.commit();
    } catch (HibernateException e) {
        tr.rollback();
        e.printStackTrace();
    }finally{
        session.close();
    }
}
删除:
[java] view plain copy

/**
* 删除
*/
@Test
public void deleteStu(){
Configuration configure = new Configuration().configure(“hibernate.cfg.xml”);
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction tr = session.beginTransaction();
int i = session.createSQLQuery(“delete from student where stuid = ?”)
.setParameter(0, 100)
.executeUpdate();
System.out.println(“TestSQL.deleteStu()” + i);
//事务回滚、关流
try {
tr.commit();
} catch (Exception e) {
tr.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
更新:
[java] view plain copy

/**

  • 更新
    */
    @Test
    public void updateStu(){
    Configuration configure = new Configuration().configure(“hibernate.cfg.xml”);
    SessionFactory sessionFactory = configure.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tr = session.beginTransaction();
    int i = session.createSQLQuery("update student set stuname = ?,stusex = ? where stuid = ? ")
    .setParameter(0, “公孙离”)
    .setParameter(1, “女”)
    .setParameter(2, 9)
    .executeUpdate();
    System.out.println(i);
    //事务回滚、关流
    try {
    tr.commit();
    } catch (Exception e) {
    tr.rollback();
    e.printStackTrace();
    }finally{
    session.close();
    }
    }
    查询:
    [java] view plain copy

/**

  • 查询
    */
    @Test
    public void searchStu(){
    Configuration configure = new Configuration().configure(“hibernate.cfg.xml”);
    SessionFactory sessionFactory = configure.buildSessionFactory();
    Session session = sessionFactory.openSession();
    List list = session.createSQLQuery(“select * from student”)
    .addEntity(Student.class)
    .list();
    for (Student student : list) {
    System.out.println(student);
    }
    session.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值