1, 使用JNDI数据源
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="/jdbc/RantzDataSource"/>
<property name=resourceRef" value="true"/>
</property>
</bean>
java:comp/env/jdbc/RantzDataSource.
使用数据连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3309/sampledb" />
<property name="username" value="root" />
<property name="password" value="1234" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<bean>
BasicDataSource的池配置属性
initialSize 池启动时创建的连接数量
maxActive
maxIdle
maxOpenPreparedStatements
maxWait
minEvictableIdleMillis
minIdle
poolPreparedStatements
2.在Spring集成Hibernate
dataSource,sessionFactory,Template, Dao(继承DAOSupport,ref=sessionFactory)
3.缓存
对频繁访问(但不频繁更新)的数据进行缓存。
缓存方案EHCache.
ehcache命名空间:
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
"http://www.springmodules.org/schema/ehcache"
"http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
spring 缓存
4.spring+hibernate OK
add spring,hibernate 的jar
package test;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.User;
import bean.UserDAO;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// 下面每一个都需要下面这两句
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDAO dao= (UserDAO)factory.getBean("userDAO");
// 增
User user = new User();
user.setUsername("gg3");
dao.save(user);
// 删
// User user=dao.findById(1);
// dao.delete(user);
// 改
// User user=dao.findById(1);
// user.setUsername("pp");
// dao.attachDirty(user); //saveorUpdate();
// 选
// List<User> list=dao.findAll();
// for(User user:list)
// {
// System.out.println(user.getUsername());
}
}