`
gordon20082008
  • 浏览: 25488 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

捣鼓ssh上手的例子-----hibernate

    博客分类:
  • SSH
阅读更多
hibernate的
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.username">root</property>
   <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="myeclipse.connection.profile">MySql</property>
   <property name="connection.password">root</property>
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="show_sql">true</property>
   <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
   <mapping resource="com/study/hibernate/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>


Person.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">
<!-- 
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.study.hibernate.Person" table="person"
   catalog="test">
   <id name="id" type="java.lang.String">
    <column name="id" length="32" />
    <generator class="assigned" />
   </id>
   <property name="name" type="java.lang.String">
    <column name="name" length="20" not-null="true" />
   </property>
   <property name="password" type="java.lang.String">
    <column name="password" length="20" not-null="true" />
   </property>
   <property name="sex" type="java.lang.String">
    <column name="sex" length="2" />
   </property>
   <property name="email" type="java.lang.String">
    <column name="email" length="30" />
   </property>
</class>
</hibernate-mapping>



package com.study.hibernate;

public class Person {
private String id;

private String name;

private String password;

private String sex;

private String email;

public String getEmail() {
   return email;
}

public void setEmail(String email) {
   this.email = email;
}

public String getId() {
   return id;
}

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

public String getName() {
   return name;
}

public void setName(String name) {
   this.name = name;
}

public String getPassword() {
   return password;
}

public void setPassword(String password) {
   this.password = password;
}

public String getSex() {
   return sex;
}

public void setSex(String sex) {
   this.sex = sex;
}

}


package com.study.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class PersonOperate {
private Session session = null;

public PersonOperate(){
   Configuration config = new Configuration().configure();
  
   SessionFactory factory = config.buildSessionFactory();
  
   this.session = factory.openSession();
}
public void insert(Person p){
   Transaction trc = this.session.beginTransaction();
   this.session.save(p);
   trc.commit();
}

}


package com.study.hibernate;

public class TestPO {
public static void main(String[] args) {
   // TODO Auto-generated method stub
   Person p = new Person();
   p.setId("san");
   p.setName("he");
   p.setPassword("yong");
   p.setSex("男");
   p.setEmail("hao123@126.com");
  
   PersonOperate po = new PersonOperate();
   po.insert(p);

}

}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics