当前位置:网站首页>4.1 声明式事务之JdbcTemplate
4.1 声明式事务之JdbcTemplate
2022-08-04 05:24:00 【猿 白】
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)
指定当前测试类在spring的测试环境中执行,
此时可以通过注入的方式直接获取ioc容器的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]");
}
③查询一条数据为实体类对象
@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);
}
⑤查询单行单列的值
@Test
public void testGetCount(){
String sql = "select count(*) from t_user";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(integer);
}
边栏推荐
- 3000 words, is take you understand machine learning!
- OpenGL绘制圆
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- 基于gRPC编写golang简单C2远控
- 擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
- What are the functions of mall App development?
- 商城App开发都有哪些功能呢
- el-Select 选择器 底部固定
- 编程大杂烩(四)
- 使用Loadrunner进行性能测试
猜你喜欢
Do you think border-radius is just rounded corners?【Various angles】
附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;
Towards Real-Time Multi-Object Tracking (JDE)
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
The symbol table
关于yolo7和gpu
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
[Cocos 3.5.2]开启模型合批
There is an 8 hour difference between the docker installation of mysql and the host.
某母婴小程序加密参数解密
随机推荐
[One step in place] Jenkins installation, deployment, startup (complete tutorial)
【云原生--Kubernetes】Pod资源管理与探针检测
What are the functions of mall App development?
5个开源组件管理小技巧
你以为border-radius只是圆角吗?【各种角度】
SLSA 框架与软件供应链安全防护
关于yolo7和gpu
2023年PMP考试会用新版教材吗?回复来了!
[SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
动态规划总括
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
FPGA学习笔记——知识点总结
获取单选框选中内容
力扣:96.不同的二叉搜索树
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
【SemiDrive源码分析】【MailBox核间通信】47 - 分析RPMSG_IPCC_RPC 方式 单次传输的极限大小 及 极限带宽测试
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work