简易搭建jar包
User类
package com.littlepage.test; public class User {
private int uid;
private String uname;
private String uage;
private String uaddress; public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUage() {
return uage;
}
public void setUage(String uage) {
this.uage = uage;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
} }
User.hbm.xml文件
<?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>
<class name="com.littlepage.test.User" table="t_User">
<id name="uid" column="uid">
<generator class="native"></generator>
</id>
<property name="uname" column="uname"></property>
<property name="uage" column="uage"></property>
<property name="uaddress" column="uaddress"></property>
</class>
</hibernate-mapping>
hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernateday01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.characterEncoding">utf8</property>
<mapping resource="com/littlepage/test/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test代码
package com.littlepage.test; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; public class TestHibernate {
@Test
public void addTest() {
Configuration cfg=new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
User user=new User();
user.setUname("littlepage");
user.setUage("18");
user.setUaddress("China");
session.save(user);
ts.commit();
session.close();;
sf.close();
}
}
Test结果截图