}
基于spring内置数据源的JdbcTemplate入门案例:
/**
-
@Author: Ly
-
@Date: 2020-08-04 23:43
*/
public class JdbcTemplateDemo1 {
public static void main(String[] args) {
//准备数据源:spring的内置数据源
DriverManagerDataSource ds=new DriverManagerDataSource();
ds.setDriverClassName(“com.mysql.cj.jdbc.Driver”);
ds.setUrl(“jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false”);
ds.setUsername(“root”);
ds.setPassword(“123456”);
//1.创建对象JdbcTemplate
//JdbcTemplate jt=new JdbcTemplate(ds);
JdbcTemplate jt=new JdbcTemplate();
//给jt设置数据源
jt.setDataSource(ds);
//2.执行操作
jt.execute(“insert into account(name,money)values(‘ccc’,1000)”);
}
}
配置bean.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”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
测试代码:
/**
-
@Author: Ly
-
@Date: 2020-08-04 23:43
*/
public class JdbcTemplateDemo2 {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext(“bean.xml”);
//2.获取对象
JdbcTemplate jt=ac.getBean(“jdbcTemplate”,JdbcTemplate.class);
//2.执行操作
jt.execute(“insert into account(name,money)values(‘dsa’,1000)”);
}
}
//保存操作
jt.update(“insert into account(name,money)values(?,?)”,“eee”,2222);
//更新操作
jt.update(“update account set name=?,money=? where id=?”,“test”,4567,1);
//删除操作
jt.update(“delete from account where id=?”,8);
//查询所有操作
List accounts=jt.query(“select * from account where money > ?”,new BeanPropertyRowMapper(Account.class),1000f);
//查询一个操作
List account=jt.query(“select * from account where id = ?”,new BeanPropertyRowMapper(Account.class),1);
//查询所有一行一列(使用聚合函数,但不加group by语句)
Integer count=jt.queryForObject(“select count(*) from account where money>?”,Integer.class,1000f);
在执行查询操作时,我们使用了BeanPropertyRowMapper,是由JdbcTemplate提供的一个返回自定义对象的一个类,我们先看一下query方法:public <T> List<T> query(String sql, RowMapper<T> rowMapper)
,BeanPropertyRowMapper实现了RowMapper接口用来对数据进行封装。
自定义AccountRowMapper类实现封装账户信息。
List accounts=jt.query(“select * from account where money > ?”,new AccountRowMapper(),1000f);
class AccountRowMapper implements RowMapper{
/**
-
把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
-
@param rs
-
@param rowNum
-
@return
-
@throws SQLException
*/
@Override
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account=new Account();
account.setId(rs.getInt(“id”));
account.setName(rs.getString(“name”));
account.setMoney(rs.getFloat(“money”));
return account;
}
账户持久层接口与实现类:
/**
-
@Author: Ly
-
@Date: 2020-08-05 12:17
*/
public interface IAccountDao {
/**
-
根据id查询账户
-
@param accountId
-
@return
*/
Account findAccountById(Integer accountId);
/**
-
根据名称查询账户
-
@param accountName
-
@return
*/
Account findAccountByName(String accountName);
/**
-
更新账户
-
@param account
*/
void updateAccount(Account account);
}
/**
-
@Author: Ly
-
@Date: 2020-08-05 12:20
*/
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
Kafka实战笔记
关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图
- Kafka入门
- 为什么选择Kafka
- Karka的安装、管理和配置
- Kafka的集群
- 第一个Kafka程序
afka的生产者
- Kafka的消费者
- 深入理解Kafka
- 可靠的数据传递
- Spring和Kalka的整合
- Sprinboot和Kafka的整合
- Kafka实战之削峰填谷
- 数据管道和流式处理(了解即可)
- Kafka实战之削峰填谷
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
8443)]
[外链图片转存中…(img-YJwjtmnI-1712679068443)]
- Spring和Kalka的整合
- Sprinboot和Kafka的整合
- Kafka实战之削峰填谷
- 数据管道和流式处理(了解即可)
[外链图片转存中…(img-BhDgf4nQ-1712679068443)]
- Kafka实战之削峰填谷
[外链图片转存中…(img-9h4IwUhm-1712679068443)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!