java 非xml实体类_hibernate 非xml实体类配置方法!

hibernate 非xml实体类配置方法!

这个是hibernate.cfg.xml配置文件

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

jdbc:mysql://localhost:3306/cms?useUnicode=true&characterEncoding=UTF-8

org.hibernate.dialect.MySQLDialect

root

1234

com.mysql.jdbc.Driver

View Code

其中             指向类名。

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bird.entity;importjava.io.Serializable;importjavax.persistence.Entity;importjavax.persistence.Id;importjavax.persistence.Table;

@Entity

@Table(name= "i_test")public class ITest implementsSerializable {private static final long serialVersionUID = 1L;private intid;privateString name;

@Idpublic intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

View Code

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

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

记住这个类:new AnnotationConfiguration();

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.bird.channel;importjava.util.Date;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importcom.bird.entity.JcChannel;importcom.bird.entity.JcChannelExt;importcom.bird.entity.JcChnlGroupContri;importcom.bird.entity.JcSiteFlow;importcom.bird.util.HibernateSessionFactory;public classTestUtil {//查询的例子

public ListgetChannelList() {

Session session=HibernateSessionFactory.getSession();

String hql= "from JcChannel where parent_id is not null ";

Query query=session.createQuery(hql);

List jchList = (List) query.list();for (int i = 0; i < jchList.size(); i++) {

JcChannel jcEn=jchList.get(i);

System.out.println(jcEn.getChannel_id());

}returnjchList;

}//查询最大id的例子

public intgetChannelMaxIdByHql() {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());

}returnid;

}//查询最大id的例子

public intgetChannelMaxIdBySql() {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());

}returnid;

}//修改的例子

public intupdateChannelMaxId() {

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();returnover;

}//删除的例子

public int deleteChannelMaxId(intid) {

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();returnover;

}

}

View Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值