用hibernate建表添数据
来源:互联网 发布:软件体系结构有哪些 编辑:程序博客网 时间:2024/05/16 00:29
在src上见film取名:hibenate.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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=test
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size"
>1</property> -->
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class"
>thread</property> -->
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class"
>org.hibernate.cache.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto"
>update</property>-->
<property name="myeclipse.connection.profile">sql</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration
>
com.person包里建class
Student.java
package com.bjsxt.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
在同一包里新建film文件取名Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjsxt.hibernate.model">
<class name="Student" table="student">
<id name="id">
</id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
编写测试类
StudentTest.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.bjsxt.hibernate.model.Student;
public class StudentTest {
/**
* @param args
*/
private static SessionFactory sessionFactory;
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建表
Configuration cofn=new Configuration().configure();
SchemaExport dbExport=new SchemaExport(cofn);
//dbExport.setOutputFile("sql_out_lib//sql.txt");
dbExport.create(true, true);
//向表里添加数据
Student s=new Student();
s.setId(1);
s.setName("S1");
s.setAge(1);
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
提示:要添加hibernate相应的数据包,特别是sqljdbc.jar(数据库驱动包)
完成!
- 用hibernate建表添数据
- 用Hibernate插入大量数据
- 用hibernate对递归数据的操作
- 用hibernate对递归数据的操作
- 用hibernate对递归数据的操作
- 用 hibernate 修改数据的奇怪问题。
- 用hibernate中的session方法删除数据
- Hibernate学习:Hibernate数据查询
- 用hibernate跨数据平台将数据存入数据库中
- hibernate更新数据问题
- 使用Hibernate处理数据
- 使用Hibernate处理数据
- Hibernate之数据检索- -
- 使用Hibernate处理数据
- hibernate插入数据
- Hibernate数据加载方式
- Hibernate 脏数据检查
- Hibernate之数据缓存
- ubuntu10下Eclipse中无法输入中文
- 初学 xml (命名)
- 敏捷的三种误区和五种改进
- 网络交换机、路由器、硬件防火墙的理解
- 云计算
- 用hibernate建表添数据
- 综合布线的结构
- linux sed命令使用总结
- AIX下如何重启WAS和已经部署的应用程序服务器
- 菜鸟看鸟哥的Linux私房菜(基础篇)
- 寻址能力
- SOA(一)、了解SOA
- poj 1089 Intervals
- 广播信道具有反馈特性