06_Spring JDBCTemplate
Spring对不同持久化技术的支持
|
ORM持久化技术 |
模板类 |
|
JDBC |
org.springframework.jdbc.core.JdbcTemplate |
|
Hibernate3.0 |
org.springframework.orm.hibernate3.HibernateTemplate |
|
IBatis(MyBatis) |
org.springframework.orm.ibatis.SqlMapClientTemplate |
|
JPA |
org.springframework.orm.jpa.JpaTemplate |
使用JDBCTemlpate
Spring JDBC是Spring提供的持久层技术,简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似,导入必要jar包到工程目录,
spring-jdbc-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
数据库驱动包
1.创建jdbc.properties
jdbc.driverClass=com.jdbc.mysql.Driver
jdbc.jdbcUrl=jdbc:mysql:///crm
jdbc.user=root
jdbc.password=123456
2.applicationContext.xml
<?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: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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!--1 创建一个jdbcTemplate对象 -->
<!-- <bean name="jt" class="org.springframework.jdbc.core.JdbcTemplate"> </bean> -->
<!--2 继承JdbcDaoSupport 通过getJdbcTemplate()方法获得 -->
<!-- 读取属性文件方式<一> -->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<!-- 读取属性文件方式<二> -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- c3p0数据源 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- userDao对象 -->
<bean name="userDao" class="com.wann.dao.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3.userDao
public interface UserDao {
//增
void save(User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);
//查
int getTotal();
//查
List<User> getAll();
}
4.userDaoImpl
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
//声明jdbcTemplate
/*@Resource(name="jt")
private JdbcTemplate jt;*/
//set方法
/*public void setJt(JdbcTemplate jt) {
this.jt = jt;
}*/
@Override
public void save(User u) {
String sql="insert into t_user values (null,?)";
getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql="delete from t_user where id=?";
getJdbcTemplate().update(sql, id);
//jt.update(sql,id);
}
@Override
public void update(User u) {
String sql="update t_user set name=? where id=?";
getJdbcTemplate().update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql="select id,name from t_user where id=?";
return getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){
@Override
//参数 结果集 args 索引 第一行0,。。。。
public User mapRow(ResultSet rs, int args) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotal() {
String sql="select count(1) n from t_user";
return getJdbcTemplate().queryForObject(sql, Integer.class);
}
@Override
public List<User> getAll() {
String sql="select id,name from t_user";
List<User> list = getJdbcTemplate().query(sql, new RowMapper<User>(){
@Override
//参数 结果集 args 索引 第一行0,。。。。
public User mapRow(ResultSet rs, int args) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
}
注意:这里要得到jdbctemplate对象,1.继承JdbcDaoSupport 后getJdbcTemplate()
2.在applicationContext.xml配置
<!--1 创建一个jdbcTemplate对象 -->
<!-- <bean name="jt" class="org.springframework.jdbc.core.JdbcTemplate"> </bean> -->
<!--2 继承JdbcDaoSupport 通过getJdbcTemplate()方法获得 -->
5.测试
@RunWith(SpringJUnit4ClassRunner.class) //帮我们创建容器
@ContextConfiguration("classpath:applicationContext.xml")//引入配置文件
public class Demo2 {
@Resource(name = "userDao")
private UserDao userDao;
@Test
public void test(){
/* User u = new User();
u.setName("李四");
userDao.add(u);*/
List<User> all = userDao.getAll();
for(User u:all) {
System.out.println(u);
}
}
@Test
public void test1(){
System.out.println(userDao.getById(1));
}
}
06_Spring JDBCTemplate的更多相关文章
- JdbcTemplate+PageImpl实现多表分页查询
一.基础实体 @MappedSuperclass public abstract class AbsIdEntity implements Serializable { private static ...
- Spring JdbcTemplate
参考链接: https://my.oschina.net/u/437232/blog/279530 http://jinnianshilongnian.iteye.com/blog/1423897 J ...
- jdbcTemplate批量插入(添加)
public void addSubscibe(List<PermedipUserSubscribeVo> list) { final List<PermedipUserSubscr ...
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- Spring MVC篇二、使用JdbcTemplate进行数据库操作
上一篇只是一个简单的Spring MVC框架,接下来添加一些跟数据库的交互. 一.添加jdbc相关配置 在maven中添加相关依赖后,配置数据库访问参数及数据源.数据库参数使用配置文件,代码如下: ...
- 使用Spring JdbcTemplate实现数据库操作
今天我来演示 关于JDBCTemplate实现对数据库的查询和添加 首先是添加 第一步大家都知道 创建一个实体类 然后写一个方法 把实体类当参数传进去 在实现这个接口 JdbcDaoSupport这个 ...
- JdbcTemplate进行查询
1.jdbcTemplate.queryForInt() 和 jdbcTemplate.queryForLong() 例如:下面使用queryForInt()方法传回user表中的记录数: jdbcT ...
- jdbcTemplate之jdbc模板技术
1:为什么要使用jdbcTemplate? 在实际开发中使用jdbc技术太过复杂,为了减少代码冗余,操作简单 步骤一:创建实体类 package beans; public class Book { ...
- Spring JdbcTemplate 方法详解
JdbcTemplate主要提供以下五类方法: execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句: update方法及batchUpdate方法:update方法用于执行新增.修 ...
随机推荐
- Docker学习のDocker和虚拟机
最初听到Docker,是作为虚拟机来宣传的,但是它本质不是虚拟机 一.虚拟机 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. ...
- window下apache2.2配置多个tomcat使用不同二级域名,共用80端口
思路:分3步, 1,安装apache服务器httpd-2.2.25-win32-x86-no_ssl.msi,安装tomcat 2,配置tomcat默认访问的项目,配置apache服务器 3,测试 第 ...
- BlueHost主机建站方案怎样选择?
BlueHost是知名美国主机商,近年来BlueHost不断加强中国市场客户的用户体验,提供多种主机租用方案,基本能够满足各类网站建设需求.下面就和大家介绍一下建站应该怎样选择主机. 1.中小型网站 ...
- arc098E Range Minimum Queries
题意:给你一个n个数的数组,每次能够选取连续的长度为K的子序列,取出其中任意一个最小元素. 一共操作Q次.问取出的元素中Max-Min最小是多少? 标程: #include<bits/stdc+ ...
- 跨域问题The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by t
withCredentials 属性 上面说到,CORS请求默认不发送Cookie和HTTP认证信息.如果要把Cookie发到服务器,一方面要服务器同意,指定Access-Control-Allow- ...
- 将Form的AlphaBlend属性设置为True,之后调整Form的AlphaBlendValue属性,255为不透明,数字越小透明度越大~~~想显示文字用l
将Form的AlphaBlend属性设置为True,之后调整Form的AlphaBlendValue属性,255为不透明,数字越小透明度越大~~~想显示文字用lable就行哈~~~直接在窗体上写字就行 ...
- Delphi屏幕截图的实现
首先要获得设备环境的句柄,可以通过GetDC函数来获得,对于这个函数,MSDN上是这样说明的 The GetDC function retrieves a handle to a device con ...
- iOS开发之SceneKit框架--SCNView.h
1.SCNView 在macOS中,SCNView是NSView的子类,在iOS和tvOS中,SCNView是UIView的子类.SCNView用于显示SceneKit的3D场景,而需要设置场景的相关 ...
- UVA-108-Maximum Sum-子矩阵最大和(最大连续子序列的变形)+降维处理+dp
A problem that is simple to solve in one dimension is often much more difficult to solve in more tha ...
- PAT甲级——A1123 Is It a Complete AVL Tree【30】
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...