springboot数据访问基本操作步骤
springboot數據訪問基本操作步驟
步驟一:創建一個springboot項目
配置pom.xml依賴(此階段我沒有配置web場景啟動器)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yyjc.springboot</groupId><artifactId>springboot_ch007</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot_ch007</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><mysql.version>5.1.6</mysql.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.4</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version><scope> runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>步驟二:在application.yaml中連接數據庫
spring:
datasource:
url: jdbc:mysql://localhost:3306/數據庫名
driver-class-name: com.mysql.jdbc.Driver
username:數據庫賬戶名
password: 數據庫賬戶密碼
type:…數據源類型(默認為HikrariDataSource)
具體可配置的內容可通過DataSourceProperties類查看
@ConfigurationProperties(
prefix = “spring.datasource”
)
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
private ClassLoader classLoader;
private String name;
private boolean generateUniqueName = true;
private Class<? extends DataSource> type;
private String driverClassName;
private String url;
private String username;
private String password;
private String jndiName;
private DataSourceInitializationMode initializationMode;
private String platform;
private List schema;
private String schemaUsername;
private String schemaPassword;
private List data;
private String dataUsername;
private String dataPassword;
private boolean continueOnError;
private String separator;
private Charset sqlScriptEncoding;
private EmbeddedDatabaseConnection embeddedDatabaseConnection;
private DataSourceProperties.Xa xa;
private String uniqueName;
…}
步驟三:在測試類中執行測試的sql代碼
@SpringBootTest class SpringbootCh007ApplicationTests {/*** jdbcTemplate是spring對jdbc的封裝,目的是使JDBC更加方便使用。JdbcTemplate處理了資源的建立和釋放。他幫助我們避免一些常見的錯誤,* 比如忘了總要關閉連接。他運行核心的JDBC工作流,如Statement的建立和執行,而我們只需要提供SQL語句和提取結果。* 在JdbcTemplate中執行SQL語句的方法大致分為3類:* execute:可以執行所有SQL語句,一般用于執行DDL語句。* update:用于執行INSERT、UPDATE、DELETE等DML語句。* queryXxx:用于DQL數據查詢語句。*/@AutowiredJdbcTemplate jdbcTemplate;@Testvoid contextLoads() {//返回查詢到的行數Long rows = jdbcTemplate.queryForObject("select count(*) from mdClient",Long.class);System.out.println("rows=" + rows);}}總結
以上是生活随笔為你收集整理的springboot数据访问基本操作步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: restful风格案例
- 下一篇: ssm idea后端接收数据输出在控制台