没有合适的资源?快使用搜索试试~ 我知道了~
使用hibernate注解不用写配置文件,此文档包括hibernate的全部注解,欢迎下载
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![xls](https://img-home.csdnimg.cn/images/20210720083658.png)
![](https://csdnimg.cn/release/download_crawler_static/5292258/bg1.jpg)
Hibernate 注解编程
<!-- EJB注解编程 -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">imti</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping class="com.xmxz.hibernate.annotation.IdCard" />
<mapping class="com.xmxz.hibernate.annotation.Person" />
</session-factory>
</hibernate-configuration>
<!—XML映射 -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">imti</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/xmxz/hibernate/mobile/Operator.hbm.xml" />
<mapping
resource="com/xmxz/hibernate/mobile/MobileUser.hbm.xml" />
<mapping
resource="com/xmxz/hibernate/mobile/FillMoneyCard.hbm.xml" />
<mapping resource="com/xmxz/hibernate/mobile/SPConst.hbm.xml" />
<mapping resource="com/xmxz/hibernate/mobile/MobileSP.hbm.xml" />
<mapping
resource="com/xmxz/hibernate/mobile/FillMoneyHistory.hbm.xml" />
<mapping resource="com/xmxz/hibernate/mobile/Message.hbm.xml" />
</session-factory>
第 1 页 共 23 页
![](https://csdnimg.cn/release/download_crawler_static/5292258/bg2.jpg)
</hibernate-configuration>
ExportDB
public class ExportDB {
public static void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration() .configure("");
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
HibernateUtils
public class HibernateUtils {
private static SessionFactory factory;
static {// 单例模式
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
/**
* 获取当前的会话工厂
* @return
*/
public static SessionFactory getSessionFactory() {
return factory;
}
/**
* 获取当前的会话
* @return
*/
public static Session getSession() {
return factory.openSession();
}
/**
* 关闭会话(session)
* @param session当前的会话
*/
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
第 2 页 共 23 页
![](https://csdnimg.cn/release/download_crawler_static/5292258/bg3.jpg)
ExportDBAnnotation
public class ExportDBAnnotation {
public static void main(String[] args) {
// 读取hibernate.cfg.annotation.xml文件
Configuration cfg = new AnnotationConfiguration()
.configure("hibernate.cfg.annotation.xml");
SchemaExport export = new SchemaExport(cfg);
// export.setFormat(true);
// 设置保存SQL脚本的文件名
// export.setOutputFile("new.sql");
// 输出SQL脚本,并执行SQL脚本
export.create(true, true);
}
}
HibernateAnnotationUtils
/**
* 使用EJB注解做的
* @author Administrator
*/
public class HibernateAnnotationUtils {
private static SessionFactory factory;
static {// 单例模式
try {
Configuration cfg = new AnnotationConfiguration()
.configure("hibernate.cfg.annotation.xml");
factory = cfg.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
第 3 页 共 23 页
![](https://csdnimg.cn/release/download_crawler_static/5292258/bg4.jpg)
}
}
}
【一对一】关系映射
模型:人 和 身份证 通过人可以得知身份证号码,根据身份证号码可以得知人
Person
/**
* 人(通过外键实现一对一关系)
* @author Administrator
*/
// 屏蔽缺少“serialVersionUID”定义警告
@SuppressWarnings("serial")
// 指明实体Bean名,默认值是类名
@Entity(name = "Person")
// 实体对应的数据表 name表名;uniqueConstraints唯一约束性;主键
@Table(name = "t_person", uniqueConstraints = { @UniqueConstraint(columnNames = {
"id" }) })
public class Person implements Serializable {
private Integer id;
private String name;
private IdCard idCard;
@Id
// 主键唯一标识
@Column(name = "id")
// 字段名
@GeneratedValue(strategy = GenerationType.AUTO)
// 自动增加
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 16, nullable = false)
//name字段名,length字段最大字符长度,nullable是否允许为空,precision字段精度,scale小数位
数
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//optional是否允许该字段为null,该属性应该根据数据库表的外键约束来确定,默认为true
@JoinColumn(name = "cardID")
@OneToOne(cascade = CascadeType.ALL, optional = false)
public IdCard getIdCard() {
return idCard;
}
第 4 页 共 23 页
![](https://csdnimg.cn/release/download_crawler_static/5292258/bg5.jpg)
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard
/**
* 身份证(通过外键实现一对一关系)
* @author Administrator
*/
// 屏蔽缺少“serialVersionUID”定义警告
@SuppressWarnings("serial")
// 指明实体Bean名,默认值是类名
@Entity(name = "IdCard")
// 实体对应的数据表 name表名;uniqueConstraints唯一约束性;主键
@Table(name = "t_idcard", uniqueConstraints = { @UniqueConstraint(columnNames = {
"id" }) })
public class IdCard implements Serializable {
private Integer id;
private String cardNo;
private Person person;
@Id
// 主键唯一标识
@Column(name = "id")
// 字段名
@GeneratedValue(strategy = GenerationType.AUTO)
// 自动增加
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "cardNo", length = 32, nullable = false)
//name字段名,length字段最大字符长度,nullable是否允许为空,precision字段精度,scale小数位
数
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
// 表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy =
"idCard")
public Person getPerson() {
return person;
}
第 5 页 共 23 页
剩余22页未读,继续阅读
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
ch593522217
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)