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 ...
随机推荐
- linux下安装node.js时npm无法使用
安装node.js 10.15.1版本时,安装完在node的安装目录下执行./node -v查看node版本,成功则表示node已安装成功 [root@localhost bin]# ./node - ...
- JAVA Atm测试实验心得
通过一个假期的自学,完成了老师布置的样卷任务.使用Escipse编写一个学生成绩的管理系统. 一开始两眼摸黑,通过观看Java课程的视频,地址:https://www.bilibili.com/vid ...
- 引用极光jar包之后出现控制台日志打印不出来的问题。解决!
由于极光的jar包中引用的有log4j,项目本身也引用有log4j,如果版本有冲突,则会出现控制台日志记录打印不出来的现象.解决:引用极光jar包的时候,排除log4j. <dependency ...
- shiro使用注解(@RequiresPermissions等)不无效及异常处理
1.注解不生效 在shiro配置类中加上如下代码: /** * Shiro生命周期处理器 */ @Bean(name = "lifecycleBeanPostProcessor") ...
- 应用角度看kafka的术语和功能
kafka的术语(Terminology) Topic 和Consumer Group Topic 每条发布到 Kafka 集群的消息都有一个类别,这个类别被称为 Topic.(物理上不同 Topic ...
- .Net Core 商城微服务项目系列(八):购物车
最近加班有点多,一周五天,四天加班到11点+,心很累.原因是我当前在的这个组比较特殊,相当于业务的架构组,要为其它的开发组提供服务和监控.所以最近更新的也少,不过这个元旦三天假应该会更新三篇. 这篇是 ...
- ORM增删改查
目录 orm django 连接mysql顺序 1 settings配置文件中 2 项目文件夹下的init文件中写上下面内容, 补充 3 models文件中创建一个类(类名就是表名) 4.执行数据库同 ...
- 设计时数据源:在PostgreSql 数据查询中使用参数过滤
在上一篇文章中,我们学习了如何设计时连接PostgreSQL 数据库及环境搭建.本节我们来学习使用PostgreSql 数据源时,创建数据集时带参数过滤的查询语句写法. 在报表中包含两种参数,可参考博 ...
- python 虚拟环境配置
刚学习 python 的同学经常会遇到一个问题: 已经安装了特定的包或者第三库,但是 pycharm 总是提示没有找到.
- day 23
目录 面向对象总复习 面向对象总复习 面向过程编程思想: 核心是过程,过程指的是做事情的步骤,即先干什么再干什么. 基于该编程思想编程,就好比一条流水线,一种机械式的思维方式. 面向对象编程思 ...