一、导入jar包:
并导入数据库连接jar包:
下载
pool.jar一定要下载1.x版本,亲测2.xxx版本会出现报错:Failed to introspect bean class [org.apache.commons.dbcp.BasicDataSource] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory。
在applicationContext.xml文件中:
<!-- 数据库配置 --> <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/student?characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="893225523"></property> <property name="maxActive" value="10"></property> <property name="maxIdle" value="6"></property> </bean> <!-- 配置事务管理器txManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 增加对事务的支持 --> <tx:annotation-driven transaction-manager="txManager"/>