Spring的jdbc與Hibernate,Mybatis相比較,功能不是特別強大,但是在小型項目中,也到還是比較靈活簡單。
首先可以看看一下傳統的jdbc是如何操作的呢
傳統JDBC
首先呢先要創建一個bean實例,例如Student.java
1 public class Student { 2 3 private Integer id; 4 private String name; 5 private String password; 6 7 public Integer getId() { 8 return id; 9 } 10 11 public void setId(Integer id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public String getPassword() { 24 return password; 25 } 26 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 31 }
為了方便簡單,直接在main里面創建數據源的連接了
1 import org.apache.commons.dbcp.BasicDataSource; 2 import org.springframework.jdbc.core.JdbcTemplate; 3 4 public class TestJdbc { 5 6 public static void main(String[] args) { 7 8 //創建數據源連接池 9 BasicDataSource dataSource = new BasicDataSource(); 10 11 dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); 12 dataSource.setUrl("jdbc:mysql://localhost:3306/stu?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"); 13 dataSource.setUsername("root"); 14 dataSource.setPassword("123456"); 15 16 //創建jdbc模板 17 JdbcTemplate jdbcTemplate = new JdbcTemplate(); 18 jdbcTemplate.setDataSource(dataSource); 19 20 //通過api操作執行sql 21 jdbcTemplate.update("insert into student(stu_name,stu_pwd) values(?,?);", "jack","46asd4634"); 22 23 } 24 25 }
因為我這里使用的是mysql8.0,所以驅動名和url有所不一樣,可以參考https://www.cnblogs.com/zhangyuanbo/p/11248334.html
以上就是傳統的JDBC操作數據庫,然后我們用Spring的xml來配置一下,現在用的是DBCP連接池來測試的:
步驟類似,先創建實體類Student.java,然后需要一個StudentDao.java,簡單一點,什么接口類實現類Service的,通通不要了,這些做起來應該也不是什么難事吧。
要操作數據庫,當然要有CRUD什么的啦,那就整一個update()方法吧
public void update(Student stu) { String sql = "update student set stu_name=? where stu_id=?"; Object[] pro = {stu.getName(),stu.getId()}; jdbcTemplate.update(sql, pro); }
我這里是根據Id來執行修改操作的,jdbc模板的方法有很多
我用的是這里紅框框的方法,第一個參數很顯然啦,意思就是你操作數據庫執行的sql語句,第二個就是sql中要傳的參數,比如我這里的sql“update student set stu_name=? where stu_id=?”,參數就是stu_name和stu_id。
哦,對了,有一件很重要的事情,可不要忘了JdbcTemplate了,不創建一下,給個setter()方法的話,就會報錯的——“jdbcTemplate is not writable or has an anvalid setter method..............”,很明顯告訴我們需要一個jdbcTemplate的setter()方法呀,所以完整的StudentDao.java就寫成這樣:
1 import org.springframework.jdbc.core.JdbcTemplate; 2 3 public class StudentDao { 4 5 private JdbcTemplate jdbcTemplate; 6 7 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 8 this.jdbcTemplate = jdbcTemplate; 9 } 10 11 public void update(Student stu) { 12 String sql = "update student set stu_name=? where stu_id=?"; 13 Object[] pro = {stu.getName(),stu.getId()}; 14 15 jdbcTemplate.update(sql, pro); 16 } 17 18 }
再整一個bean.xml唄,當然了,實際項目中可不這樣命名,一般是“applicationContext.xml”,這里就隨意了,怎么簡單方便怎么來
在寫xml的時候,比如“<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"></bean>”,在class=""這里面按alt+/沒有代碼提示很煩,像我這樣不喜歡敲這么長一串的人來說,特別記憶力又不好,一不小心敲錯了,最后報錯了呀,找起來也煩。於是,還可以這樣來解決:
Windows→Preferences
里面那個紅××請忽略,網絡代理問題,與這個項目無關。再點“Add”:
注意:外部引用的是你要使用的炸包(jar)對應的“.xsd”格式文件,最后點確定就可以了。效果如下:
有意思吧,方便吧,懶癌患者的福音啊!!!!
咳咳咳,說正事,回到我們的bean.xml的寫法
為了好理解,我們就倒着來寫吧,以后就順着寫咯
首先肯定要配置Dao的bean
<bean id="studentDao" class="com.yuanbo.xml.StudentDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>
怎么ide里的格式好好地,復制過來就這樣了,算了,懶得改了,繼續一頓操作
我們把jdbcTemplate注入到這個Dao的bean中,那么,必須要有一個jdbcTemplate的bean撒,
也可以從最開始的傳統IDBC可以看到,我們不是new了一個jdbcTemplate嘛,一看到new,那
么,改成xml配置文件形式,得整一個相對應的bean出來撒,於是,吧啦啦啦,整出來了:
1 <!-- 創建模板,注入數據源 --> 2 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 3 <property name="dataSource" ref="dataSource"></property> 4 </bean>
你看吧,現在格式又好好的了,,,,,,emmmm,想起來了,好像是復制的時候沒有從定
格復制,少復制了一個“Tab”,算求了,無關緊要,沒得啥子強迫症
模板里面必須要注入jdbcTemplate哦,從傳統JDBC這里“jdbcTemplate.setDataSource(dataSource);”
有了set方法,就要想到,那得注入了呀不是,注入就等同於set
既然注入了,那不是還缺少一個dataSource呀,這還不灑灑水啊,明顯需要再整一個數據源的bean撒,吧啦啦啦啦,小魔仙,全身變!出來吧
<!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver "></property> <property name="url" value="jdbc:mysql://localhost:3306/stu?serverTimezone=GMT%2B8"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property>
</bean>
哦,對了,有一個塊選中的快捷鍵,shift+alt+a,就可以了,其實我還是喜歡代碼對齊的,可讀性必須高,
寫代碼的基本准則。再按一下組合鍵就可以退出當前模式了
這里的url不要在意,mysql8.0的url寫法又好幾種,這是比較隨意的寫法
最后看看bean.xml的完整代碼:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context.xsd"> 8 9 <!-- <context:property-placeholder location="classpath:com/yuanbo/xml/jdbc.properties"/> --> 10 11 <!-- 配置數據源 --> 12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 13 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver "></property> 14 <property name="url" value="jdbc:mysql://localhost:3306/stu?serverTimezone=GMT%2B8"></property> 15 <property name="username" value="root"></property> 16 <property name="password" value="123456"></property> 17 </bean> 18 19 <!-- 創建模板,注入數據源 --> 20 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 21 <property name="dataSource" ref="dataSource"></property> 22 </bean> 23 24 <!-- 配置Dao --> 25 <bean id="studentDao" class="com.yuanbo.xml.StudentDao"> 26 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 27 </bean> 28 </beans>
注釋的別管,我把數據源里面屬性提出來放到.properties文件里,注釋掉的這句話就有用了。
最后,來個測試類吧
1 import org.junit.Test; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.ConfigurableApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class TestDBCP { 7 8 @Test 9 public void demo01() { 10 Student stu = new Student(); 11 12 String xmlPath = "com/yuanbo/xml/beans.xml"; 13 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); 14 StudentDao dao = (StudentDao) applicationContext.getBean("studentDao"); 15 16 stu.setName("ali"); 17 stu.setId(2); 18 dao.update(stu); 19 20 ((ConfigurableApplicationContext)applicationContext).close(); 21 } 22 23 }
好像還忘記了啥東西,哦,是了,貼上數據庫的吧
還有炸包