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
随机推荐
- QPainter就是手里的作图工具,只需要三洋东西:笔(颜色,宽度,样式),字体(写字),刷子(大面积作画),这里有三个典型例子
设置笔和字体以后,就可以写字了: void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter ...
- tomcat配置访问日志,访问首页主目录
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ...
- 安全运维之:Linux系统账户和登录安全
一.合理使用Shell历史命令记录功能 在Linux下可通过history命令查看用户所有的历史操作记录,同时shell命令操作记录默认保存在用户目录下 的.bash_history文件中,通过这个文 ...
- HTML5 Canvas Cheat Sheet
HTML5 Canvas Cheat Sheet HTML5 Canvas Cheat Sheet v1.x
- 有一种acm题目叫做,奇葩!
本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pi ...
- vim中对文本的选择
本文主要解说vim中对文本的选择,vim中选择文本分为: (1)选择字符 ---- 命令行模式下输入小写v (2)选择行 ---- 命令行模式下输入大写V (3)选择块 ---- ...
- Chord算法实现具体
背景 Chord算法是DHT(Distributed Hash Table)的一种经典实现.下面从网上无节操盗了一段介绍性文字: Chord是最简单.最精确的环形P2P模型."Chord&q ...
- CentOS6.5下解压文件.tar.gz .war .zip
解压.tar.gz文件: tar -zxvf web.tar.gz tar不支付解压文件到指定的文件夹! 解压.war .zip文件到指定文件夹: unzip web.war -d webapps/R ...
- windows下搭建及配置mantis缺陷管理工具
在windows XP 操作系统下,如何更快.更容易地搭建及配置mantis缺陷管理工具呢?以下是我实践的具体步骤: 一.安装mantis的前提环境,需要先安装Apache HTTP Server2. ...
- NET基础课--JIT编译器如何工作1
1..Net运行时调用JIT编译器,用来把由C#编译器生成的IL指令编译成机器代码.这一任务在应用程序的运行期间是分步进行的.JIT并不是在程序一开始就编译整个应用程序,取而代之的是,CLR是一个函数 ...