Demo1

hibernate.cfg.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  5.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  6.       
  7. <hibernate-configuration>  
  8.     <session-factory>  
  9.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>  
  11.         <property name="hibernate.connection.username">root</property>  
  12.         <property name="hibernate.connection.password">123</property>  
  13.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  14.         <!-- 映射配置文件,需要引入映射的配置文件 -->  
  15.         <mapping resource="domain/User.hbm.xml"/>  
  16.     </session-factory>  
  17. </hibernate-configuration>  

User.hbm.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.   
  5.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  6.   
  7.   
  8.   
  9. <hibernate-mapping>  
  10.     <class name="domain.User" table="user">  
  11.         <id name="id" column="id">  
  12.             <generator class="native"/>  
  13.         </id>  
  14.           
  15.         <property name="name" column="name"/>  
  16.         <property name="sex" column="sex"/>  
  17.         <property name="age" column="age"/>  
  18.         <property name="height" column="height"/>  
  19.         <property name="hobby" column="hobby"/>         
  20.     </class>    
  21. </hibernate-mapping>  

User.java

[java]  view plain  copy
  1. package domain;  
  2.   
  3. public class User {  
  4.     private String name;  
  5.     private String sex;  
  6.     private int age;  
  7.     private int height;  
  8.     private String hobby;  
  9.     private int id;  
  10.       
  11.       
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(int id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public String getSex() {  
  25.         return sex;  
  26.     }  
  27.     public void setSex(String sex) {  
  28.         this.sex = sex;  
  29.     }  
  30.     public int getAge() {  
  31.         return age;  
  32.     }  
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.     public int getHeight() {  
  37.         return height;  
  38.     }  
  39.     public void setHeight(int height) {  
  40.         this.height = height;  
  41.     }  
  42.     public String getHobby() {  
  43.         return hobby;  
  44.     }  
  45.     public void setHobby(String hobby) {  
  46.         this.hobby = hobby;  
  47.     }  
  48.     @Override  
  49.     public String toString() {  
  50.         return "User [name=" + name + ", sex=" + sex + ", age=" + age + ", height=" + height + ", hobby=" + hobby + "]";  
  51.     }  
  52.           
  53.   
  54. }  

Demo1.java

[java]  view plain  copy
  1. package homework;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.Transaction;  
  5. import domain.User;  
  6. import utils.HibernateUtils;  
  7.   
  8.   
  9. public class Demo1 {  
  10.     public static void main(String[] args) {  
  11.           
  12.         Session session = HibernateUtils.getSession();  
  13.         Transaction ts = session.beginTransaction();  
  14.           
  15.         User user = new User();  
  16.   
  17.   
  18.         user.setName("周建鹏");  
  19.         user.setSex("男");  
  20.         user.setAge(21);  
  21.         user.setHeight(185);  
  22.         user.setHobby("敲代码");  
  23.           
  24.         session.save(user);  
  25.         ts.commit();  
  26.           
  27.         session.close();  
  28.     }  
  29. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值