DataSource
(1)配置datasource的方式1:使用BasicDataSource类
例如:在Spring的配置文件中,配置如下片段:
<bean id="dataSource" 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/spring" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
(2)将连接数据库所需的配置信息单独写在配置文件中,然后从配置文件中读取。
以下是将相关的配置信息配置在classpath:jdbc.properties文件中,配置文件如下classpath:jdbc.properties的配置如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
(3)Spring与Hibernate的整合
通过如下注入一个sessionFactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.zgy.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
(4)UserService.java
package com.zgy.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}
(5)UserDAOImpl.java
package com.zgy.impl;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user) {
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
System.out.println("user saved!");
}
}
(6)测试:
package com.zgy.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
User u = new User();
u.setId(1);
u.setName("zhangsan");
service.add(u);
ctx.destroy();
}
}
(7)结果:
Hibernate: insert into User (name, id) values (?, ?)
user saved!