Spring+jdbc使用示例
来源:互联网 发布:北风网 人工智能很贵 编辑:程序博客网 时间:2024/06/06 01:20
myeclipse10使用spring框架结合jdbc操作数据库
步骤:
1、引入必要的jar包,使用到了如下的jar包
spring.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1.3.jar
common-annotations.jar
common-logging.jar
common-dbcp.jar
common-pool.jar
mysql-connector-java-5.1.13.jar
2、配置命名空间
<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:<span style="color:#ff0000;">tx="http://www.springframework.org/schema/tx</span>" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd <span style="color:#ff0000;"> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd</span>">
3、配置数据源
使用的是从properties读取属性,下面蓝色部分是引入属性文件必要的代码,其中
jdbc.properties是属性文件的名称。
<span style="color:#000066;"><context:property-placeholder location="classpath:jdbc.properties"/></span><!-- 属性占位,提取配置文件中的值 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="${initialSize}"/> <!-- 连接池的最大值 --> <property name="maxActive" value="${maxActive}"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="${maxIdle}"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="${minIdle}"/> </bean>
下面是主要的文件的源代码:
beans.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/><!-- 属性占位,提取配置文件中的值 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="${initialSize}"/> <!-- 连接池的最大值 --> <property name="maxActive" value="${maxActive}"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="${maxIdle}"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="${minIdle}"/> </bean> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 采用@Transactional注解方式使用事务 --> <tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="com.gdhdcy.service.impl.PersonServiceBean"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
jdbc.properties文件
driverClassName=org.gjt.mm.mysql.Driverurl=jdbc\:mysql\://localhost\:3306/person?useUnicode\=true&characterEncoding\=UTF-8username=rootpassword=rootinitialSize=1maxActive=500maxIdle=2minIdle=1
数据库实现增删查改的java文件
package com.gdhdcy.service.impl;import java.util.List;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.transaction.annotation.Transactional;import com.gdhdcy.bean.Person;import com.gdhdcy.service.PersonService;//声明事务,这样就会自动的打开事务和关闭事务@Transactionalpublic class PersonServiceBean implements PersonService {private JdbcTemplate jdbcTemplate;public void setDataSource(DataSource dataSource) {this.jdbcTemplate = new JdbcTemplate(dataSource);}public void delete(Integer personid) {jdbcTemplate.update("delete from person where id=?", new Object[]{personid},new int[]{java.sql.Types.INTEGER});}public Person getPerson(Integer personid) {return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, new int[]{java.sql.Types.INTEGER}, new PersonRowMapper());}@SuppressWarnings("unchecked")public List<Person> getPersons() {return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());}public void save(Person person) {jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},new int[]{java.sql.Types.VARCHAR});System.out.println("保存成功");}public void update(Person person) {jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()},new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});}}
下面是完整源代码下载链接
http://pan.baidu.com/s/1ntG6T3z
下载文件直接导入到您的myeclipse,然后建立mysql的数据库(数据库文件也在代码文件夹中)。
0 0
- Spring+jdbc使用示例
- spring jdbc使用总结及示例代码
- Spring JDBC 示例
- JDBC使用示例
- spring framwor操作数据库示例spring-jdbc
- Spring JDBC-使用Spring JDBC访问数据库
- spring中使用jdbc
- 使用Spring Jdbc (1)
- 使用Spring Jdbc (2)
- 使用Spring Jdbc (3)
- Spring JDBC 的使用
- Spring中使用JDBC
- Spring中使用JDBC
- spring中使用jdbc
- 使用Spring 的JDBC
- 使用Spring 的JDBC
- Spring里使用JDBC
- Spring jdbc 的使用
- uva10271 - Chopsticks(递推)
- android开发利器--站在巨人肩膀上前行
- 黑马程序员---内存
- 简单实现SYN端口扫描
- FreeRTOS代码剖析之2:内存管理Heap_2.c
- Spring+jdbc使用示例
- LeetCode | Substring with Concatenation of All Words(链接所有单词的子串)
- Oracle数据库SQL重要语法总结
- 如何让 keil MDK v5 支持 ARM7/9 设备
- 一个网站的诞生08-- 在Web App嵌入地图
- 字符串排序问题
- 栈与递归的实现(Hanoi塔问题等等)
- 链表的插入、删除、逆序打印、输出倒数第N个、链表逆序
- ACM 输入输出模板