spring 整合 hibernate
1.闲话少说了,先看版本吧。
2.在看要导入的包吧:
其中打圈的,我在hibernate和spring的lib包中无法找到,要自己单独下载。
因为版本的原因,或许你下载的包可能会导致包无法找到,没有关系,有些事情总是试试才知道的。
这也是学习必经之路哦。
其中的dbcp 和pool 是spring配置数据源(数据库连接池要用到的),而slf4j的几个包,是由于看了马士兵视频
才养成这个习惯,整合的时候要用到这个log包。
3.整合
整个工程的样子
其中打红圈的是主运行类,会在下面打上代码。。。
-----------
基本的代码
Entity类,Item.java
package com.endual.domain;
/**
* @author Gavin King
*/
public class Item {
private Long id;
private String name;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Item.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"> <hibernate-mapping package="com.endual.domain"> <class name="Item" table="table_items"> <id name="id"> <generator class="increment"/> </id> <property name="name" /> <property name="description" /> </class> </hibernate-mapping>
applicationContext.xml配置代码
我首先要向你强烈推荐的是spring的文档,里面有详细的配置信息哦。网上的资料都是那来的。
<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="mappingResources"> <list> <value>com/endual/domain/Item.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql">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="mySessionFactory"></property> </bean> <bean name="daoImpl" class="com.endual.test.DaoImpl"> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <bean name="person" class="com.endual.domain.Person"> <property name="name" value="chenwei" /> </bean> </beans>
你可能在这里会错误的:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
因为文档中,用的HQL数据库吧,所有会导致我们的数据库自动建表的语句没有了。
配置文件里面的信息不解释。
Dao接口的代码
package com.endual.test;
public interface Dao {
public void save() ;
}
DaoImpl.java代码
package com.endual.test;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.endual.domain.Item;
public class DaoImpl extends HibernateDaoSupport implements Dao{
public void save() {
Item entity = new Item();
entity.setName("dd") ;
entity.setDescription("dd") ;
try {
this.getHibernateTemplate().save(entity) ;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//this.getHibernateTemplate().save(entity ) ;
}
}
主运行类:
package com.endual.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.endual.domain.Person;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// DaoImpl dd = new DaoImpl() ;
// dd.save() ;
ApplicationContext application=new ClassPathXmlApplicationContext("/applicationContext.xml") ;
DaoImpl dao =(DaoImpl)application.getBean("daoImpl");
dao.save() ;
// DaoImpl dd = new DaoImpl() ;
// dd.save() ;
}
}
运行结果和SQL数据不显示,祝你成功吧。O(∩_∩)O