首页 技术 正文
技术 2022年11月13日
0 收藏 579 点赞 3,742 浏览 1957 个字

1,导入hibernate 的jar包,c3p0jar包

2,创建和表关联的实体类,创建关联实体类的配置文件

package com.entity;public class News {    private Integer id;    private String title;    private String content;    public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping
package="com.entity"> <class name="News" table="news_table">
<id name="id"><generator class="identity"/></id>
<property name="title"/>
<property name="content"/>
</class><!-- <class name="Parent">
<id name="name"/>
<list name="children" fetch="subselect" cascade="persist, delete">
<key column="parentName" not-null="true"/>
<list-index column="loc"/>
<one-to-many class="Child"/>
</list>
<list name="moreChildren" table="ParentChild" fetch="subselect">
<key column="parentName" not-null="true"/>
<list-index column="loc"/>
<many-to-many class="Child" column="childName"/>
</list>
</class> --></hibernate-mapping>

然后吗,自己看注释去…

package com.hirbernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import com.entity.News;public class NewsManager { @SuppressWarnings("deprecation")
public static void main(String[] args) {
//默认加载src下hibernate.cfg.xml文件,不行就自己另行指定,我还是自己来吧
Configuration configuration = new Configuration().configure("config/hibernate.cfg.xml");
//用configuration创建SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//创建session
Session session =sessionFactory.openSession();
//开始事务
Transaction trans = session.beginTransaction();
//创建消息实例
News news = new News();
//设置实例值
news.setTitle("ddd");
news.setContent("ddddd");
session.save(news); //提交当前事务
trans.commit();
//关闭session
session.close();
//关闭sessionfactory
sessionFactory.close();
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290