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 ...
随机推荐
- asp.net core3.0 mvc 用 autofac
好久没有写文章了,最近在用.net core3.0,一些开发中问题顺便记录: 1.首先nuget引入 Autofac Autofac.Extensions.DependencyInjection 2. ...
- Android [启动方式:standard singleTop singleTask singleInstance]
栈顶Activity是当前正在显示的. 以A.B举例 1.standard 不同的Activity都存放在同一个栈中,每次创建实例都会堆放到栈顶,逐次返回直至退出. 创建实例B 创建实例A 点击返回时 ...
- 23种设计模式之责任链模式(Chain of Responsibility Pattern)
责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计模式属于行为型模式. ...
- Gradle 梳理:安装、入门使用方法
Gradle 教程:第一部分,安装[翻译] 原文地址:http://rominirani.com/2014/07/28/gradle-tutorial-part-1-installation-se ...
- 使用.NET Core中创建Windows服务(一) - 使用官方推荐方式
原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tu ...
- 05-padding
padding padding:内边距,内容到边框的距离 而且,padding是有背景颜色的.background-color属性将填充所有border以内的区域(不包括border) padding ...
- Ionic2优于Ionic1的6个理由
经历了一个从0到有的app的开发,我已经很熟悉Ionic1了,在此期间我曾发现过Ionic1的一些bug,和一些不合理的地方(根基版本 不同,后续我会陆续发表这些文章),我甚至在此期间对Ionic1进 ...
- SpringBootSecurity学习(21)前后端分离版之OAuth2.0非对称加密
JWT转换器 前面的例子中,都是在授权服务配置类中配置了一个很简单的jwt转换器,如下: 可以看到我们只用setSigningKey方法配置了一个秘钥,这里使用的是简单的对称加密的方式来加密jwt内容 ...
- WebGL简易教程(十):光照
目录 1. 概述 2. 原理 2.1. 光源类型 2.2. 反射类型 2.2.1. 环境反射(enviroment/ambient reflection) 2.2.2. 漫反射(diffuse ref ...
- Linux修改屏幕分辨率至2K
使用命令:cvt,与 xrandr 使用cvt命令查看分辨率配置: Modeline后边分别是 modeName 以及 详细的配置 linklee@linklee-270E5G-270E5U:~$ c ...