整体配置

1、配置xml文件

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置基础数据源 -->
<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<beans:property name="user" value="${user}"></beans:property>
<beans:property name="password" value="${password}"></beans:property>
<beans:property name="driverClass" value="${driverclass}"></beans:property>
<beans:property name="jdbcUrl" value="${jdbcUrl}"></beans:property>
</beans:bean> <!-- 配置Spring 的jdbcTemplate -->
<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<beans:property name="dataSource" ref="dataSource"></beans:property>
</beans:bean> </beans:beans>

2、properties文件

user=root
password=admin
driverclass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\:///authority

3、测试类

package com.spring.jdbc;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; public class jdbcTest { public static void main(String[] args) throws SQLException{
String sql = "UPDATE person SET personName = ? WHERE Id = ?"; ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
jdbcTemplate.update(sql, "GunnerChenk",1);
System.out.println("");
}
}

注意点:

  需要引入org.springframework-transaction.jar包。否则update 方法会,cannot find DataAccessException

BatchUpdate:

1、定义了一个操作数据库的公共方法类

package com.spring.jdbc;

import java.util.ArrayList;
import java.util.*;
import org.springframework.jdbc.core.JdbcTemplate; public class jdbcOperation { private JdbcTemplate jdbcTemplate = null; public jdbcOperation(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
} public void BatchUpdate(){
String sql = "INSERT INTO person (personName,personSex,departmentId,departmentName,createtime,isDel) values" +
"(?,?,?,?,?,?)";
List<Object[]> batchArgs = new ArrayList<Object[]>(); batchArgs.add(new Object[]{"test1",1,2,"programmer1",new Date(),1});
batchArgs.add(new Object[]{"test2",1,2,"programmer2",new Date(),1});
jdbcTemplate.batchUpdate(sql, batchArgs); } public void Update(){
String sql = "UPDATE person SET personName = ? WHERE id = ?";
jdbcTemplate.update(sql, "Arsenal",1);
}
}

2、测试类

package com.spring.jdbc;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; public class jdbcTest { public static void main(String[] args) throws SQLException{ ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcContext.xml");
jdbcOperation operation = new jdbcOperation((JdbcTemplate)ctx.getBean("jdbcTemplate"));
//operation.Update();
operation.BatchUpdate();
System.out.println("");
}
}

获取对象的方法

1、定义person 的bean文件

package com.spring.jdbc;

import java.util.Date;

public class Person {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String personName;
private Integer personSex;
private Department department;
private Date createtime;
private Integer isDel;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Integer getPersonSex() {
return personSex;
}
public void setPersonSex(Integer personSex) {
this.personSex = personSex;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Integer getIsDel() {
return isDel;
}
@Override
public String toString() {
return "Person [createtime=" + createtime + ", department="
+ department + ", id=" + id + ", isDel=" + isDel
+ ", peronSex=" + personSex + ", personName=" + personName + "]";
}
public void setIsDel(Integer isDel) {
this.isDel = isDel;
} }

2、定义department 的 bean文件

package com.spring.jdbc;

import java.util.Date;

public class Department {

    private Integer Id;

    private String departmentCode;

    private String departmentName;

    public Integer getId() {
return Id;
} @Override
public String toString() {
return "Department [Id=" + Id + ", createtime=" + createtime
+ ", departmentCode=" + departmentCode + ", departmentName="
+ departmentName + ", isDel=" + isDel + ", parentDepartmentId="
+ parentDepartmentId + "]";
} public void setId(Integer id) {
Id = id;
} public String getDepartmentCode() {
return departmentCode;
} public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
} public String getDepartmentName() {
return departmentName;
} public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
} public Integer getParentDepartmentId() {
return parentDepartmentId;
} public void setParentDepartmentId(Integer parentDepartmentId) {
this.parentDepartmentId = parentDepartmentId;
} public Date getCreatetime() {
return createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} public Integer getIsDel() {
return isDel;
} public void setIsDel(Integer isDel) {
this.isDel = isDel;
} private Integer parentDepartmentId; private Date createtime; private Integer isDel;
}

3、定义获取实例的方法

    public Person GetEntity(){
String sql = "SELECT id,personName,personSex,createtime,isDel FROM person WHERE id = 1";
RowMapper<Person> personMapper = new BeanPropertyRowMapper<Person>(Person.class);
return jdbcTemplate.queryForObject(sql, personMapper);
}

4、定义测试类

package com.spring.jdbc;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; public class jdbcTest { public static void main(String[] args) throws SQLException{ ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcContext.xml");
jdbcOperation operation = new jdbcOperation((JdbcTemplate)ctx.getBean("jdbcTemplate"));
//operation.Update();
//int[] executeSize = operation.BatchUpdate();
Person person = operation.GetEntity();
System.out.println(person);
}
}

在此处虽然定义了department的bean文件,但是通过hibernate的方式是无法获取到departmentId的,毕竟是jdbc 而不是ORM框架。

Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库的更多相关文章

  1. Python学习笔记 - day11 - Python操作数据库

    MySQL的事务 MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关,MySQL的两种引擎如下: 1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB:支持ACID ...

  2. 数据库学习笔记 (三) python操作数据库

    python 操作MYSQL数据库主要有两种方式: 使用原生模块:pymysql ORM框架:SQLAchemy 一.pymysql 1.1下载安装模块 第一种:cmd下:执行命令下载安装:pip3 ...

  3. thinkphp5.0学习笔记(四)数据库的操作

    ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库驱动来处理.采用PDO ...

  4. Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知

    Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...

  5. Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean

    1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...

  6. Spring4.0学习笔记(6) —— 通过工厂方法配置Bean

    1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...

  7. Spring4.0学习笔记(5) —— 管理bean的生命周期

    Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...

  8. Spring4.0学习笔记(4) —— 使用外部属性文件

    1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  9. Spring4.0学习笔记(3) —— Spring_Bean之间的关系

    1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

随机推荐

  1. Subsets II ——LeetCode

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  2. HDOJ 2026 首字母变大写

    Problem Description 输入一个英文句子,将每个单词的第一个字母改成大写字母. Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行. Outpu ...

  3. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  4. 关闭ES动态创建type

    虽说ES的默认设置已经够我们使用了,但是总有些情景需要我们修改一些配置. 由于ES 2.*不能单独删除某个type,只能将整个index删除.这无疑非常让人苦恼. 所以我们需要关闭动态创建type以减 ...

  5. sqlserver授予用户查看执行计划的权限

    sqlserver查看语句的执行计划是非常重要的,可以提高开发人员代码的质量.所以有必要授予开发人员对数据库查看执行计划的权限.   查看执行计划的权限属于数据库一级别的权限,具体例子如下   use ...

  6. java 从零开始,学习笔记之基础入门<Oracle_基础>(三十三)

    Oracle 数据库基本知识   [训练1] 显示DEPT表的指定字段的查询.               输入并执行查询:               SELECTdeptno,dname FROM ...

  7. maven建module子模块

    在父工程中,点击new ->other  ->maven  -> maven module, 按照提示直到完成. module 可以是普通的工程也可以是web工程. 遇到的问题: 新 ...

  8. Struts2自己定义拦截器实例—登陆权限验证

    版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...

  9. 【转】Android Studio中Git的配置及协同开发

    一. Android Stutio配置git   setting–>Version Control–>Git–>Path to Git executable中选择git.exe的位置 ...

  10. Filter过滤器实现同一地址手机和电脑页面不同

    最近做一个网站,客户要求在访问主域名的时候实现电脑访问时展示电脑页面,手机访问时展示h5的手机页面,这种需求的使用还是比较多的:尤其网站需要百度推广的时候,百度推广就要求同一域名下,手机访问时展示手机 ...