当前位置:网站首页>4.1 JdbcTemplate for declarative transactions
4.1 JdbcTemplate for declarative transactions
2022-08-04 05:31:00 【ape white】
4、声明式事务
4.1、JdbcTemplate
4.1.1、简介
Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
4.1.2、准备工作
①加入依赖
<dependencies>
<!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring 持久化层支持jar包 -->
<!-- Spring 在执行持久化层操作、与持久化层技术进行整合过程中,需要使用orm、jdbc、tx三个
jar包 -->
<!-- 导入 orm 包就可以通过 Maven 的依赖传递性把其他两个也导入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring 测试相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.1</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
</dependencies>
②创建jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=root
③配置Spring的配置文件
<!--引入jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
4.1.3、测试
①在测试类装配 JdbcTemplate
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JDBCTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
}
@RunWith
(SpringJUnit4ClassRunner.class)
Specifies that the current test class is inspringExecuted in the test environment,
At this point, it can be obtained directly by injectionioc容器的bean
@ContextConfiguration
(“classpath:spring-jdbc.xml”)
设置spring测试环境的配置文件
②测试增删改功能
增删改都是update:
@Test
public void jdbcInsert(){
String sql = "insert into t_user values(null,?,?,?,?,?)";
jdbcTemplate.update(sql,"root","3333",33,"女","[email protected]");
}
③Query a piece of data as an entity class object
@Test
public void testGetUserById(){
String sql = "select * from t_user where id = ?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 2);
System.out.println(user);
}
④查询多条数据为一个list集合
@Test
public void testGetAllUser(){
String sql = "select * from t_user";
List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
query.forEach(System.out::println);
}
⑤Query the value of a single row and single column
@Test
public void testGetCount(){
String sql = "select count(*) from t_user";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(integer);
}
边栏推荐
- npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
- 5个开源组件管理小技巧
- 动态规划总括
- System design. How to design a spike system (full version transfer)
- 某母婴小程序加密参数解密
- C语言 -- 操作符详解
- Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
- 详解八大排序
- Cannot read properties of null (reading ‘insertBefore‘)
- Get the selected content of the radio box
猜你喜欢
高性能高可靠性高扩展性分布式防火墙架构
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
Teenage Achievement Hackers Need These Skills
Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
8.03 Day34---BaseMapper查询语句用法
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
About yolo7 and gpu
Performance testing with Loadrunner
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
随机推荐
力扣:746. 使用最小花费爬楼梯
如何低成本修bug?测试左移给你答案
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
MySQL数据库面试题总结(2022最新版)
《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
详解八大排序
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
7.18 Day23----标记语言
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
Write golang simple C2 remote control based on gRPC
FPGA学习笔记——知识点总结
8. Custom mapping resultMap
7.16 Day22---MYSQL(Dao模式封装JDBC)
MySql数据恢复方法个人总结
SLSA 框架与软件供应链安全防护
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
The 2022 PMP exam has been delayed, should we be happy or worried?
Bolb analysis of image processing (1)