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. C++ 11学习(1):lambda表达式

    转载请注明,来自:http://blog.csdn.net/skymanwu #include <iostream> #include <vector> #include &l ...

  2. HTML5视音频小结

    目前,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件.HTML5 规定了一种通过 video 元素来包含视频的标准方法.当前HTML5只支持三种格式的视频. 格 ...

  3. HDOJ-1009 FatMouse' Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=1009 # include <stdio.h> # include <algorithm> ...

  4. REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件

    使用安装Linux系统时的光盘镜像来安装软件 (1)以虚拟机上,安装mysql为例: 查看mysql是否安装 rpm -qa|grep -i mysql    显示下面,证明mysql已安装客户端,下 ...

  5. js点击事件防止用户重复点击执行

    点击事件里给button标签加一个自定义属性,存上次点击时间 追问: 求详细代码,JS 真心的没怎么做过 追答:   <input type="button" id=&quo ...

  6. Python 列表生成式、生成器、迭代器

    列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 如果要生成[1x1, 2x2, 3x3, ..., 10x10]怎么 ...

  7. Cable master(好题,二分)

    Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. 【二分答案】【POJ3122】【Northwestern Europe 2006】Pie

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10629   Accepted: 3744   Special Ju ...

  9. 安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)

    原因是fileinfo这个函数在编译时非常消耗内存,而系统内存又不够了,所以才会出现此问题. 网上找了方法: 1,关闭其他占用大内存的进程. 2,在编译是添加参数 --disable-fileinfo

  10. 判断网络是否连接Internet

    添加 system32.Management 引用 private bool ListenNET()        {            ManagementObjectSearcher s = ...