## 13.1、環境搭建 ### 13.1.1、創建module ![image](https://img2023.cnblogs.com/blog/2052479/202308/2052479-20230821075157720-1152690363.png) ### 13.1.2、選擇maven ...
13.1、環境搭建
13.1.1、創建module
13.1.2、選擇maven
13.1.3、設置module名稱和路徑
13.1.4、module初始狀態
13.1.5、配置打包方式和依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.rain</groupId>
<artifactId>spring_jdbcTemplate</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Spring IOC相關 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring 持久化層相關 -->
<!-- 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>5.1.49</version>
</dependency>
<!-- 數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
</dependencies>
</project>
13.1.6、創建jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
13.1.7、創建Spring的配置文件
<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 導入外部屬性文件 -->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!-- 配置數據源 -->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<!--通過${key}的方式訪問外部屬性文件的value-->
<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>
<!-- 配置 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 裝配數據源 -->
<property name="dataSource" ref="datasource"></property>
</bean>
</beans>
13.2、Spring整合Junit4單元測試
13.2.1、創建測試類
package org.rain.spring.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author liaojy
* @date 2023/8/22 - 7:43
*/
// @RunWith註解:表示使用 SpringJUnit4ClassRunner 運行器來運行測試方法
// 這樣測試類便在Spring的測試環境中執行,此時就可以通過註入的方式直接獲取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration註解:用來指定載入ApplicationContext的配置文件或配置類
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testInsert(){
String sql = "insert into t_user values(null,?,?,?,?,?)";
jdbcTemplate.update(sql,"jdbcTemplate","123",23,"男","[email protected]");
}
}
13.2.2、執行測試方法
++++++++++++++++++++++++++++++分割線++++++++++++++++++++++++++++++
JdbcTemplate的增刪改都是用update這個方法
++++++++++++++++++++++++++++++分割線++++++++++++++++++++++++++++++
13.3、JdbcTemplate的查詢功能
13.3.1、創建實體類User
package org.rain.spring.pojo;
/**
* @author liaojy
* @date 2023/8/22 - 8:12
*/
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
private String email;
public User(Integer id, String username, String password, Integer age, String gender, String email) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.gender = gender;
this.email = email;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
}
13.3.2、查詢單條數據
@Test
public void testGetUserById(){
String sql = "select * from t_user where id = ?";
// 第一個參數是設置要執行的sql語句,第二個參數是設置查詢結果和實體類的對應關係,後面的參數列表是設置sql語句中占位符的值
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), 10);
System.out.println(user);
}
13.3.3、查詢多條數據
@Test
public void testGetAllUser(){
String sql = "select * from t_user";
List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
for (User user : users) {
System.out.println(user);
}
}
13.3.4、查詢單行單列
@Test
public void testUserCount(){
String sql = "select count(*) from t_user";
// 第二個參數是設置查詢結果對應的Java類型;如果sql中有占位符,後面可以用參數列表來設置sql語句中占位符的值
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("用戶數量為:"+integer);
}