配置hibernate

1.下载hibernate3,将包hibernate3.jar,spring-orm.jar,dom4j.jar,log4j.jar,antlr.jar,commons-cllections.jar,javassist.jar,jta.jar,slf4j-api.jar,slf4j-log4j12.jar,mysql-connecter-java-x.jar,c3p0.jar或commons-dbcp.jar和commons-pool.jar拷贝到lib目录下(没有的包可以在spring下找到)

 

2.在applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/testJ2EE"></property>
		<property name="username" value="root"></property>
		<property name="password" value="mysql"></property>
	</bean>
<!-- 
		<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass"
		value="com.mysql.jdbc.Driver">
		</property>
		<property name="jdbcUrl"
		value="jdbc:mysql://localhost:3306/testJ2EE">
		</property>
		<property name="user" value="root"></property>
		<property name="password" value="mysql"></property>
		<property name="minPoolSize">
		<value>1</value>
		</property>
		<property name="maxPoolSize">
		<value>20</value>
		</property>
		<property name="maxIdleTime">
		<value>1800</value>
		</property>
		<property name="acquireIncrement">
		<value>2</value>
		</property>
		<property name="maxStatements">
		<value>0</value>
		</property>
		<property name="initialPoolSize">
		<value>2</value>
		</property>
		<property name="idleConnectionTestPeriod">
		<value>1800</value>
		</property>
		<property name="acquireRetryAttempts">
		<value>30</value>
		</property>
		<property name="breakAfterAcquireFailure">
		<value>true</value>
		</property>
		<property name="testConnectionOnCheckout">
		<value>false</value>
		</property>
		</bean>
	-->

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
			<value>testJ2EE/test/model/User.hbm.xml</value>
			
			</list>
		</property>
	</bean>
    
    <bean id="userDao" class="testJ2EE.test.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
	<bean id="testStruts2" class="testJ2EE.test.TestStruts2"
		scope="request" init-method="init">
		<property name="userDao" ref="userDao"></property>
	</bean>

</beans>

 

3.添加测试需要的类文件

User.java

package testJ2EE.test.model;

public class User {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 对于User.Java的配置文件User.hbm.xml(相同目录下)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="testJ2EE.test.model.User" table="user" catalog="testJ2EE">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
 

类UserDao.java

package testJ2EE.test.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import testJ2EE.test.model.User;


public class UserDao extends HibernateDaoSupport {
	public List<User> getUsers(){
		return (List<User>)this.getHibernateTemplate().find("from User");
	}
}

 修改TestStruts2.Java

package testJ2EE.test;

import java.util.List;

import testJ2EE.test.dao.UserDao;
import testJ2EE.test.model.User;

public class TestStruts2 {
	private UserDao userDao;
	public void init(){
		System.out.println("init TestStruts2.");
	}
	public String test(){
		List<User> users = userDao.getUsers();
		for(User u : users){
			System.out.println("user: "+u.getName()+"\n");
		}
		return "success";
	}
	
	
	
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}

 向数据库中插几条数据,启动tomcat,在浏览器中输入http://localhost:8081/testJ2EE/testStruts2.action

在命令窗口中检出数据,测试成功!

你可能感兴趣的:(spring,bean,Hibernate,mysql,jdbc)