>Java >java지도 시간 >springJDBC01은 springJDBC를 사용하여 데이터베이스를 작동합니다.

springJDBC01은 springJDBC를 사용하여 데이터베이스를 작동합니다.

PHP中文网
PHP中文网원래의
2017-07-14 18:13:151406검색

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 프레임워크 패키지
  annotation: @resource는 패키지를 사용해야 하며, 패키지는 웹인 경우 Tomcat에서 사용할 수 있습니다. 프로젝트와 실행 환경은 Tomcat이므로 이 패키지를 가져올 필요가 없습니다
 Junit: 단위 테스트 패키지

 2.2 데이터베이스 (mysql 데이터베이스 5.4 사용)

으아악
관련 테이블

3 환경 설정(Eclipse 사용)

 3.1 maven을 사용하여 관련 jar 패키지 가져오기

으아악
pom.xml
 3.2 데이터베이스 관련 정보를 저장하기 위한 속성 파일 생성

으아악
mysql.properties
 3.3 스프링 구성 파일 생성

 3.3.1 속성 파일에서 빈 구성

  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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.