Spring 框架下 (增 删 改 )基本操作
//applicationContext.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 指定读取配置文件 -->
<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 将jdbc模板放进连接池 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3将userDaoImp放进连接池 -->
<bean name="userDaoImp" class="cn.jy.domain.UserDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
//UserDaoImp继承于UserDao接口
package cn.jy.domain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//import org.apache.tomcat.util.digester.RuleSet;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import cn.jy.dao.UserDao;
//使用JDBC模板实现增删改查
public class UserDaoImp extends JdbcDaoSupport implements UserDao {
public void save(User u) {
String sql="insert into user1 values(2,?)";
super.getJdbcTemplate().update(sql, u.getName());
}
public void delete(Integer id) {
String sql="delete from user1 where id=?";
super.getJdbcTemplate().update(sql, id);
}
public void update(User u) {
String sql="update user1 set name=? where id=?";
super.getJdbcTemplate().update(sql,u.getName(), u.getId());
}
public User getById(Integer id) {
String sql="select * from user1 where id=?";
return super.getJdbcTemplate().queryForObject(sql, new RowMapper<User>(){
public User mapRow(ResultSet rs,int arg) throws SQLException{
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
},id);
}
public int getTotalCount() {
String sql ="select count(*) from user1";
Integer num = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return num ;
}
public List<User> getAll() {
String sql="select * from user1";
List<User> list= super.getJdbcTemplate().query(sql, new RowMapper<User>(){
public User mapRow(ResultSet rs,int arg) throws SQLException{
User u=new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
System.out.println(u);
return u;
}
});
return list;
}
}
//测试类Demo.java
package cn.jy.dao;
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;
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//连接配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
//与 Spring框架下配置UserDaoImp进行连接
@Resource(name="userDaoImp")
//声明要调用的对象
private UserDao us;
@Test
//存储数据
// public void fun1(){
// User u=new User();
// u.setName("tong");
// us.save(u);
// }
//获得id为2的name值
// public void fun1(){
// System.out.println(us.getById(2));
// }
//插入数据
// public void fun1(){
// User u = new User();
// u.setId(2);
// u.setName("tiger");
// us.update(u);
// }
//获得所有数据
// public void fun1(){
// us.getAll();
// }
//获得数据总数
// public void fun1(){
// System.out.println(us.getTotalCount());
//
// }
//删除数据
public void fun1(){
us.delete(3);
}
}
Spring 框架下 (增 删 改 )基本操作的更多相关文章
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- Spring框架下Junit测试
Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...
随机推荐
- SpringBoot Web开发(4) Thymeleaf模板与freemaker
SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...
- 支付宝 net
- day34进程相关
进程1 什么是进程 进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系 ...
- AlexNet实践
注释: CNN使用TF搭建比较简单,就像Hough检测使用CV很简单一样.但是怎么使用CNN去做一些实际操作,或者说怎么使用现有的方法进行慢慢改进,这是一个很大的问题! 直接跟着书本或者视频学习有点膨 ...
- C# 操作符 << 与 >>
1.<< 左移操作符: 左移操作符,将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0.左移相当于乘. 左移一位相当于乘2;左移两位相当于乘4;左移三位相当于乘8. 如:x< ...
- java并发等待条件的实现原理(Condition)
本篇继续学习AQS中的另外一个内容-Condition.想必学过java的都知道Object.wait和Object.notify,同时也应该知晓这两个方法的使用离不开synchronized关键字. ...
- leetcode46
public class Solution { public IList<IList<int>> Permute(int[] nums) { IList<IList< ...
- component lists rendered with v-for should have explicit keys
错误:component lists rendered with v-for should have explicit keys 解析:使用vue 的v-for时,需要:key指定唯一key 文档:h ...
- Linux:sudo,没有找到有效的 sudoers 资源。
首先,这是因为用户的权限不够导致的. 使用 ls -l /etc/passwd 查看所有用户及权限.只有可读权限(r),说明用户的权限不够. 因此,我们可以用以下方法修改用户权限: 1. su roo ...
- springboot整合devtool无法热部署
参见https://www.cnblogs.com/winner-0715/p/6666579.html.