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. Windows7安装SQL Server 2008图解

    不知是什么时候,把这篇博客给删除了,今天才发现,想恢复好像又不行,所以重新发布一下吧! 这几天因为需要,一直想安装SQL Server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天 ...

  2. 2015第10周三jquery ui position

    jQuery UI API - .position() 所属类别 方法重载(Method Overrides) | 方法(Methods) | 实用工具(Utilities) 用法 描述:相对另一个元 ...

  3. Php开发官方IDE ZEND

    From http://www.zend.com/en/products/studio 注:唯一的缺点是收费.

  4. LeeCode-Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. Mod_python: The Long Story

    mod_python: the long story - Grisha Trubetskoy Mod_python: The Long Story Oct 25th, 2013 | Comments ...

  6. Ffmpeg和SDL如何同步音频

    ong> 同步音頻 现在我们已经有了一个比较像样的播放器.所以让我们看一下还有哪些零碎的东西没处理.上次,我们掩饰了一点同步问题,也就是同步音频到视频而不是其它的同步方式.我们将采用和视频一样的 ...

  7. WPF拖动总结[转载]

    WPF拖动总结   这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...

  8. 并查集+二分-hdu-4750-Count The Pairs

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4750 题目大意: 给一无向图,n个点,m条边,每条边有个长度,且不一样.定义f(i,j)表示从节点i ...

  9. 使用Marshal.Copy把Txt行数据转为Struct类型值

    添加重要的命名空间: using System.Runtime.InteropServices; 先建立结构相同(char长度相同)的Struct类型用于转换: [StructLayout(Layou ...

  10. js库开发--参数传递及方法修改

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...