hibernate 非xml实体类配置方法!
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&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实体类配置方法!的更多相关文章
- EF实体类配置总结
实体类配置总结 Entity Framework 6 Code First 实践系列(1):实体类配置总结 2014-03-25 12:58 by TJerry, 719 阅读, 6 评论, 收藏, ...
- 在Intellij IDEA下通过Hibernate逆向生成实体类
前言:在IDEA中,通过相关插件,可以利用Hibernate逆向生成数据表对应的实体类.具体操作及注意事项见本篇随笔. 1.创建一个基于maven的hibernate工程.并在工程中添夹hiberna ...
- EntityFramework 系列:实体类配置-根据依赖配置关系和关联
EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...
- Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联
EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...
- 【转】Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联
本文转自:http://www.cnblogs.com/easygame/p/3622893.html EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关 ...
- 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法
问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置
笔记 7.接口配置文件自动映射到属性和实体类配置 简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解: ...
- hibernate.cfg.xml文件的配置模板和不同数据库的配置參数
(1)hibernate.cfg.xml文件的配置模板 <?xml version="1.0" encoding="UTF-8"?> <!DO ...
- hibernate 反向生实体类 and 为什么老是多一个id
hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548
随机推荐
- C语言经典程序190例
[程序1] 题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,及809*??后的结果. 1.程序分析: 2.程序 ...
- css案例学习之div a实现立体菜单
效果 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- HDOJ-1010 Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...
- Jekins安装
1. Jekins支持多系统:windows, mac,linux 2. Jekins安装方式有三种:直接war文件安装,安装包安装,将war文件放到web容器安装 3. 在windows下安装 a. ...
- SDK调试出错小技巧=。=
学习Unity小伙伴完全不懂Android编程抓错误是很困难的..... 1. 使用UnityPlayer.UnitySendMessage(); 发送到Unity使用OnGUI显示错误 2. 直接在 ...
- IOS 下雪动画修改版本
#define SNOW_IMAGENAME @"snow" #define IMAGE_X arc4random()%(int)Main_Screen_Width #define ...
- poj1664 放苹果(递归)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1664 ------ ...
- dl dt dd标签
<dl>标记定义了一个定义列表,定义列表中的条目是通过使用<dt>标记(“definition title”,定义标题)和<dd>标记(“definition de ...
- Java之可变参数
Java中支持可变参数 意思就是:参数的个数可以根据需要写,你可以写1个.2个.3个....他们都被保存到一个参数的数组中. 但是这些参有一些约束:他们必须是同类型的,比如都是String字符串类型. ...
- PHP学习笔记二
<?php //布尔类型 $a=true; //值不区分大小写 $b=false;//整型0,浮点数0.0,空字符串,不包括任何元素的数组,空对象,null都表示false $c=0; if($ ...