首页 > Java > java教程 > 正文

springJDBC01 利用springJDBC操作数据库

PHP中文网
发布: 2017-07-14 18:13:15
原创
1386 人浏览过

 

1 什么是springJDBC

  spring通过抽象JDBC访问并一致的API来简化JDBC编程的工作量。我们只需要声明SQL、调用合适的SpringJDBC框架API、处理结果集即可。事务由Spring管理,并将JDBC受查异常转换为Spring一致的非受查异常,从而简化开发。

  利用传统的jdbc操作数据库的步骤:获取连接→创建Statement→执行数据操作→获取结果→关闭Statement→关闭结果集→关闭连接;而Spring JDBC通过一个模板类org.springframework. jdbc.core.JdbcTemplate封装了样板式的代码,用户通过模板类就可以轻松地完成大部分数据访问的操作。

 

2 前期准备

  2.1 jar包

    spring-jdbc : springjdbc的包
    mysql : MySQL的驱动包
    dbcp :数据库连接池
    spring-webmvc : springmvc框架包
    annotation :@resource需要用到的包,该包在Tomcat中有,如果是web项目而且运行环境是Tomcat的话就不需要导入这个包了
    junit : 单元测试包

  2.2 数据库(使用mysql数据库5.4)

<span style="color: #008080"> 1</span> <span style="color: #000000"># 创建用户表
</span><span style="color: #008080"> 2</span> <span style="color: #000000">CREATE TABLE t_user (
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    user_id INT AUTO_INCREMENT PRIMARY KEY,
</span><span style="color: #008080"> 4</span> <span style="color: #000000">    user_name VARCHAR (30),
</span><span style="color: #008080"> 5</span> <span style="color: #000000">    credits INT,
</span><span style="color: #008080"> 6</span> <span style="color: #000000">    password VARCHAR (32),
</span><span style="color: #008080"> 7</span> <span style="color: #000000">    last_visit DATETIME,
</span><span style="color: #008080"> 8</span> <span style="color: #000000">    last_ip VARCHAR(23)
</span><span style="color: #008080"> 9</span> <span style="color: #000000">) ENGINE = InnoDB;
</span><span style="color: #008080">10</span> 
<span style="color: #008080">11</span> <span style="color: #000000"># 查询t_user表的结构
</span><span style="color: #008080">12</span> <span style="color: #000000">DESC t_user;
</span><span style="color: #008080">13</span> 
<span style="color: #008080">14</span> <span style="color: #000000"># 创建用户登录日志表
</span><span style="color: #008080">15</span> <span style="color: #000000">CREATE TABLE t_login_log (
</span><span style="color: #008080">16</span> <span style="color: #000000">    login_log_id INT AUTO_INCREMENT PRIMARY KEY,
</span><span style="color: #008080">17</span> <span style="color: #000000">    user_id INT,
</span><span style="color: #008080">18</span> <span style="color: #000000">    ip VARCHAR (23),
</span><span style="color: #008080">19</span> <span style="color: #000000">    login_datetime DATETIME
</span><span style="color: #008080">20</span> <span style="color: #000000">) ENGINE = InnoDB;
</span><span style="color: #008080">21</span> 
<span style="color: #008080">22</span> <span style="color: #000000">#查询 t_login_log 表的结构
</span><span style="color: #008080">23</span> <span style="color: #000000">DESC t_login_log;
</span><span style="color: #008080">24</span> 
<span style="color: #008080">25</span> <span style="color: #000000">INSERT INTO t_user
</span><span style="color: #008080">26</span> <span style="color: #000000">(user_name, password) 
</span><span style="color: #008080">27</span> <span style="color: #000000">VALUES
</span><span style="color: #008080">28</span> <span style="color: #000000">("wys", "182838" ); 
</span><span style="color: #008080">29</span> 
<span style="color: #008080">30</span> SELECT * FROM t_user;
登录后复制
相关表

 

3 环境搭建(使用的是eclipse)

  3.1 利用maven导入相关jar包

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><</span><span style="color: #800000">project </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://maven.apache.org/POM/4.0.0"</span><span style="color: #ff0000"> xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000"> xsi:schemaLocation</span><span style="color: #0000ff">="http://maven.apache.org/POM/4.0.0 "</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 2</span>   <span style="color: #0000ff"><</span><span style="color: #800000">modelVersion</span><span style="color: #0000ff">></span>4.0.0<span style="color: #0000ff"></</span><span style="color: #800000">modelVersion</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 3</span>   <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>cn.xiangxu<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 4</span>   <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>baobaotao<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span>   <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>0.0.1-SNAPSHOT<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span>   <span style="color: #0000ff"><</span><span style="color: #800000">packaging</span><span style="color: #0000ff">></span>war<span style="color: #0000ff"></</span><span style="color: #800000">packaging</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 7</span>   <span style="color: #0000ff"><</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-webmvc<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">11</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>3.2.8.RELEASE<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">13</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>mysql<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">15</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>mysql-connector-java<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">16</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>5.1.37<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">17</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">19</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.springframework<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">20</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>spring-jdbc<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">21</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>3.2.8.RELEASE<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">22</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">23</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">24</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>commons-dbcp<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">25</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>commons-dbcp<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">26</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>1.4<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">27</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">28</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">29</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>javax.annotation<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">30</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>javax.annotation-api<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">31</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>1.2<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">32</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">33</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">34</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>junit<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">35</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>junit<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">36</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>4.12<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">37</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">38</span>   <span style="color: #0000ff"></</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #008080">39</span> <span style="color: #0000ff"></</span><span style="color: #800000">project</span><span style="color: #0000ff">></span>
登录后复制
pom.xml

  3.2 创建properties文件,用于存放数据库相关信息

<span style="color: #008080">1</span> <span style="color: #000000">driverClassName=com.mysql.jdbc.Driver
</span><span style="color: #008080">2</span> <span style="color: #000000">url=jdbc:mysql://127.0.0.1:3306/sampledb
</span><span style="color: #008080">3</span> <span style="color: #000000">username=root
</span><span style="color: #008080">4</span> <span style="color: #000000">password=182838
</span><span style="color: #008080">5</span> <span style="color: #000000">maxActive=10
</span><span style="color: #008080">6</span> maxWait=3000
登录后复制
mysql.properties

  3.3 创建spring配置文件

    3.3.1 配置properties文件的bean

    3.3.2 配置数据库连接池

    3.3.3 配置jdbcTemplate

    3.3.4 配置组件扫描

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff">?></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><</span><span style="color: #800000">beans </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://www.springframework.org/schema/beans"</span>
<span style="color: #008080"> 3</span> <span style="color: #ff0000">    xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000"> xmlns:context</span><span style="color: #0000ff">="http://www.springframework.org/schema/context"</span>
<span style="color: #008080"> 4</span> <span style="color: #ff0000">    xmlns:jdbc</span><span style="color: #0000ff">="http://www.springframework.org/schema/jdbc"</span><span style="color: #ff0000"> xmlns:jee</span><span style="color: #0000ff">="http://www.springframework.org/schema/jee"</span>
<span style="color: #008080"> 5</span> <span style="color: #ff0000">    xmlns:tx</span><span style="color: #0000ff">="http://www.springframework.org/schema/tx"</span><span style="color: #ff0000"> xmlns:aop</span><span style="color: #0000ff">="http://www.springframework.org/schema/aop"</span>
<span style="color: #008080"> 6</span> <span style="color: #ff0000">    xmlns:mvc</span><span style="color: #0000ff">="http://www.springframework.org/schema/mvc"</span><span style="color: #ff0000"> xmlns:util</span><span style="color: #0000ff">="http://www.springframework.org/schema/util"</span>
<span style="color: #008080"> 7</span> <span style="color: #ff0000">    xmlns:jpa</span><span style="color: #0000ff">="http://www.springframework.org/schema/data/jpa"</span>
<span style="color: #008080"> 8</span> <span style="color: #ff0000">    xsi:schemaLocation</span><span style="color: #0000ff">="
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
</span><span style="color: #008080">10</span> <span style="color: #0000ff">         http://www.springframework.org/schema/context/spring-context-3.2.xsd
</span><span style="color: #008080">11</span> <span style="color: #0000ff">         http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
</span><span style="color: #008080">12</span> <span style="color: #0000ff">         http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
</span><span style="color: #008080">13</span> <span style="color: #0000ff">         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
</span><span style="color: #008080">14</span> <span style="color: #0000ff">         http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
</span><span style="color: #008080">15</span> <span style="color: #0000ff">         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
</span><span style="color: #008080">16</span> <span style="color: #0000ff">         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
</span><span style="color: #008080">17</span> <span style="color: #0000ff">         http://www.springframework.org/schema/util/spring-util-3.2.xsd"</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span>     
<span style="color: #008080">19</span>     <span style="color: #008000"><!--</span><span style="color: #008000"> 读取mysql.properties文件 </span><span style="color: #008000">--></span>
<span style="color: #008080">20</span>     <span style="color: #0000ff"><</span><span style="color: #800000">util:properties </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="mysql"</span><span style="color: #ff0000"> location</span><span style="color: #0000ff">="classpath:config/mysql.properties"</span><span style="color: #0000ff">/></span>
<span style="color: #008080">21</span>     
<span style="color: #008080">22</span>     <span style="color: #008000"><!--</span><span style="color: #008000"> 配置连接池 </span><span style="color: #008000">--></span>
<span style="color: #008080">23</span>     <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="ds"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="org.apache.commons.dbcp.BasicDataSource"</span><span style="color: #ff0000"> destroy-method</span><span style="color: #0000ff">="close"</span><span style="color: #0000ff">></span>       
<span style="color: #008080">24</span>       <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="driverClassName"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="#{mysql.driverClassName}"</span> <span style="color: #0000ff">/></span>      
<span style="color: #008080">25</span>       <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="url"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="#{mysql.url}"</span> <span style="color: #0000ff">/></span>      
<span style="color: #008080">26</span>       <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="username"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="#{mysql.username}"</span> <span style="color: #0000ff">/></span>      
<span style="color: #008080">27</span>       <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="password"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="#{mysql.password}"</span> <span style="color: #0000ff">/></span>      
<span style="color: #008080">28</span>     <span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
<span style="color: #008080">29</span>     
<span style="color: #008080">30</span>     <span style="color: #008000"><!--</span><span style="color: #008000"> 配置jdbcTemplate </span><span style="color: #008000">--></span>
<span style="color: #008080">31</span>     <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="jt"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="org.springframework.jdbc.core.JdbcTemplate"</span><span style="color: #0000ff">></span>
<span style="color: #008080">32</span>         <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="dataSource"</span><span style="color: #ff0000"> ref</span><span style="color: #0000ff">="ds"</span><span style="color: #0000ff">></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span>
<span style="color: #008080">33</span>     <span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
<span style="color: #008080">34</span>     
<span style="color: #008080">35</span>     <span style="color: #008000"><!--</span><span style="color: #008000"> 组件扫描 </span><span style="color: #008000">--></span>
<span style="color: #008080">36</span>     <span style="color: #0000ff"><</span><span style="color: #800000">context:component-scan </span><span style="color: #ff0000">base-package</span><span style="color: #0000ff">="com.baobaotao"</span><span style="color: #0000ff">></</span><span style="color: #800000">context:component-scan</span><span style="color: #0000ff">></span>
<span style="color: #008080">37</span>     
<span style="color: #008080">38</span> <span style="color: #0000ff"></</span><span style="color: #800000">beans</span><span style="color: #0000ff">></span>
登录后复制
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中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板