学了几天的Hibernate,再此做出简单配置测试的整理,方便使用。
1.jar包导入 :hibernate-core-4.2.0.CR2.jar,mysql-connector-java-3.1.12-bin.jar,等
2.po类(以Student为例)
@Entity
@Table(name="student")
public class Student implements Serializable {
@Id
@GeneratedValue
private Integer stuId;@Column
private String stuName;@Column
private String stuSex;@Column
private Integer stuAge;@Column
private String stuAddress;
public Student() {
super();
}
public Student( String stuName, String stuSex, Integer stuAge, String stuAddress) {
super();
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
this.stuAddress = stuAddress;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public Integer getStuAge() {
return stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
@Override
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuSex="
+ stuSex + ", stuAge=" + stuAge + ", stuAddress=" + stuAddress
+ "]";
}
}
方式一: 配置Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- ORM -->
<hibernate-mapping package="com.po">
<class name="Student" table="student">
<!-- id 指主键 -->
<id name="stuId" column="stuId">
<!-- 自动增长 -->
<generator class="native"/>
</id>
<!-- 一般属性 -->
<property name="stuName" type="string" column="stuName"/>
<property name="stuSex" type="string" column="stuSex"/>
<property name="stuAge" type="integer" column="stuAge"/>
<property name="stuAddress" type="string" column="stuAddress"/></class>
</hibernate-mapping>
方式二:使用annotation,配置方式见Student类
3.配置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配置 -->
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/stub</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property><property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 其它配置 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 映射配置文件 -->
<mapping resource="com/po/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.配置日志文件log4j,让控制台不输出
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=hibernate.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### fatal error warn info debug
log4j.rootLogger=warn, file, stdout
5.Test类进行测试
public class StudentTest {
@Test
public void testCreateTable() {
Configuration cfg = new Configuration().configure();
SchemaExport sche = new SchemaExport(cfg);
sche.create(true,true);
}
@Test
public void testCreateTable2() {
Configuration cfg = new Configuration().configure();
cfg.buildSessionFactory();
}
//插入
@Test
public void testSave() {
//读取并解析xml
Configuration cfg = new Configuration().configure();
//创建sessionFactory
SessionFactory sf = cfg.buildSessionFactory();
//打开session
Session session = sf.openSession();
//创建事务
Transaction tran = session.beginTransaction();
Student stu = new Student("李四","男",18,"南京");
//持久化操作
session.save(stu );
//提交事务
tran.commit();
//关闭session
session.close();
//关闭sessionFactory
sf.close();
}//查询全部
@Test
public void testQuery(){
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction tran = session.beginTransaction();
List<Student> stulst=session.createSQLQuery("select * from Student").addEntity(Student.class).list();
for (Student stu:stulst) {
System.out.println(stu);
}
tran.commit();
session.close();
sf.close();
}
//查询一条
@Test
public void testById(){
Integer stuId = new Integer(1);
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction tran = session.beginTransaction();
Student stu =(Student) session.load(Student.class, stuId);
System.out.println(stu);
tran.commit();
session.close();
sf.close();
}//进行修改操作
@Test
public void testUpdateStudent(){
Integer stuId = new Integer(1);
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction tran = session.beginTransaction();
Student stu = (Student)session.load(Student.class, stuId);
stu.setStuName("王小二");
session.save(stu);
System.out.println(stu);
tran.commit();
session.close();
sf.close();
}
//删除操作
@Test
public void testDelete(){
Integer stuId = new Integer(24);
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session =sf.openSession();
Transaction tran =session.beginTransaction();
Student stu = (Student)session.load(Student.class, stuId);
session.delete(stu);
tran.commit();
session.close();
sf.close();
}
}