ホームページ  >  記事  >  Java  >  springJDBC01 は springJDBC を使用してデータベースを操作します

springJDBC01 は springJDBC を使用してデータベースを操作します

PHP中文网
PHP中文网オリジナル
2017-07-14 18:13:151374ブラウズ

1 springJDBCとは

Spring は、JDBC アクセスを抽象化し、一貫した API を提供することで、 JDBC プログラミングのワークロードを簡素化します。必要なのは、 SQL を宣言し、 適切な SpringJDBC フレームワーク API を呼び出し、 結果セットを処理することだけです。トランザクションは Spring によって管理され、JDBC チェック例外を Spring と一貫性のある非チェック例外に変換するため、開発が簡素化されます。 従来の jdbc を使用してデータベースを操作する手順: 接続の取得 → ステートメントの作成 → データ操作の実行 → 結果の取得 → ステートメントのクローズ → 結果セットのクローズ → 接続のクローズ; Spring JDBC はテンプレート クラス org.springframework.jdbc.core によってカプセル化されます。 .JdbcTemplate ボイラープレート コードを使用しなくても、ユーザーはテンプレート クラスを通じてほとんどのデータ アクセス操作を簡単に完了できます。

2 準備

2.1瓶パッケージ

spring-jdbc: springjdbc パッケージ

mysql: MySQL ドライバーパッケージ

dbcp: データベース接続プール

spring-webmvc: springmvc フレームワークパッケージ

アノテーション: @resource はパッケージを使用する必要があり、パッケージは Tomcat で利用可能です (Web の場合)プロジェクトと実行環境はTomcatなので、このパッケージをインポートする必要はありません
Junit:単体テストパッケージ

2.2 データベース(mysqlデータベース5.4を使用)

リーリー
関連テーブル
3 環境設定(Eclipseを使用)

3.1 Mavenを使用して関連するjarパッケージをインポートします

リーリー
pom.xml
3.2 データベース関連情報を格納するプロパティファイルを作成する

リーリー
mysql.properties
3.3 Spring設定ファイルを作成する 3.3.1 プロパティファイルでBeanを設定する

3.3.2 データベース接続プールの設定

3.3.3 jdbcTemplateの設定

3.3.4 コンポーネントスキャンの設定

リーリー
spring_mysql.xml

  注意:我们不用配置spirng的主控制器,因为我们只是操作持久层;虽然我们用到了spring容器,但是我们可以通过编写代码来启动容器

  3.4 项目结构图

    

 

4 代码实现数据库操作

  4.1 编写实体类

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> com.baobaotao.entity;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.Serializable;
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Date;
</span><span style="color: #008080"> 5</span> 
<span style="color: #008080"> 6</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> User <span style="color: #0000ff">implements</span><span style="color: #000000"> Serializable {
</span><span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = -3573627859368072117L<span style="color: #000000">;
</span><span style="color: #008080"> 9</span>     
<span style="color: #008080">10</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> Integer userId;
</span><span style="color: #008080">11</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> String userName;
</span><span style="color: #008080">12</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> Integer credits;
</span><span style="color: #008080">13</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> String password;
</span><span style="color: #008080">14</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> Date lastVisit;
</span><span style="color: #008080">15</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> String lastIp;
</span><span style="color: #008080">16</span>     
<span style="color: #008080">17</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> User() {
</span><span style="color: #008080">18</span>         <span style="color: #0000ff">super</span><span style="color: #000000">();
</span><span style="color: #008080">19</span>         <span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated constructor stub</span>
<span style="color: #008080">20</span> <span style="color: #000000">    }
</span><span style="color: #008080">21</span>     
<span style="color: #008080">22</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> User(Integer userId, String userName, Integer credits, String password, Date lastVisit, String lastIp) {
</span><span style="color: #008080">23</span>         <span style="color: #0000ff">super</span><span style="color: #000000">();
</span><span style="color: #008080">24</span>         <span style="color: #0000ff">this</span>.userId =<span style="color: #000000"> userId;
</span><span style="color: #008080">25</span>         <span style="color: #0000ff">this</span>.userName =<span style="color: #000000"> userName;
</span><span style="color: #008080">26</span>         <span style="color: #0000ff">this</span>.credits =<span style="color: #000000"> credits;
</span><span style="color: #008080">27</span>         <span style="color: #0000ff">this</span>.password =<span style="color: #000000"> password;
</span><span style="color: #008080">28</span>         <span style="color: #0000ff">this</span>.lastVisit =<span style="color: #000000"> lastVisit;
</span><span style="color: #008080">29</span>         <span style="color: #0000ff">this</span>.lastIp =<span style="color: #000000"> lastIp;
</span><span style="color: #008080">30</span> <span style="color: #000000">    }
</span><span style="color: #008080">31</span> 
<span style="color: #008080">32</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">33</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span><span style="color: #000000"> hashCode() {
</span><span style="color: #008080">34</span>         <span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> prime = 31<span style="color: #000000">;
</span><span style="color: #008080">35</span>         <span style="color: #0000ff">int</span> result = 1<span style="color: #000000">;
</span><span style="color: #008080">36</span>         result = prime * result + ((lastIp == <span style="color: #0000ff">null</span>) ? 0<span style="color: #000000"> : lastIp.hashCode());
</span><span style="color: #008080">37</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> result;
</span><span style="color: #008080">38</span> <span style="color: #000000">    }
</span><span style="color: #008080">39</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">40</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> equals(Object obj) {
</span><span style="color: #008080">41</span>         <span style="color: #0000ff">if</span> (<span style="color: #0000ff">this</span> ==<span style="color: #000000"> obj)
</span><span style="color: #008080">42</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
</span><span style="color: #008080">43</span>         <span style="color: #0000ff">if</span> (obj == <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">44</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">45</span>         <span style="color: #0000ff">if</span> (getClass() !=<span style="color: #000000"> obj.getClass())
</span><span style="color: #008080">46</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">47</span>         User other =<span style="color: #000000"> (User) obj;
</span><span style="color: #008080">48</span>         <span style="color: #0000ff">if</span> (lastIp == <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008080">49</span>             <span style="color: #0000ff">if</span> (other.lastIp != <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">50</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">51</span>         } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (!<span style="color: #000000">lastIp.equals(other.lastIp))
</span><span style="color: #008080">52</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">53</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
</span><span style="color: #008080">54</span> <span style="color: #000000">    }
</span><span style="color: #008080">55</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> Integer getUserId() {
</span><span style="color: #008080">56</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> userId;
</span><span style="color: #008080">57</span> <span style="color: #000000">    }
</span><span style="color: #008080">58</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setUserId(Integer userId) {
</span><span style="color: #008080">59</span>         <span style="color: #0000ff">this</span>.userId =<span style="color: #000000"> userId;
</span><span style="color: #008080">60</span> <span style="color: #000000">    }
</span><span style="color: #008080">61</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String getUserName() {
</span><span style="color: #008080">62</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> userName;
</span><span style="color: #008080">63</span> <span style="color: #000000">    }
</span><span style="color: #008080">64</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setUserName(String userName) {
</span><span style="color: #008080">65</span>         <span style="color: #0000ff">this</span>.userName =<span style="color: #000000"> userName;
</span><span style="color: #008080">66</span> <span style="color: #000000">    }
</span><span style="color: #008080">67</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> Integer getCredits() {
</span><span style="color: #008080">68</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> credits;
</span><span style="color: #008080">69</span> <span style="color: #000000">    }
</span><span style="color: #008080">70</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setCredits(Integer credits) {
</span><span style="color: #008080">71</span>         <span style="color: #0000ff">this</span>.credits =<span style="color: #000000"> credits;
</span><span style="color: #008080">72</span> <span style="color: #000000">    }
</span><span style="color: #008080">73</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String getPassword() {
</span><span style="color: #008080">74</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> password;
</span><span style="color: #008080">75</span> <span style="color: #000000">    }
</span><span style="color: #008080">76</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setPassword(String password) {
</span><span style="color: #008080">77</span>         <span style="color: #0000ff">this</span>.password =<span style="color: #000000"> password;
</span><span style="color: #008080">78</span> <span style="color: #000000">    }
</span><span style="color: #008080">79</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> Date getLastVisit() {
</span><span style="color: #008080">80</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> lastVisit;
</span><span style="color: #008080">81</span> <span style="color: #000000">    }
</span><span style="color: #008080">82</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setLastVisit(Date lastVisit) {
</span><span style="color: #008080">83</span>         <span style="color: #0000ff">this</span>.lastVisit =<span style="color: #000000"> lastVisit;
</span><span style="color: #008080">84</span> <span style="color: #000000">    }
</span><span style="color: #008080">85</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String getLastIp() {
</span><span style="color: #008080">86</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> lastIp;
</span><span style="color: #008080">87</span> <span style="color: #000000">    }
</span><span style="color: #008080">88</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setLastIp(String lastIp) {
</span><span style="color: #008080">89</span>         <span style="color: #0000ff">this</span>.lastIp =<span style="color: #000000"> lastIp;
</span><span style="color: #008080">90</span> <span style="color: #000000">    }
</span><span style="color: #008080">91</span> 
<span style="color: #008080">92</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">93</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String toString() {
</span><span style="color: #008080">94</span>         <span style="color: #0000ff">return</span> "User [userId=" + userId + ", userName=" + userName + ", credits=" + credits + ", password=" +<span style="color: #000000"> password
</span><span style="color: #008080">95</span>                 + ", lastVisit=" + lastVisit + ", lastIp=" + lastIp + "]"<span style="color: #000000">;
</span><span style="color: #008080">96</span> <span style="color: #000000">    }
</span><span style="color: #008080">97</span> 
<span style="color: #008080">98</span> }
User.java
<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> com.baobaotao.entity;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.Serializable;
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Date;
</span><span style="color: #008080"> 5</span> 
<span style="color: #008080"> 6</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LoginLog <span style="color: #0000ff">implements</span><span style="color: #000000"> Serializable {
</span><span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = 5176708814959439551L<span style="color: #000000">;
</span><span style="color: #008080"> 9</span>     
<span style="color: #008080">10</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> Integer loginLogId;
</span><span style="color: #008080">11</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> String userId;
</span><span style="color: #008080">12</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> String ip;
</span><span style="color: #008080">13</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> Date loginDatetime;
</span><span style="color: #008080">14</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">15</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span><span style="color: #000000"> hashCode() {
</span><span style="color: #008080">16</span>         <span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> prime = 31<span style="color: #000000">;
</span><span style="color: #008080">17</span>         <span style="color: #0000ff">int</span> result = 1<span style="color: #000000">;
</span><span style="color: #008080">18</span>         result = prime * result + ((ip == <span style="color: #0000ff">null</span>) ? 0<span style="color: #000000"> : ip.hashCode());
</span><span style="color: #008080">19</span>         result = prime * result + ((loginDatetime == <span style="color: #0000ff">null</span>) ? 0<span style="color: #000000"> : loginDatetime.hashCode());
</span><span style="color: #008080">20</span>         result = prime * result + ((loginLogId == <span style="color: #0000ff">null</span>) ? 0<span style="color: #000000"> : loginLogId.hashCode());
</span><span style="color: #008080">21</span>         result = prime * result + ((userId == <span style="color: #0000ff">null</span>) ? 0<span style="color: #000000"> : userId.hashCode());
</span><span style="color: #008080">22</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> result;
</span><span style="color: #008080">23</span> <span style="color: #000000">    }
</span><span style="color: #008080">24</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">25</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> equals(Object obj) {
</span><span style="color: #008080">26</span>         <span style="color: #0000ff">if</span> (<span style="color: #0000ff">this</span> ==<span style="color: #000000"> obj)
</span><span style="color: #008080">27</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
</span><span style="color: #008080">28</span>         <span style="color: #0000ff">if</span> (obj == <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">29</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">30</span>         <span style="color: #0000ff">if</span> (getClass() !=<span style="color: #000000"> obj.getClass())
</span><span style="color: #008080">31</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">32</span>         LoginLog other =<span style="color: #000000"> (LoginLog) obj;
</span><span style="color: #008080">33</span>         <span style="color: #0000ff">if</span> (ip == <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008080">34</span>             <span style="color: #0000ff">if</span> (other.ip != <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">35</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">36</span>         } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (!<span style="color: #000000">ip.equals(other.ip))
</span><span style="color: #008080">37</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">38</span>         <span style="color: #0000ff">if</span> (loginDatetime == <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008080">39</span>             <span style="color: #0000ff">if</span> (other.loginDatetime != <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">40</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">41</span>         } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (!<span style="color: #000000">loginDatetime.equals(other.loginDatetime))
</span><span style="color: #008080">42</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">43</span>         <span style="color: #0000ff">if</span> (loginLogId == <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008080">44</span>             <span style="color: #0000ff">if</span> (other.loginLogId != <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">45</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">46</span>         } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (!<span style="color: #000000">loginLogId.equals(other.loginLogId))
</span><span style="color: #008080">47</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">48</span>         <span style="color: #0000ff">if</span> (userId == <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008080">49</span>             <span style="color: #0000ff">if</span> (other.userId != <span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">50</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">51</span>         } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (!<span style="color: #000000">userId.equals(other.userId))
</span><span style="color: #008080">52</span>             <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #008080">53</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
</span><span style="color: #008080">54</span> <span style="color: #000000">    }
</span><span style="color: #008080">55</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> Integer getLoginLogId() {
</span><span style="color: #008080">56</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> loginLogId;
</span><span style="color: #008080">57</span> <span style="color: #000000">    }
</span><span style="color: #008080">58</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setLoginLogId(Integer loginLogId) {
</span><span style="color: #008080">59</span>         <span style="color: #0000ff">this</span>.loginLogId =<span style="color: #000000"> loginLogId;
</span><span style="color: #008080">60</span> <span style="color: #000000">    }
</span><span style="color: #008080">61</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String getUserId() {
</span><span style="color: #008080">62</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> userId;
</span><span style="color: #008080">63</span> <span style="color: #000000">    }
</span><span style="color: #008080">64</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setUserId(String userId) {
</span><span style="color: #008080">65</span>         <span style="color: #0000ff">this</span>.userId =<span style="color: #000000"> userId;
</span><span style="color: #008080">66</span> <span style="color: #000000">    }
</span><span style="color: #008080">67</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String getIp() {
</span><span style="color: #008080">68</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> ip;
</span><span style="color: #008080">69</span> <span style="color: #000000">    }
</span><span style="color: #008080">70</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setIp(String ip) {
</span><span style="color: #008080">71</span>         <span style="color: #0000ff">this</span>.ip =<span style="color: #000000"> ip;
</span><span style="color: #008080">72</span> <span style="color: #000000">    }
</span><span style="color: #008080">73</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> Date getLoginDatetime() {
</span><span style="color: #008080">74</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> loginDatetime;
</span><span style="color: #008080">75</span> <span style="color: #000000">    }
</span><span style="color: #008080">76</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setLoginDatetime(Date loginDatetime) {
</span><span style="color: #008080">77</span>         <span style="color: #0000ff">this</span>.loginDatetime =<span style="color: #000000"> loginDatetime;
</span><span style="color: #008080">78</span> <span style="color: #000000">    }
</span><span style="color: #008080">79</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> LoginLog() {
</span><span style="color: #008080">80</span>         <span style="color: #0000ff">super</span><span style="color: #000000">();
</span><span style="color: #008080">81</span>         <span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated constructor stub</span>
<span style="color: #008080">82</span> <span style="color: #000000">    }
</span><span style="color: #008080">83</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> LoginLog(Integer loginLogId, String userId, String ip, Date loginDatetime) {
</span><span style="color: #008080">84</span>         <span style="color: #0000ff">super</span><span style="color: #000000">();
</span><span style="color: #008080">85</span>         <span style="color: #0000ff">this</span>.loginLogId =<span style="color: #000000"> loginLogId;
</span><span style="color: #008080">86</span>         <span style="color: #0000ff">this</span>.userId =<span style="color: #000000"> userId;
</span><span style="color: #008080">87</span>         <span style="color: #0000ff">this</span>.ip =<span style="color: #000000"> ip;
</span><span style="color: #008080">88</span>         <span style="color: #0000ff">this</span>.loginDatetime =<span style="color: #000000"> loginDatetime;
</span><span style="color: #008080">89</span> <span style="color: #000000">    }
</span><span style="color: #008080">90</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">91</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String toString() {
</span><span style="color: #008080">92</span>         <span style="color: #0000ff">return</span> "LoginLog [loginLogId=" + loginLogId + ", userId=" + userId + ", ip=" + ip + ", loginDatetime="
<span style="color: #008080">93</span>                 + loginDatetime + "]"<span style="color: #000000">;
</span><span style="color: #008080">94</span> <span style="color: #000000">    }    
</span><span style="color: #008080">95</span>     
<span style="color: #008080">96</span> }
LoginLog.java

  4.2 编写UserDao接口

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> com.baobaotao.dao;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.util.List;
</span><span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> <span style="color: #0000ff">import</span><span style="color: #000000"> com.baobaotao.entity.User;
</span><span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span><span style="color: #000000"> UserDao {
</span><span style="color: #008080"> 8</span>     <span style="color: #008000">/**</span>
<span style="color: #008080"> 9</span> <span style="color: #008000">     * 向用户表中添加记录
</span><span style="color: #008080">10</span> <span style="color: #008000">     * </span><span style="color: #808080">@param</span><span style="color: #008000"> user 用户表实体对象
</span><span style="color: #008080">11</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">12</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> insert(User user);
</span><span style="color: #008080">13</span>     
<span style="color: #008080">14</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">15</span> <span style="color: #008000">     * 查询所有用户数据
</span><span style="color: #008080">16</span> <span style="color: #008000">     * </span><span style="color: #808080">@return</span><span style="color: #008000"> 由查询到记录组成的集合
</span><span style="color: #008080">17</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">18</span>     <span style="color: #0000ff">public</span> List<User><span style="color: #000000"> findAll();
</span><span style="color: #008080">19</span> 
<span style="color: #008080">20</span> }
UserDao.java

  4.3 编写UserDao接口的实现类UserDaoImpl  

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> com.baobaotao.dao;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.ResultSet;
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.SQLException;
</span><span style="color: #008080"> 5</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.util.List;
</span><span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.annotation.Resource;
</span><span style="color: #008080"> 8</span> 
<span style="color: #008080"> 9</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.jdbc.core.JdbcTemplate;
</span><span style="color: #008080">10</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.jdbc.core.RowMapper;
</span><span style="color: #008080">11</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Repository;
</span><span style="color: #008080">12</span> 
<span style="color: #008080">13</span> <span style="color: #0000ff">import</span><span style="color: #000000"> com.baobaotao.entity.User;
</span><span style="color: #008080">14</span> 
<span style="color: #008080">15</span> @Repository("userDao"<span style="color: #000000">)
</span><span style="color: #008080">16</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> UserDaoImpl <span style="color: #0000ff">implements</span><span style="color: #000000"> UserDao {
</span><span style="color: #008080">17</span>     
<span style="color: #008080">18</span>     @Resource(name="jt"<span style="color: #000000">)
</span><span style="color: #008080">19</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> JdbcTemplate jt;
</span><span style="color: #008080">20</span> 
<span style="color: #008080">21</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> insert(User user) {
</span><span style="color: #008080">22</span>         
<span style="color: #008080">23</span>         String sql = "INSERT INTO t_user " + 
<span style="color: #008080">24</span>                 "(user_name, password) " + 
<span style="color: #008080">25</span>                 "VALUES " + 
<span style="color: #008080">26</span>                 "(?, ?) "<span style="color: #000000">;
</span><span style="color: #008080">27</span>         Object [] args =<span style="color: #000000"> {user.getUserName(), user.getPassword()};
</span><span style="color: #008080">28</span>         Integer num =<span style="color: #000000"> jt.update(sql, args);
</span><span style="color: #008080">29</span>         <span style="color: #0000ff">if</span>(num > 0<span style="color: #000000">) {
</span><span style="color: #008080">30</span>             System.out.println("插入数据成功"<span style="color: #000000">);
</span><span style="color: #008080">31</span>         } <span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #008080">32</span>             System.out.println("插入数据失败"<span style="color: #000000">);
</span><span style="color: #008080">33</span> <span style="color: #000000">        }
</span><span style="color: #008080">34</span>         
<span style="color: #008080">35</span> <span style="color: #000000">    }
</span><span style="color: #008080">36</span>     
<span style="color: #008080">37</span>     <span style="color: #0000ff">public</span> List<User><span style="color: #000000"> findAll() {
</span><span style="color: #008080">38</span>         String sql = "SELECT * FROM t_user "<span style="color: #000000">;
</span><span style="color: #008080">39</span>         List<User> users = jt.query(sql, <span style="color: #0000ff">new</span><span style="color: #000000"> UserRowMapper());
</span><span style="color: #008080">40</span>         <span style="color: #0000ff">return</span><span style="color: #000000"> users;
</span><span style="color: #008080">41</span> <span style="color: #000000">    }
</span><span style="color: #008080">42</span>     
<span style="color: #008080">43</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">44</span> <span style="color: #008000">     * 结果集处理:经每一条查询记录转变成一个实体对象
</span><span style="color: #008080">45</span> <span style="color: #008000">     * </span><span style="color: #808080">@author</span><span style="color: #008000"> 三少
</span><span style="color: #008080">46</span> <span style="color: #008000">     *
</span><span style="color: #008080">47</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">48</span>     <span style="color: #0000ff">class</span> UserRowMapper <span style="color: #0000ff">implements</span> RowMapper<User><span style="color: #000000"> {
</span><span style="color: #008080">49</span> 
<span style="color: #008080">50</span>         <span style="color: #0000ff">public</span> User mapRow(ResultSet rs, <span style="color: #0000ff">int</span> rowNum) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
</span><span style="color: #008080">51</span>             User user = <span style="color: #0000ff">new</span><span style="color: #000000"> User();
</span><span style="color: #008080">52</span>             user.setUserId(rs.getInt("user_id"<span style="color: #000000">));
</span><span style="color: #008080">53</span>             user.setUserName(rs.getString("user_name"<span style="color: #000000">));
</span><span style="color: #008080">54</span>             user.setCredits(rs.getInt("credits"<span style="color: #000000">));
</span><span style="color: #008080">55</span>             user.setPassword(rs.getString("password"<span style="color: #000000">));
</span><span style="color: #008080">56</span>             user.setLastVisit(rs.getDate("last_visit"<span style="color: #000000">));
</span><span style="color: #008080">57</span>             user.setLastIp(rs.getString("last_ip"<span style="color: #000000">));
</span><span style="color: #008080">58</span>             <span style="color: #0000ff">return</span><span style="color: #000000"> user;
</span><span style="color: #008080">59</span> <span style="color: #000000">        }
</span><span style="color: #008080">60</span> <span style="color: #000000">    }
</span><span style="color: #008080">61</span>     
<span style="color: #008080">62</span> }
UserDaoImpl

    使用springJDBC操作数据库时,程序员只需要编写相关的sql语句,待定参数可以用?代替,然后调用JdbcTemplate类的相关方法来执行sql语句就行啦。

    JdbcTemplate类中的主要方法

      execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句; 
      update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句; 
      batchUpdate方法用于执行批处理相关语句; 
      query方法及queryForXXX方法:用于执行查询相关语句 
      call方法:用于执行存储过程、函数相关语句。

      注意:

        使用query方法时有三个参数

          参数1  sql语句字符串

          参数2  未知参数组

          参数3  查询结果处理(就是讲每一条查询记录变成一个实体对象,三少的一般做法是编写一个实现了RowMapper接口的内部类,然后创建一个该内部类对象来作为参数3

  4.4 项目结构图

    

 

5 测试类

  5.1 测试前需要启动spring容器,因为我们的代码中使用到了spring容器的功能

    

  5.2 编写测试方法

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> testDao;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.util.List;
</span><span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.junit.Before;
</span><span style="color: #008080"> 6</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.junit.Test;
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.context.ApplicationContext;
</span><span style="color: #008080"> 8</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.context.support.ClassPathXmlApplicationContext;
</span><span style="color: #008080"> 9</span> 
<span style="color: #008080">10</span> <span style="color: #0000ff">import</span><span style="color: #000000"> com.baobaotao.dao.UserDao;
</span><span style="color: #008080">11</span> <span style="color: #0000ff">import</span><span style="color: #000000"> com.baobaotao.entity.User;
</span><span style="color: #008080">12</span> 
<span style="color: #008080">13</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> TestDao {
</span><span style="color: #008080">14</span>     
<span style="color: #008080">15</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> ApplicationContext ac;
</span><span style="color: #008080">16</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> UserDao userDao;
</span><span style="color: #008080">17</span>     
<span style="color: #008080">18</span> <span style="color: #000000">    @Before
</span><span style="color: #008080">19</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> init() {
</span><span style="color: #008080">20</span>         ac = <span style="color: #0000ff">new</span> ClassPathXmlApplicationContext("config/spring_mysql.xml"); <span style="color: #008000">//</span><span style="color: #008000"> 启动容器</span>
<span style="color: #008080">21</span> <span style="color: #000000">        System.out.println(ac);
</span><span style="color: #008080">22</span>         
<span style="color: #008080">23</span>         userDao = ac.getBean("userDao", UserDao.<span style="color: #0000ff">class</span>); <span style="color: #008000">//</span><span style="color: #008000"> 利用容器创建对象</span>
<span style="color: #008080">24</span> <span style="color: #000000">        System.out.println(userDao);
</span><span style="color: #008080">25</span> <span style="color: #000000">    }
</span><span style="color: #008080">26</span>     
<span style="color: #008080">27</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">28</span> <span style="color: #008000">     * 测试插入数据
</span><span style="color: #008080">29</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">30</span> <span style="color: #000000">    @Test
</span><span style="color: #008080">31</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> test01() {
</span><span style="color: #008080">32</span>         User user = <span style="color: #0000ff">new</span><span style="color: #000000"> User();
</span><span style="color: #008080">33</span>         user.setUserName("wym"<span style="color: #000000">);
</span><span style="color: #008080">34</span>         user.setPassword("111"<span style="color: #000000">);
</span><span style="color: #008080">35</span>         
<span style="color: #008080">36</span> <span style="color: #000000">        userDao.insert(user);
</span><span style="color: #008080">37</span> <span style="color: #000000">    }
</span><span style="color: #008080">38</span>     
<span style="color: #008080">39</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">40</span> <span style="color: #008000">     * 测试查询所有数据
</span><span style="color: #008080">41</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">42</span> <span style="color: #000000">    @Test
</span><span style="color: #008080">43</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> test02() {
</span><span style="color: #008080">44</span>         List<User> users =<span style="color: #000000"> userDao.findAll();
</span><span style="color: #008080">45</span>         <span style="color: #0000ff">for</span><span style="color: #000000">(User user : users) {
</span><span style="color: #008080">46</span> <span style="color: #000000">            System.out.println(user);
</span><span style="color: #008080">47</span> <span style="color: #000000">        }
</span><span style="color: #008080">48</span> <span style="color: #000000">    }
</span><span style="color: #008080">49</span>     
<span style="color: #008080">50</span>     
<span style="color: #008080">51</span> }
测试类

 

以上がspringJDBC01 は springJDBC を使用してデータベースを操作しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。