JdbcTemplate增删改
(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增删改的更多相关文章
- JdbcTemplate增删改查
1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...
- Spring之jdbcTemplate:增删改
JdbcTemplate增删改数据操作步骤:1.导入jar包:2.设置数据库信息:3.设置数据源:4.调用jdbcTemplate对象中的方法实现操作 package helloworld.jdbcT ...
- Spring JdbcTemplate框架搭建及其增删改查使用指南
Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...
- spring学习(四)spring的jdbcTemplate(增删改查封装)
Spring的jdbcTemplate操作 1.Spring框架一站式框架 (1)针对javaee三层,每一层都有解决技术 (2)到dao 层,使用 jdbcTemplate 2.Spring对不同的 ...
- JdbcTemplate实现增删改查操作
JdbcTemplate介绍 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data-JP ...
- SpringBoot2+Druid+JdbcTemplate+MySql实现增删改查
1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 23Spring_JdbcTemplate来实现单表的增删改查
第一步建表:
- SpringMvc学习-增删改查
本节主要介绍SpringMVC简单的增删改查功能. 1.查询 dao中的代码 public List<WeatherPojo> getAllWeather(){ String sql=&q ...
- 【java学习】spring mvc 公共dao的实现,定义基本的增删改查
接口类: package com.blog.db.dao; import com.blog.util.Pagination; import java.util.List; public interfa ...
随机推荐
- Android Studio [相对布局RelativeLayout]
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- Maven项目运行报错提示找不到加载主类
遇到这个问题花了几小时时间看网上的各种解决方法.试了几种都没用,最后用了这种本办法. 亲测第三条 https://blog.csdn.net/yuliantao/article/details/766 ...
- 编写shell脚本实现一键创建KVM虚拟机
shell脚本一键创建虚拟机 代码如下: #!/bin/bashname=$1 #把位置变量$1重新定义为name(创建虚拟机的名字)path1=/var/lib/libvirt/images/ #i ...
- .NET开发者必须学习.NET Core
很多的.NET开发者在接触.Net Core之前,对于linux系统一点也不了解,也未曾有过主动去学习的念头.在接触了.Net Core之后才会慢慢学习linux相关知识,很多同学想转Java,这个很 ...
- yum install php-gd 安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64 大
yum install php-gd安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64大概的 ...
- python 虚拟环境配置
刚学习 python 的同学经常会遇到一个问题: 已经安装了特定的包或者第三库,但是 pycharm 总是提示没有找到.
- Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)
1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...
- TCP/UDP的小事情
UDP: 没有复杂的控制机制,面向无连接的通信服务. 常用于: 包总量少的通信 音视频传输(即时通信) TCP: 对传输.发送.通信.进行控制的协议.面向有连接的协议,只有在确认通信对端存在时才会发送 ...
- 服务器时间误差导致的google sign-in后台验证错误(远程调试java程序)
https://developers.google.com/identity/sign-in/web/backend-auth import com.google.api.client.googlea ...
- Linux内存描述之内存节点node–Linux内存管理(二)
日期 内核版本 架构 作者 GitHub CSDN 2016-06-14 Linux-4.7 X86 & arm gatieme LinuxDeviceDrivers Linux内存管理 #1 ...