關於Spring的JDBC鏈接mysql(與傳統jdbc比較)

Spring的jdbc與Hibernate,Mybatis相比較,功能不是特別強大,可是在小型項目中,也到仍是比較靈活簡單。html

首先能夠看看一下傳統的jdbc是如何操做的呢java

傳統JDBC

首先呢先要建立一個bean實例,例如Student.javamysql

 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裏面建立數據源的鏈接了spring

 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有所不同,能夠參考http://www.javashuo.com/article/p-txqsuoos-cx.htmlsql

以上就是傳統的JDBC操做數據庫,而後咱們用Spring的xml來配置一下,如今用的是DBCP鏈接池來測試的:數據庫

步驟相似,先建立實體類Student.java,而後須要一個StudentDao.java,簡單一點,什麼接口類實現類Service的,統統不要了,這些作起來應該也不是什麼難事吧。apache

要操做數據庫,固然要有CRUD什麼的啦,那就整一個update()方法吧api

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。app

哦,對了,有一件很重要的事情,可不要忘了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 }

好像還忘記了啥東西,哦,是了,貼上數據庫的吧

還有炸包

相關文章
相關標籤/搜索