1. Use the JNDI Data Source
<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.
Use Data Connection Pool
<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 pool configuration Properties
Number of connections created when the initialsize pool is started
Maxactive
Maxidle
Maxopenpreparedstatements
Maxwait
Minevictableidlemillis
Minidle
Poolpreparedstatements
2. Integrate hibernate in spring
Datasource, sessionfactory, template, Dao (inherit daosupport, ref = sessionfactory)
3. Cache
Cache frequently accessed (but not updated) data.
Cache scheme ehcache.
Ehcache namespace:
Xmlns: ehcache = "http://www.springmodules.org/schema/ehcache"
Http://www.springmodules.org/schema/ehcache"
Http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
Spring Cache
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 ){
// The following two sentences are required for each of the following statements:
Beanfactory factory = new classpathxmlapplicationcontext ("applicationcontext. xml ");
Userdao Dao = (userdao) Factory. getbean ("userdao ");
// Add
User user = new user ();
User. setusername ("gg3 ");
Dao. Save (User );
// Delete
// User = Dao. findbyid (1 );
// Dao. Delete (User );
// Modify
// User = Dao. findbyid (1 );
// User. setusername ("PP ");
// Dao. attachdirty (User); // saveorupdate ();
// Select
// List <user> List = Dao. findall ();
// For (User: List)
//{
// System. Out. println (user. GetUserName ());
}
}