----------------------------------------spring 整合jdbc---------------------
Posted 【宁静的夏天】
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了----------------------------------------spring 整合jdbc---------------------相关的知识,希望对你有一定的参考价值。
一、
1.spring 容器可以有多种方式连接数据库:
01.
<!--SpringJDBC需要的jar 只是spring默认的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
02.
<!--dbcp Aphche比较常用-->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
3.
<!--c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
4.
<!--alibaba datasource-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
上面是四中方式的依赖
二、我们dao实现以前都是继承basedao现在我们继承JDBCDaoSupport,具体代码如下:代码中有一个
private JdbcTemplate jdbcTemplate;这属性,spring在底层通过给dataSource赋值就可以给jdbcTemplate给上值。
package cn.bdqn.dao.impl; import cn.bdqn.Account; import cn.bdqn.dao.AccountDao; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { private JdbcTemplate jdbcTemplate; public List<Account> selectAccount() { String sql="select * from Account"; 注意:我们查询的时候,第二个参数是RowMapper类型的,这里通过匿名类部类实现 List<Account> list = getJdbcTemplate().query(sql, new RowMapper<Account>() { public Account mapRow(ResultSet resultSet, int i) throws SQLException { Account account=new Account(); account.setAid(resultSet.getInt("aid")); account.setAname(resultSet.getString("aname")); account.setBalance(resultSet.getDouble("balance")); return account; } }); return list; } }
三、applicationContext.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--01.加载jdbc.properties文件 的两种方式--> <!--<context:property-placeholder location="classpath:jdbc.properties"/>--> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <!-- 02.配置数据源 001.使用spring默认的数据源 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.jdbcUrl}"/> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--002.dbcp数据源--> 这种配置比较常用 <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.jdbcUrl}"/> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--003 c3p0数据源--> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="user" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean>--> <!--004 使用阿里巴巴de数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.jdbcUrl}"/> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--03.配置dao--> <bean id="accountDao" class="cn.bdqn.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <!--04.配置service--> <bean id="impl" class="cn.bdqn.service.impl"> <property name="accountDao" ref="accountDao"/> </bean> </beans>
四、jdbc.properties文件的代码:
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///mybatis jdbc.userName=root jdbc.password=000
五、目录结构:
六、prom.xml 的全部依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>U3</artifactId> <groupId>cn.bdqn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springJDBCt14</artifactId> <packaging>jar</packaging> <name>springJDBCt14</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--spring对应的版本号--> <spring.version>4.2.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <scope>test</scope> </dependency> <!--引入需要的spring 核心jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--引入mysql的驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <!--配置jdbc需要的数据源--> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!--SpringJDBC需要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--alibaba datasource--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> </dependencies> </project>
以上是关于----------------------------------------spring 整合jdbc---------------------的主要内容,如果未能解决你的问题,请参考以下文章