一、JDBCTemplate JDBC模板

user类

package cn.itcast.bean;

import java.util.Date;

public class User {
private Integer id; private String username; private Date birthday; private String sex; private String address; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username == null ? null : username.trim();
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
}

准备数据库:

package cn.itcast.a_jdbctemplate;

import java.beans.PropertyVetoException;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate; import com.mchange.v2.c3p0.ComboPooledDataSource; //演示JDBC模板
public class Demo { @Test
public void fun1() throws Exception {
//0准备连接池C3p0
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///mybatis");
dataSource.setUser("root");
dataSource.setPassword("mysqladmin"); //1创建JDBC模板对象
JdbcTemplate it = new JdbcTemplate(); it.setDataSource(dataSource); //2、书写sql语句,并执行
String sql="insert into user(username) value('rose')";
it.update(sql); } }

结果:

二、UserDao用JDBCJDBCTemplate实现

public interface UserDao {

    //增
void save (User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);
//查
int getTotalCount();
//查 }

实现类:

手动注入JDBC模板

private JdbcTemplate it;

package cn.itcast.a_jdbctemplate;

import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import cn.itcast.bean.User; public class UserDaoImpl implements UserDao{
private JdbcTemplate it;
@Override
public void save(User u) {
String sql="insert into user value(null,?)";
// TODO Auto-generated method stub
it.update(sql, u.getUsername());
} public JdbcTemplate getIt() {
return it;
} public void setIt(JdbcTemplate it) {
this.it = it;
} @Override
public void delete(Integer id) {
// TODO Auto-generated method stub } @Override
public void update(User u) {
// TODO Auto-generated method stub } @Override
public User getById(Integer id) { String sql = "select * from user where id=?"; return it.queryForObject(sql, new RowMapper<User>() { @Override
public User mapRow(ResultSet rs, int arg1) throws SQLException { User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("name"));
// TODO Auto-generated method stub
return u;
} }, id); // TODO Auto-generated method stub
} @Override
public int getTotalCount() { String sql = "select count(*) from user";
// TODO Auto-generated method stub
return it.queryForObject(sql,Integer.class);
} }

配置文件:

顺序:

db.properties配置文件

jdbc.jdbcUrl=jdbc:mysql:///mybatis
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=*****

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将JDBCTemplate放入Spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl">
<property name="it" ref="jdbcTemplate"></property> </bean> </beans>

测试:

package cn.itcast.a_jdbctemplate;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.bean.User; //演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo02 {
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun() throws Exception{
int totalCount = ud.getTotalCount();
System.out.println(totalCount);
}
}

结果:

数据库资料:

控制台输出结果:

14

三、通过继承来准备JDBC模板

配置文件修改:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl">
<!-- <property name="it" ref="jdbcTemplate"></property>-->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

spring再学习之整合JDBC的更多相关文章

  1. Spring学习5-Spring整合JDBC及其事务处理(注解方式)

    一.整合的步骤   1.步骤一:首先要获得DataSource连接池(推荐使用B方式): 要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是 ...

  2. Spring Boot 学习笔记--整合Thymeleaf

    1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...

  3. Spring Boot2.0之 整合JDBC

    很入门的知识,大家了解下就OK maven配置文件pom: spring: datasource: url: jdbc:mysql://localhost:3306/test username: ro ...

  4. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...

  5. spring再学习之AOP事务

    spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney( ...

  6. spring再学习之注解

    1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...

  7. spring再学习之配置详解

    applicationContext.xml文件配置: bean元素: <?xml version="1.0" encoding="UTF-8"?> ...

  8. spring再学习之简单测试

    一.spring是怎么工作的那,通过一个类装载进容器进行描述: 首先创建一个类user: package cn.itcast.bean; public class User { public User ...

  9. Spring再学习

    一.主要版本变更 框架最早发布于2004年,其后发布了几个重大的版本更新:在Spring 2.0中提供对XML命名空间和AspectJ的支持:Spring 2.5中新增了注解驱动的配置:在Spring ...

随机推荐

  1. 5V充12.6V三节锂电池,5V升压12.6V的电路图

    三串锂电池的充电电压是三串锂电池的最高电压值,就是12.6V了.5V充12.6V是5V给三串锂电池充电.如笔记本的USB口5V给三串锂电池充电,如5V的适配器或者手机充电器插上数据线给三串锂电池充电电 ...

  2. Vue项目之实现登录功能的表单验证!

    Vue项目之实现登录功能的表单验证! 步骤: 配置 Form表单验证; 1.必须给el-from组件绑定model 为表单数据对象 2 给需要验证的表单项 el-form-item 绑定 prop 属 ...

  3. PB 级大规模 Elasticsearch 集群运维与调优实践

    PB 级大规模 Elasticsearch 集群运维与调优实践 https://mp.weixin.qq.com/s/PDyHT9IuRij20JBgbPTjFA | 导语 腾讯云 Elasticse ...

  4. proxy_http_version 1.0 | 1.1

    Module ngx_http_proxy_module http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_ver ...

  5. (Shell)Shell命令整理

    目录 常用命令 1. 上传.下载 2. 删除文件和文件夹 3. 目录操作 4. 文件的操作 4.vim 为新添加的文件后缀支持语法高亮 常用命令 1. 上传.下载 上传文件:rz,然后回车弹出上传文件 ...

  6. MDX学习笔记(整理) MDX语法

    1.1.members和Children的用法. select [Measures].[Internet Sales Count] on columns, [客户].[全名] on rows from ...

  7. Spring 设计模式介绍

    JDK 中用到了那些设计模式?Spring 中用到了那些设计模式?这两个问题,在面试中比较常见.我在网上搜索了一下关于 Spring 中设计模式的讲解几乎都是千篇一律,而且大部分都年代久远.所以,花了 ...

  8. python 招聘数据分析

    导入包 import pandas as pd import numpy as np import matplotlib.pyplot as plt 读文件 df=pd.read_csv(r'C:\U ...

  9. 关于MongoDB的简单理解(三)--Spring Boot篇

    一.前言 Spring Boot集成MongoDB非常简单,主要为加依赖,加配置,编码. 二.说明 环境说明: JDK版本为15(1.8+即可) Spring Boot 2.4.1 三.集成步骤 3. ...

  10. office提示“应用程序无法正常启动(0xc0000142)。请单击确认关闭应用程序”

    打开word文档,突然弹出如下提示框: 网上查询,说应用程序无法正常启动(0xc0000142)的原因可能是缺少组件导致的.控制面板 - 时钟和区域 - 更改日期.时间或数字格式 - 管理 - 更改系 ...