1. Add the JNDI configuration file to the Project
<? Xml version = "1.0" encoding = "UTF-8"?>
<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" xmlns: jdbc = "http://www.springframework.org/schema/jdbc"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: util = "http://www.springframework.org/schema/util">
<Bean id = "dataSource" class = "org. springframework. jndi. JndiObjectFactoryBean">
<Property name = "jndiName" value = "java: comp/env/jdbc/myDataSource"/>
<SPAN style = "WHITE-SPACE: pre"> </SPAN> <property name = "expectedType" value = "javax. SQL. DataSource"/>
</Bean>
</Beans>
2. Add the database connection configuration in Tomcat. The specific location is/conf/context. xml under the Tomcat installation directory.
Note that the name must correspond to the second half of java: comp/env in jndiName in the above Spring configuration file.
<Resource name = "jdbc/myDataSource"
Auth = "Container" type = "javax. SQL. DataSource"
Password = "test" username = "test"
DriverClassName = "com. mysql. jdbc. Driver"
Url = "jdbc: mysql: // 192.168.229.97: 3306/test"
MaxActive = "100" maxIdle = "30" maxWait = "5000"/>
3. Add the JDBC driver mysql-connector-java-5.1.6.jar of MySQL to the lib directory of Tomcat, otherwise Tomcat will report an exception that cannot find the driver class.