spring系列之2--对接数据库

来源:互联网 发布:linux开机无法进入系统 编辑:程序博客网 时间:2024/04/29 07:45

数据库选择很多:jdbc->ibatis->mybatis->druid

我们现在用的是druid

一、加入maven库

加入maven库的目的是下载druid这个第三方库,以便我们随后使用。在pom.xml中加入:

<dependency>   <groupId>mysql</groupId>   <artifactId>mysql-connector-java</artifactId>   <version>${mysql.verison}</version></dependency><dependency>    <groupId>com.alibaba</groupId>    <artifactId>druid</artifactId>    <version>1.0.15</version></dependency><dependency>   <groupId>javax.servlet</groupId>   <artifactId>servlet-api</artifactId>   <version>${servlet.version}</version>   <scope>provided</scope></dependency>

二、配置dataSource

加入bean,在spring配置文件spring-core.xml中加入:

<!--dataSource  --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><!-- 基本属性 url、user、password --><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="1" /><property name="minIdle" value="1" /><property name="maxActive" value="20" /><!-- 配置获取连接等待超时的时间 --><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="false" /><property name="maxPoolPreparedStatementPerConnectionSize" value="20" /><!-- 配置监控统计拦截的filters --><property name="filters" value="stat" /></bean>
我们发现此文件中的数据库地址,用户名,密码这些都是在别的地方,所以我们还要引入jdbc数据库配置信息。就是下一步

三、添加数据库配置信息

在resources文件夹下建jdbc.propertites文件,加入:

jdbc.url=jdbc:mysql://127.0.0.1:3358/db_name?characterEncoding=utf-8jdbc.username=rootjdbc.password=123
四、spring配置文件中引入数据库配置信息

在spring-core.xml文件中加入:

<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:/jdbc.properties</value></list></property></bean>

五、测试数据库连通性

为了测试数据库的连通性,我们创建一个测试类,该类的功能是创建一张表,如果表创建成功,表示数据库连通性正常。

public class TestCase {    @Test    public void testConnectDB() throws Exception {        DataSource dataSource;        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-core.xml");        dataSource = (DataSource) applicationContext.getBean("dataSource");//bean的名称 ;        StringBuffer ddl = new StringBuffer();        ddl.append("CREATE TABLE t_big (FID INT AUTO_INCREMENT PRIMARY KEY ");        for (int i = 0; i < 10; ++i) {            ddl.append(", ");            ddl.append("F" + i);            ddl.append(" BIGINT NULL");        }        ddl.append(")");        Connection conn = dataSource.getConnection();        Statement stmt = conn.createStatement();        stmt.execute(ddl.toString());        System.out.println("create table success");        stmt.close();        conn.close();    }}


0 0