(1)Accountsdao层

//删除单个账户
int delaccount(Integer accountid);
//添加单个用户
int addaccount(Accounts accounts);
//修改单个用户
int updateaccount(Accounts accounts);

代码实现

(2)AccountdaoImpl实现类

@Repository
public class AccountDaoImpl implements AccountsDao {
@Resource
private JdbcTemplate jdbcTemplate; @Override
public int delaccount(Integer accountid) { String sql="delete from accounts where accountid=5";
int count = jdbcTemplate.update(sql);
return count;
} @Override
public int addaccount(Accounts accounts) {
String sql="INSERT INTO accounts(accountname,balance) VALUES (?,?)";
int add = jdbcTemplate.update(sql,accounts.getAccountname(),accounts.getBalance());
return add;
} @Override
public int updateaccount(Accounts accounts) {
String sql="update accounts set accountname=?,balance=? where accountid=?";
int update = jdbcTemplate.update(sql, accounts.getAccountname(), accounts.getBalance(),accounts.getAccountid());
return update;
}
}

代码实现

(3)AccountService层

//删除单个账户
int delaccount(Integer accountid);
//添加单个用户
int addaccount(Accounts accounts);
//修改单个用户
int updateaccount(Accounts accounts);

代码实现

(4)AccountServiceImpl实现类

@Service("accountsServiceImpl")
public class AccountsServiceImpl implements AccountsService{
//使用Spring IOC 将AccountDao对象注入
@Autowired
AccountsDao dao; @Override
public int delaccount(Integer accountid) {
return dao.delaccount(accountid);
} @Override
public int addaccount(Accounts accounts) {
return dao.addaccount(accounts);
} @Override
public int updateaccount(Accounts accounts) {
return dao.updateaccount(accounts);
} public AccountsDao getDao() {
return dao;
} public void setDao(AccountsDao dao) {
this.dao = dao;
}
}

代码实现

(5)applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--1.配置数据源 spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2.引入属性文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

代码实现

(6)测试类

@Test
public void delaccountTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountsService accountsService=(AccountsService) context.getBean(AccountsService.class);
int delaccount = accountsService.delaccount(5);
if (delaccount>0){
System.out.println("删除成功!");
}
} @Test
public void addaccountTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountsService accountsServiceImpl = (AccountsService)context.getBean("accountsServiceImpl");
Accounts accounts=new Accounts();
accounts.setAccountname("小丫头");
accounts.setBalance(500);
int addaccount = accountsServiceImpl.addaccount(accounts);
if (addaccount>0){
System.out.println("添加成功!");
}
} @Test
public void updateaccountTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountsService accountsServiceImpl = (AccountsService)context.getBean("accountsServiceImpl");
Accounts accounts=new Accounts();
accounts.setAccountname("丫头片子");
accounts.setBalance(1200);
accounts.setAccountid(9);
int updateaccount = accountsServiceImpl.updateaccount(accounts);
if (updateaccount>0){
System.out.println("修改成功!");
}

代码实现

JdbcTemplate增删改的更多相关文章

  1. JdbcTemplate增删改查

    1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...

  2. Spring之jdbcTemplate:增删改

    JdbcTemplate增删改数据操作步骤:1.导入jar包:2.设置数据库信息:3.设置数据源:4.调用jdbcTemplate对象中的方法实现操作 package helloworld.jdbcT ...

  3. Spring JdbcTemplate框架搭建及其增删改查使用指南

    Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...

  4. spring学习(四)spring的jdbcTemplate(增删改查封装)

    Spring的jdbcTemplate操作 1.Spring框架一站式框架 (1)针对javaee三层,每一层都有解决技术 (2)到dao 层,使用 jdbcTemplate 2.Spring对不同的 ...

  5. JdbcTemplate实现增删改查操作

    JdbcTemplate介绍 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data-JP ...

  6. SpringBoot2+Druid+JdbcTemplate+MySql实现增删改查

    1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  7. 23Spring_JdbcTemplate来实现单表的增删改查

    第一步建表:

  8. SpringMvc学习-增删改查

    本节主要介绍SpringMVC简单的增删改查功能. 1.查询 dao中的代码 public List<WeatherPojo> getAllWeather(){ String sql=&q ...

  9. 【java学习】spring mvc 公共dao的实现,定义基本的增删改查

    接口类: package com.blog.db.dao; import com.blog.util.Pagination; import java.util.List; public interfa ...

随机推荐

  1. opencv图像处理之常见滤波器

    图像平滑 Smoothing, also called blurring, is a simple and frequently used image processing operation. 平滑 ...

  2. 【已解决】关于IDEA中 Tomcat 控制台打印日志中文乱码的解决

    在 Idea 上面使用 Tomcat 时,发现控制台打印信息的时候,出行中文乱码问题; 可以通过以下几种解决办法 1:在-Dfile.encoding=UTF-8 在vm中设置编码方式 2.然后从Fi ...

  3. [python]兔子问题,斐波那契数列 递归&非递归

    假设一对幼年兔子需要一个月长成成年兔子,一对成年兔子一个月后每个月都可以繁衍出一对新的幼年兔子(即兔子诞生两个月后开始繁殖).不考虑死亡的情况,问第 N 个月时共有多少对兔子? 结果前几个月的兔子数量 ...

  4. SPSS基础学习方差分析—单因素分析

    为什么要进行方差分析? 单样本.两样本t检验其最终目的都是分析两组数据间是否存在显著性差异,但如果要分析多组数据间是否存在显著性差异就很困难,因此用方差分析解决这个问题:举例:t检验可以分析一个班男女 ...

  5. 认识Airflow的DAG

    前文Airflow的第一个DAG已经跑起来了我们的第一个任务. 本文就来丰富这个任务. 回顾我们的任务内容 我们定义了DAG的名称为Hello-World, 这个叫dag_id, 补充说明descri ...

  6. 基于Python协同过滤算法的认识

    Contents    1. 协同过滤的简介    2. 协同过滤的核心    3. 协同过滤的实现    4. 协同过滤的应用 1. 协同过滤的简介 关于协同过滤的一个最经典的例子就是看电影,有时候 ...

  7. 说说 Java 线程间通信

    序言 正文 一.Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在一个 ...

  8. Git很麻烦?不存在的!掌握这几招就够了

    废话不多说,下面直接开始了! 查看原文 确保代码库是最新的,先用这条命令把你的代码拉取到本地 git clone -- 修改完代码后,按顺序执行下面四个命令 git pull git add * /r ...

  9. C++ProtoBuf的安装与使用

    目录 安装(Ubuntu 16.04) 简介 proto2 proto3 用法 proto3 输出结果 总结 @(目录) 安装(Ubuntu 16.04) sudo apt-get install a ...

  10. Python3 学习笔记之 类型/运算符

    类型/运算符: 类型: 整数 字符串 浮点数 布尔类型 类型转换: 检查类型: 算术操作符: 逻辑操作符: 优先级: