标签:drive 数据 instance string oss 最大连接数 work actor apc
除了Spring、Mybatis相关的jar包以外,还需要引入整合的jar包、
项目目录结构如下图所示:
首先是Spring-framework.xml的配置文件,此配置文件主要作用是引入其他配置文件,代码如下图所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<import resource="classpath:spring-mvc.xml"/>
<import resource="classpath:spring-jdbc.xml"/>
</beans>
然后是spring-mvc的配置,主要作用是设置要自动扫描的包和Mybatis代理模式的依赖注入,代码如下图所示:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<mvc:default-servlet-handler />
<!-- 启动注解驱动 SpringMVC 功能 -->
<mvc:annotation-driven />
<!-- 定义要扫描 controller的包 -->
<context:component-scan base-package="com.nhu.boom.service.impl"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载Mybatis的配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/nhu/boom/mapper/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.nhu.boom.dao"/>
</bean>
<!-- 配置视图解析器解析路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 定义视图存放路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 定义视图后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
然后是spring-jdbc.xml的配置, 这里是用了C3P0连接池,代码如下图所示:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 引用配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1、声明数据源对象:C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 加载jdbc驱动 -->
<property name="driverClass" value="${driverClass}"></property>
<!-- jdbc连接地址 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<!-- 连接数据库的用户名 -->
<property name="user" value="${user}"></property>
<!-- 连接数据库的密码 -->
<property name="password" value="${password}"></property>
<!-- 数据库的初始化连接数 -->
<property name="initialPoolSize" value="3"></property>
<!-- 数据库的最大连接数 -->
<property name="maxPoolSize" value="10"></property>
<!-- 数据库的最小连接数 -->
<property name="minPoolSize" value="1"></property>
<!-- 数据库最多执行的事务 -->
<property name="maxStatements" value="100"></property>
<!-- 连接数量不够时每次的增量 -->
<property name="acquireIncrement" value="2"></property>
</bean>
</beans>
数据连接的配置文件 db.properties 如下图所示:
完成以上配置后,在test包中编写测试代码进行测试:
TestMapper.java的代码如下图所示,测试代码需要手动加载spring容器:
package com.nhu.boom.test;
public class TestMapper {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-framework.xml");
CKKuCunServiceImpl ckKuCunServiceImpl = (CKKuCunServiceImpl)context.getBean(CKKuCunServiceImpl.class);
List<CKKuCun> ckKuCun = ckKuCunServiceImpl.queryAllCkKuCun();
for (CKKuCun ckKuCun2 : ckKuCun) {
System.out.println(ckKuCun2);
}
}
}
标签:drive 数据 instance string oss 最大连接数 work actor apc
原文地址:https://www.cnblogs.com/dennyw/p/14192615.html