Dao使用jdbcTemplate(注入过程)
来源:互联网 发布:网络吸毒案 编辑:程序博客网 时间:2024/06/01 10:18
一。Spring配置c3p0连接池
1.导入c3p0jar包
2.配置连接池
3.建好 UserDao UserService 和测试方法
package c3p0;public class UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void add(){userDao.add();}}
package c3p0;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.springframework.jdbc.core.JdbcTemplate;public class UserDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void add(){String sql="insert into test2 values(?,?)";jdbcTemplate.update(sql,"ys","666");}}
package c3p0;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAdd {@Testpublic void testAdd(){ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService=(UserService) context.getBean("userService");userService.add();}}
4.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><!-- 配置c3po连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://xxxx:3306/test"></property><property name="user" value="hpn"></property><property name="password" value="hpn2017"></property></bean><bean id="userDao" class="c3p0.UserDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="userService" class="c3p0.UserService"><!-- 注入dao对象 --><property name="userDao" ref="userDao"></property></bean><!-- 创建jdbcTemplate对象 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean></beans>
5.运行测试:
阅读全文
0 0
- Dao使用jdbcTemplate(注入过程)
- day3_spring10_dao使用jdbcTemplate(注入过程)
- jdbcTemplate注入过程
- 在DAO中使用JdbcTemplate
- Spring通过XML配置c3p0连接池和dao层注解注入使用 jdbcTemplate
- 使用Spring的JdbcTemplate实现DAO
- 使用Spring的JdbcTemplate编写的DAO
- webservice使用@Autowaired注入JDBCTemplate为NULL
- 使用Spring的JdbcTemplate实现DAO(续)
- c3p0连接池配置和Dao使用JdbcTemplate模板
- Spring配置连接池和dao使用jdbcTemplate
- Spring配置连接池和dao使用JdbcTemplate
- DAO和JdbcTemplate
- 使用JdbcTemplate调用数据库存储过程
- 使用Spring JdbcTemplate调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- iOS AVAssetExportSession视频进行转码、裁剪
- Java多线程之模拟一个阻塞队列
- Android相机开发中遇到的坑(注意事项)
- 两数组的交 II
- 导入SDE类型为esriGeometryPoint,在SDE中导出为esriGeometryAny
- Dao使用jdbcTemplate(注入过程)
- tcp超时重传
- TPshop5最新版 安装 nginx 开启PATHINFO 模式资源加载路径加载失败问题,适用tp3.2PATHINFO模式REWRITE模式
- Pycharm 快捷键盘
- Struts2+MVC+MySql数据库增删查改及分页显示
- 整数排序
- Android Studio 使用教程(十五)之 This support library should not use a different version(x)than the compileS
- NetBean、MyEclipse、Eclipse + Svn/Git 环境搭建和使用比较(详细)
- animate实现单向轨道旋转