写了两天的Hibernate,到最后是数据库的链接写错了!怨呀!废话不多说,进入主题。
1. 导入jar包。用迅雷下载jar:http://dl2.cr173.com//soft3/s2sh_jar.zip
2. model类Student(在com.zjh.model下):
package com.zjh.model;
public class Student {
private int id;
private String name;
private String computerMac;
private int num;
private char sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student() {
}
public Student(String name, String computerMac, int num) {
this.name = name;
this.computerMac = computerMac;
this.num = num;
}
}
3. 在com.zjh.model下建立Student对应的映射文件
<?xml version="1.0" encoding="gb2312"?>
<!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.zjh.model.Student" table="student">
<id name="id" column="id" type="int">
<generator class="identity"/>
</id>
<property name="num" not-null="true" />
<property name="computerMac" not-null="true" />
<property name="name" not-null="true" />
<property name="sex" />
<property name="age" />
</class>
</hibernate-mapping>
4. 在根目录下建立hibernate.cfg.xml
<!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>
<mapping resource="com/zjh/model/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5. 接下来,是最重要的spring的配置文件spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:http="http://servicemix.apache.org/http/1.0" xmlns:bar="http://testserver"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://servicemix.apache.org/http/1.0
http://servicemix.apache.org/schema/servicemix-http-3.2.3.xsd">
<bean id="student" class="com.zjh.model.Student">
<property name="num" value="20121181" />
<property name="name" value="zjh" />
<property name="computerMac" value="HJ-SDF-SD-SD"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost/test" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="50" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="10" />
<property name="idleConnectionTestPeriod" value="30" />
<property name="acquireRetryAttempts" value="30" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="hibernateTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!--
<bean id="hibernateSupport" class="com.zjh.dao.HibernateSupport">
<property name="hibernateTemplate" ref="hibernateTemplete" />
</bean>
-->
<bean id="baseDao" class="com.zjh.dao.BaseDao" abstract="true"
>
<property name="hibernateTemplate" ref="hibernateTemplete" />
</bean>
<bean id="studentDao" class="com.zjh.dao.StudentDao" parent="baseDao">
</bean>
</beans>
6.从上面的配置文件可以看出,还有两个自定义的类
BaseDao、StudentDao
我把它们放在model.zjh.dao下面
BaseDao
package com.zjh.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class BaseDao extends HibernateDaoSupport {
public void delete(Object object) {
super.getHibernateTemplate().delete(object);
}
public void add(Object object) {
super.getHibernateTemplate().save(object);
}
public void update(Object boject) {
super.getHibernateTemplate().update(boject);
}
public List findByParameter(String para, String value,Object object) {
String string = "from " + object.getClass()+" as object where " +para + "="+ value;
return super.getHibernateTemplate().find(string);
}
}
StudentDao
package com.zjh.dao;
import java.util.List;
import com.zjh.model.Student;
public class StudentDao extends BaseDao {
// private Student student;
//
// public Student getStudent() {
// return student;
// }
//
// public void setStudent(Student student) {
// this.student = student;
// }
//
public void delete(Student student) {
super.delete(student);
}
public void add(Student student) {
super.add(student);
}
public void update(Student student) {
super.update(student);
}
public List findByParameter(String para, String value,Student object) {
String string = "from " + object.getClass()+" as object where " +para + "="+ value;
return super.getHibernateTemplate().find(string);
}
}
7. 好的,下面就只剩下测试了(在com.zjh.test下面建Test类)!
package com.zjh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zjh.dao.StudentDao;
import com.zjh.model.Student;
import com.zjh.model.SuperClass;
public class Test {
@org.junit.Test
public void ObjectTest() {
SuperClass d = new SuperClass() {
};
Integer i = new Integer(2);
System.out.println(i instanceof Object);
System.out.println(d instanceof Object);
}
@org.junit.Test
public void HibernateAndSpringTest() {
ApplicationContext spring = new ClassPathXmlApplicationContext(
"spring.xml");
Student student = (Student) spring.getBean("student");
StudentDao studentDao = (StudentDao) spring.getBean("studentDao");
studentDao.add(student);
}
public static void main(String[] args) {
ApplicationContext spring = new ClassPathXmlApplicationContext(
"spring.xml");
Student student = (Student) spring.getBean("student");
StudentDao studentDao = (StudentDao) spring.getBean("studentDao");
studentDao.add(student);
}
}
好了,大功告成!!