(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. Audio Bit Depth Super-Resolution with Neural Networks

    Audio Bit Depth Super-Resolution with Neural Networks 作者:Thomas Liu.Taylor Lundy.William Qi 摘要 Audio ...

  2. JAVASE知识点总结(四)

    第十七章:内部类和枚举 一.内部类 1.什么是内部类? 2.为什么要有内部类? 3.内部类的种类: 5.实例内部类 6.创建实例内部类 7.实例内部类注意点 8.访问变量原则: 4 9.静态内部类: ...

  3. Spring Cloud系列之Eureka服务治理

    写在前面 Spring Cloud Eureka是基于Netflix Eureka做的二次封装.主要包含两部分: 服务注册中心 eureka server 服务提供者 eureka client ps ...

  4. mapper操作数据的同时返回操作后的值

    <insert id="insertSelective" parameterType="com.test.entity.business" keyProp ...

  5. Vue2.0+ElementUI实现表格翻页的实例

    参考地址: https://www.cnblogs.com/zhouyifeng/p/7706815.html

  6. springboot结合mybatis使用pageHelper插件进行分页查询

    1.pom相关依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  7. Kubernetes 系列(五):Prometheus监控框架简介

    由于容器化和微服务的大力发展,Kubernetes基本已经统一了容器管理方案,当我们使用Kubernetes来进行容器化管理的时候,全面监控Kubernetes也就成了我们第一个需要探索的问题.我们需 ...

  8. Spring Boot 整合视图层技术

    这一节我们主要学习如何整合视图层技术: Jsp Freemarker Thymeleaf 在之前的案例中,我们都是通过 @RestController 来处理请求,所以返回的内容为json对象.那么如 ...

  9. golang学习之路

    目录 go语言介绍 开发环境准备 go语言基础 Go语言常用标准库 数据库相关 前端相关 web开发 go语言介绍 为什么要学习go语言 开发环境准备 从零开始搭建Go语言开发环境 VS Code配置 ...

  10. 集合查询表--Map

    查询表 Map接口java提供了一组可以以键值对(key-value)的形式存储数据的数据结构,这种数据结构成为Map.我们可以把Map看成一个多行两列的表格,其中第一列存放key,第二列存放valu ...