Hibernate实战——定制SQL

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate-configuration是配置文件的根元素 -->
<hibernate-configuration>
     <session-factory>
           <!-- 指定连接数据库所用的驱动 -->
           <property  name="connection.driver_class">com.mysql.jdbc.Driver</property>
           <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
           <property  name="connection.url">jdbc:mysql://localhost/hibernate</property>
           <!-- 指定连接数据库的用户名 -->
           <property name="connection.username">root</property>
           <!-- 指定连接数据库的密码 -->
           <property name="connection.password">32147</property>
           <!-- 指定连接池里最大连接数 -->
           <property  name="hibernate.c3p0.max_size">20</property>
           <!-- 指定连接池里最小连接数 -->
           <property name="hibernate.c3p0.min_size">1</property>
           <!-- 指定连接池里连接的超时时长 -->
           <property  name="hibernate.c3p0.timeout">5000</property>
           <!-- 指定连接池里最大缓存多少个Statement对象 -->
           <property  name="hibernate.c3p0.max_statements">100</property>
           <property  name="hibernate.c3p0.idle_test_period">3000</property>
           <property  name="hibernate.c3p0.acquire_increment">2</property>
           <property  name="hibernate.c3p0.validate">true</property>
           <!-- 指定数据库方言 -->
           <property  name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
           <!-- 根据需要自动创建数据库 -->
           <property name="hbm2ddl.auto">update</property>
           <!-- 显示Hibernate持久化操作所生成的SQL -->
           <property name="show_sql">true</property>
           <!-- 将SQL脚本进行格式化后再输出 -->
           <property name="hibernate.format_sql">true</property>
           <!-- 罗列所有持久化类的类名 -->
           <mapping class="org.crazyit.app.domain.News"/>
     </session-factory>
</hibernate-configuration>

二 PO

package org.crazyit.app.domain;

import javax.persistence.*;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Loader;

// 定制insert的SQL语句
@SQLInsert(sql="insert into news_inf(content , title) values(upper(?), ?)")
// 定制update的SQL语句
@SQLUpdate(sql="update news_inf set content=upper(?), title=? where news_id=?")
// 定制delete的SQL语句
@SQLDelete(sql="delete from news_inf where news_id=?")
// 定制删除所有实体的SQL语句
@SQLDeleteAll(sql="delete from news_inf")
// 指定使用news_loader命名查询作为定制查询的查询语句
@Loader(namedQuery = "news_loader")
// 定义一个命名SQL查询
@NamedNativeQuery(name="news_loader"
    , query="select news_id , concat('===' , concat(title , '===')) as title"
        + " , content from news_inf n where news_id=?"
    , resultClass = News.class)
@Entity
@Table(name="news_inf")
public class News
{
    @Id @Column(name="news_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    private String title;
    private String content;

    // id的setter和getter方法
    public void setId(Integer id)
    {
        this.id = id;
    }
    public Integer getId()
    {
        return this.id;
    }

    // title的setter和getter方法
    public void setTitle(String title)
    {
        this.title = title;
    }
    public String getTitle()
    {
        return this.title;
    }

    // content的setter和getter方法
    public void setContent(String content)
    {
        this.content = content;
    }
    public String getContent()
    {
        return this.content;
    }

}

三 测试

package lee;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;

import org.crazyit.app.domain.*;

public class NewsManager
{
    public static void main(String[] args) throws Exception
    {
        // 实例化Configuration,这行代码默认加载hibernate.cfg.xml文件
        Configuration conf = new Configuration().configure();
        // 以Configuration实例创建SessionFactory实例
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(conf.getProperties()).build();
        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
        // 实例化Session
        Session sess = sf.openSession();
        // 开始事务
        Transaction tx = sess.beginTransaction();
        // 创建消息实例
        News n = new News();
        // 设置消息标题和消息内容
        n.setTitle("疯狂Java联盟成立了");
        n.setContent("adcb");
        // 保存消息
        sess.save(n);
        News n2 = (News)sess.get(News.class, 1);
        System.out.println(n2.getTitle());
        n2.setContent("aaaaaaaa");
        // 提交事务
        tx.commit();
        // 关闭Session
        sess.close();
    }
}

四 测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值