hibernate 非xml实体类配置方法!

这个是hibernate.cfg.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"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/cms?useUnicode=true&amp;characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- test2 注入Entity.class-->
<mapping class="com.bird.entity.JcChannel"/>
<mapping class="com.bird.entity.JcChannelExt"/>
<mapping class="com.bird.entity.JcChnlGroupContri"/>
<mapping class="com.bird.entity.JcSiteFlow"/>
<mapping class="com.bird.entity.ITest"/> </session-factory> </hibernate-configuration>

其中          <mapping  class="com.bird.entity.ITest"/>   指向类名。

下面是这个类的代码,其中用了ITest注解。

package com.bird.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name = "i_test")
public class ITest implements Serializable { private static final long serialVersionUID = 1L;
private int id;
private String name;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

最重要的是工具类 HibernateSessionFactory.java  ,

因为采用了 Configuration configuration = new AnnotationConfiguration();  这段代码创建Configuration ,所以才能不用去写实体类的xml配置文件了。

记住这个类:new AnnotationConfiguration();

(这个类要用     session.beginTransaction().commit();  提交请求!)

TestUti.java   测试类代码,含有删改查功能

package com.bird.channel;

import java.util.Date;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session; import com.bird.entity.JcChannel;
import com.bird.entity.JcChannelExt;
import com.bird.entity.JcChnlGroupContri;
import com.bird.entity.JcSiteFlow;
import com.bird.util.HibernateSessionFactory; public class TestUtil {
//查询的例子
public List<JcChannel> getChannelList() {
Session session = HibernateSessionFactory.getSession();
String hql = "from JcChannel where parent_id is not null ";
Query query = session.createQuery(hql);
List<JcChannel> jchList = (List<JcChannel>) query.list();
for (int i = 0; i < jchList.size(); i++) {
JcChannel jcEn = jchList.get(i);
System.out.println(jcEn.getChannel_id());
}
return jchList;
}
//查询最大id的例子
public int getChannelMaxIdByHql() {
int id = 0;
Session session = HibernateSessionFactory.getSession();
String hql = "select max(channel_id) from JcChannel";
Query query = session.createQuery(hql);
List jchList = query.list();
if (jchList.size() > 0) {
id = Integer.parseInt(jchList.get(0).toString());
}
return id;
}
//查询最大id的例子
public int getChannelMaxIdBySql() {
int id = 0;
Session session = HibernateSessionFactory.getSession();
String hql = "select max(channel_id) from jc_channel ";
Query query = session.createSQLQuery(hql);
List jchList = query.list();
if (jchList.size() > 0) {
id = Integer.parseInt(jchList.get(0).toString());
}
return id;
}
// 修改的例子
public int updateChannelMaxId() {
Session session = HibernateSessionFactory.getSession();
String hql = "update jc_channel t set t.rgt = 2 ";
Query query = session.createSQLQuery(hql);
int over = query.executeUpdate();
session.beginTransaction().commit();
return over;
}
// 删除的例子
public int deleteChannelMaxId(int id) {
Session session = HibernateSessionFactory.getSession();
String hql = "delete from jc_channel where channel_id = ? ";
Query query = session.createSQLQuery(hql).setParameter(0, id);
int over = query.executeUpdate();
session.beginTransaction().commit();
return over;
} }

hibernate 非xml实体类配置方法!的更多相关文章

  1. EF实体类配置总结

    实体类配置总结 Entity Framework 6 Code First 实践系列(1):实体类配置总结 2014-03-25 12:58 by TJerry, 719 阅读, 6 评论, 收藏,  ...

  2. 在Intellij IDEA下通过Hibernate逆向生成实体类

    前言:在IDEA中,通过相关插件,可以利用Hibernate逆向生成数据表对应的实体类.具体操作及注意事项见本篇随笔. 1.创建一个基于maven的hibernate工程.并在工程中添夹hiberna ...

  3. EntityFramework 系列:实体类配置-根据依赖配置关系和关联

    EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...

  4. Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联

    EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...

  5. 【转】Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联

    本文转自:http://www.cnblogs.com/easygame/p/3622893.html EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关 ...

  6. 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法

    问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...

  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置

    笔记 7.接口配置文件自动映射到属性和实体类配置     简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解:        ...

  8. hibernate.cfg.xml文件的配置模板和不同数据库的配置參数

    (1)hibernate.cfg.xml文件的配置模板 <?xml version="1.0" encoding="UTF-8"?> <!DO ...

  9. hibernate 反向生实体类 and 为什么老是多一个id

    hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548

随机推荐

  1. 仿新浪微博短网址PHP实现方案

    微博限制140字,但是我们知道有时需要分享一个类似淘宝商品的链接,很长,为了避免这个问题,所有了短网址的概念,废话不多说,直接把我的实现方案分享一下: 1)将长网址md5生成32位签名串,分为4段, ...

  2. 关于找不到stdafx.h头文件问题

    代码: #include "stdafx.h" #include "stdlib.h" char* getcharBuffer() { return " ...

  3. UESTC_冰雪奇缘 CDOJ 843

    艾莎女王又开始用冰雪魔法盖宫殿了. 她决定先造一堵墙,于是释放魔法让形为直角梯形的冰砖从天而降,定入冻土之中. 现在你将回答女王的询问:某段冻土上冰砖的面积. 注:多块冰砖之间会互相重叠,重叠部分要多 ...

  4. ubuntu 硬件系统信息

    查看ubuntu硬件信息 1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用命令 dmidecode | ...

  5. Impala 4、Impala JDBC

    • 配置: – impala.driver=org.apache.hive.jdbc.HiveDriver – impala.url=jdbc:hive2://node2:21050/;auth=no ...

  6. LightOJ 1338 && 1387 - Setu && LightOJ 1433 && CodeForces 246B(水题)

    B - B Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status P ...

  7. 使用disqus搭建comment时一件非常二的事

    近期在github 上面搭建自己的博客,搭建comment部分的时候出现了一个问题:配置都配置好了,可是comment就是不成功.昨天为这个问题折腾了了半晚上没找出原因,今天晚上我突然发现一个地方设置 ...

  8. QUdpSocket Class

    翻译自:QT官网文档QUdpSocket类 QUdpSocket类提供一个UDP套接字. Header: #include <QUdpSocket> qmake: QT += networ ...

  9. Core Bluetooth【官方文档翻译】【02】

    1.中心设备和外围设备以及它们在蓝牙通讯中的角色. 在所有的BLE( Bluetooth low energy,下文简称蓝牙4.0 )通讯中都涉及2个主要的角色:中心设备和外围设备.它是基于传统的客户 ...

  10. (转)MVC语法-@helpers和@functions(Razor内定义函数)

    (转)MVC语法-@helpers和@functions(Razor内定义函数) 转自:http://www.mikesdotnetting.com/Article/173/The-Differenc ...