Spring4.0学习笔记(12) —— JDBCTemplate 操作数据库
整体配置
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 操作数据库的更多相关文章
- Python学习笔记 - day11 - Python操作数据库
MySQL的事务 MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关,MySQL的两种引擎如下: 1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB:支持ACID ...
- 数据库学习笔记 (三) python操作数据库
python 操作MYSQL数据库主要有两种方式: 使用原生模块:pymysql ORM框架:SQLAchemy 一.pymysql 1.1下载安装模块 第一种:cmd下:执行命令下载安装:pip3 ...
- thinkphp5.0学习笔记(四)数据库的操作
ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库驱动来处理.采用PDO ...
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
- Spring4.0学习笔记(4) —— 使用外部属性文件
1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
随机推荐
- codeforce-191E-Thwarting Demonstrations(树状数组+二分+离散)
题意: 求第K 大连续区间 分析: 二分答案,再n * log(n)判断有几个区间的区间和大于mid,然后调整上下界,使这个值不断的接近k. 判断符合条件的区间总数:线性扫描sum[n](前n项和) ...
- Linux下的定时器:alarm()与setitimer()
Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...
- 图论(差分约束系统):POJ 1275 Cashier Employment
Cashier Employment Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7651 Accepted: 288 ...
- 【动态规划】HDU 5791 Two
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 题目大意: A,B两个数列,问A的子集和B的子集相等的子集对数.子集内顺序按照数列顺序,相同的 ...
- 暴力求解——素环数 Prime Ring Problem ,UVa 524
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...
- SRM 405(1-250pt, 1-500pt)
DIV1 250pt 题意:以linux系统中文件系统的路径表示方法为背景,告诉你某文件的绝对路径和当前位置,求相对路径.具体看样例. 解法:模拟题,不多说.每次碰到STL的题自己的代码都会显得很sb ...
- 上海Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 考研路茫茫--单词情结 - HDU 2243(AC自动机+矩阵乘法)
分析:与poj的2778差不多的,求出来所有的情况然后减去不包含的就行了,这次使用了一下kuangbin的那种自动机写法,确实还不错,因为尤是在建立矩阵的时候更加方便. 代码如下: ======= ...
- 高频交易:Solarflare组建超低延迟网络
10Gb以太网适配器制.网卡造商Solarflare目前正在将自己的网卡系列产品转变为服务器产品.其产品在金融领域有着广泛的应用. Solarflare首先将现场可编程门阵列(FPGA)放入网络适配器 ...
- Eclipse连接SVN服务器
(1)安装 eclipse SVN 插件 插件名称 site-1.4.8.zip Help --> SoftwareUpdates --->Find and Insta ...