本文介紹使用最原始的JDBC方式。
1. 建表,表結構如下;
2.配置數據源;
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置一個數據源 --> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/xwh" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="student" class="com.chapter5.Student"> <property name="id" value="4"/> <property name="name" value="jack"/> </bean> <bean id="studentDao" class="com.chapter5.StudentDao"> <property name="dataSource" ref="datasource"/> </bean> </beans>
3.新建相應的POJO類
package com.chapter5; public class Student { int id; String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.新建DAO
package com.chapter5; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.sql.DataSource; public class StudentDao { DataSource dataSource; private static final String student_insert = "insert into student values(?,?)"; /** * 使用Spring的注入方式 * @param dataSource */ public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } /** * 客套話太多 * @param student */ public void saveData(Student student) { Connection conn = null; PreparedStatement stmt = null; try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(student_insert); stmt.setInt(1, student.getId()); stmt.setString(2, student.getName()); stmt.execute(); } catch(SQLException e) { e.printStackTrace(); } finally { try { if(stmt != null) { stmt.close(); } if(conn != null) { conn.close(); } System.out.println("成功入庫!"); } catch(SQLException e) { e.printStackTrace(); } } } }
5.測試
package com.chapter5; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("springdao.xml"); Student student = (Student) factory.getBean("student"); StudentDao studentDao = (StudentDao) factory.getBean("studentDao"); studentDao.saveData(student); } }
· 前言 · 在國內,公網服務器與本地服務器的通信一直是個難題,本地服務器因爲IP是動態變化的,公網服務器沒辦法將請求發送給本地服務器。爲了解決這個問題,所以採用WebSocket協議替換Http協議。爲了實現請求等待,使用Gu
本文分享自華爲雲社區《10分鐘搞懂各種內存溢出案例!!(含完整源碼,建議收藏)》,作者:冰 河。 作爲程序員,多多少少都會遇到一些內存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個假程序員!哈哈,開個玩笑。今天,我
本文分享自華爲雲社區《一文徹底喫透MyBatis源碼!!》,作者:冰 河。 寫在前面 隨着互聯網的發展,越來越多的公司摒棄了Hibernate,而選擇擁抱了MyBatis。而且,很多大廠在面試的時候喜歡問MyBatis底層的原理和源碼實現
本文分享自華爲雲社區《超詳細的Java後臺開發面試題之Spring IOC與AOP》,作者:GaussDB 數據庫。 一、前言 IOC和AOP是Spring中的兩個核心的概念,下面談談對這兩個概念的理解。 二、IOC(Inverse o
一、配置入口 Logback支持XML、Groovy的配置方式,以XML來說,它會默認查找resources目錄下的logback-test.xml(用於測試)/logback.xml文件。 而如果你使用的Spring Boot,那麼你還可
這個其實是一個特別高頻的面試題,松哥也一直很想和大家仔細來聊一聊這個話題,網上關於這塊的文章很多,但是我一直覺得要把這個問題講清楚還有點難度,今天我來試一試,看能不能和小夥伴們把這個問題梳理清楚,當然,如果小夥伴們覺得看文章不過癮,松哥也有
有小夥伴在學習 Spring Security 的遇到一個問題: 箭頭所指的位置報紅,也就是 Spring 容器中沒有找到一個類型爲 HttpSecurity 的 Bean。 小夥伴說如果他在配置類上加 @EnableWebSecurit
前兩天和小夥伴聊了 Spring Security+JWT 實現無狀態登錄,然後有小夥伴反饋了一個問題,感覺這是一個我們平時寫代碼容易忽略的問題,寫一篇文章和小夥伴們聊一聊。 一 問題復原 先來說問題吧,在 Spring Security+
如果你脫口而出說添加 @Order 註解或者是實現 Ordered 接口,那麼恭喜,你掉坑了。 一 @Order 註解和 Ordered 接口 在 Spring 框架中,@Order 是一個非常實用的元註解,它位於 spring-core
本教程將引導您向現有的Web應用程序添加Spring安全策略,在本教程中您將學習如何: 搭建Spring安全策略 部署應用程序 MyEclipse v2023.1.2離線版下載 搭建Spring安全策略 1. 創建一個 scaf
文|苟振東(花名:盛知) Koupleless 項目 committer 螞蟻集團技術專家 本文 5789 字 閱讀 12 分鐘 本篇文章屬於 Koupleless 進階系列文章第二篇,默認讀者對 Koupleless 的基礎概念、能力都
1. 引言 在現代軟件開發過程中,性能優化和故障排查是保證應用穩定運行的關鍵任務之一。Java作爲一種廣泛使用的編程語言,其生態中湧現出了許多優秀的監控和診斷工具,諸如:SkyWalking、Zipkin等,它們幫助開發者和運維人員
pt-osc原理探索及其觸發器的深入分析 > 作者:莫善,某互聯網公司高級 DBA。 > > 愛可生開源社區出品,原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。 > > 本文約 6000 字,預計閱讀需要 20 分鐘。 背景 自工