标签:des style blog http color java 使用 os
上一篇文章已经讲解了如何使用Spring搭建工程,这一篇文章是接着上一篇文章来描述的。
一、载入依赖
新增加了两个依赖,mysql数据库驱动和alibaba数据源包
<!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- 阿里巴巴数据源包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.2</version> </dependency>
二、添加配置文件
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/mydays?useUnicode=true&characterEncoding=utf-8 jdbc_username=root jdbc_password=li*******
三、修改spring.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <context:component-scan base-package="com.qunar.studyspring"></context:component-scan> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName"> <value>${jdbc_driverClassName}</value> </property> <property name="url"> <value>${jdbc_url}</value> </property> <property name="username"> <value>${jdbc_username}</value> </property> <property name="password"> <value>${jdbc_password}</value> </property> <!-- 连接池最大使用连接数 --> <property name="maxActive"> <value>20</value> </property> <!-- 初始化连接大小 --> <property name="initialSize"> <value>1</value> </property> <!-- 获取连接最大等待时间 --> <property name="maxWait"> <value>60000</value> </property> <!-- 连接池最大空闲 --> <property name="maxIdle"> <value>20</value> </property> <!-- 连接池最小空闲 --> <property name="minIdle"> <value>3</value> </property> <!-- 自动清除无用连接 --> <property name="removeAbandoned"> <value>true</value> </property> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout"> <value>180</value> </property> <!-- 连接属性 --> <property name="connectionProperties"> <value>clientEncoding=UTF-8</value> </property> </bean> <!-- 对数据源进行事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- @Trasaction注解 事物配置项 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
四、写Dao层代码
package com.qunar.studyspring.dao; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.qunar.studyspring.object.Person; @Repository public class PersonDao { @Autowired private DataSource dataSource; private JdbcTemplate jdbcTemplate; public void save(Person person){ this.jdbcTemplate = new JdbcTemplate(this.dataSource); int flag = this.jdbcTemplate.update("insert into user (nickname) values (?)",new Object[]{person.getNickName()}, new int[]{java.sql.Types.VARCHAR}); System.out.println(flag); } }
五、写测试代码
package com.qunar.studyspring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.qunar.studyspring.Service.IPersonService; import com.qunar.studyspring.object.Person; public class SpringTest { @Test public void instanceSpring(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); IPersonService personService = (IPersonService) ctx.getBean("personService"); Person p = new Person(); p.setId(1); p.setNickName("liqiu"); personService.save(p); } }
六、测试成功
代码已经上传,下载地址:http://files.cnblogs.com/liqiu/studyspringjdbc.zip
Spring Jdbc事例说明(三),布布扣,bubuko.com
标签:des style blog http color java 使用 os
原文地址:http://www.cnblogs.com/liqiu/p/3917250.html