search
HomeJavajavaTutorialspringJDBC01 uses springJDBC to operate the database

1 What is springJDBC

Spring simplifies the workload of JDBC programming by abstracting JDBC access and creating a consistent API. We only need to declare SQL, call the appropriate SpringJDBC framework API, and process the result set. Transactions are managed by Spring and convert JDBC checked exceptions into Spring-consistent unchecked exceptions, simplifying development.

The steps to use traditional jdbc to operate the database: obtain connection → create Statement → perform data operations → obtain results → close Statement → close result set → close connection; while Spring JDBC uses a template class org.springframework.jdbc.core. JdbcTemplate encapsulates boilerplate code, and users can easily complete most data access operations through template classes.

2 Preparation

 2.1 jar package

spring-jdbc: springjdbc package

mysql: MySQL driver package
dbcp: database connection pool
spring-webmvc: springmvc framework package
annotation: the package that @resource needs to use, This package is included in Tomcat. If it is a web project and the running environment is Tomcat, there is no need to import this package
 Junit: unit test package

 2.2 Database (using mysql database 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;
Related tables

3 Environment setup (using eclipse)

 3.1 Use maven to import related jar packages

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><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 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 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 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 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 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 style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span>       <span style="color: #0000ff"><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span>           <span style="color: #0000ff"><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 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 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 style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>           <span style="color: #0000ff"><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 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 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 style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">19</span>           <span style="color: #0000ff"><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 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 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 style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">24</span>           <span style="color: #0000ff"><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 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 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 style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">29</span>           <span style="color: #0000ff"><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 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 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 style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">34</span>           <span style="color: #0000ff"><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 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 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></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
pom.xml
 3.2 Create a properties file to store database related information

<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 Create spring configuration file

 3.3.1 Configure the beans of the properties file

 3.3.2 Configuring database connection pool

 3.3.3 Configure jdbcTemplate

 3.3.4 Configure component scanning

<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 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 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 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 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 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 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 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 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 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 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></span></span></span></span></span></span></span></span></span></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> }</user>
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> }</user></user></user>
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> }</user>
测试类

 

The above is the detailed content of springJDBC01 uses springJDBC to operate the database. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是什么软件microsoft sql server是什么软件Feb 28, 2023 pm 03:00 PM

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

数据库的什么是指数据的正确性和相容性数据库的什么是指数据的正确性和相容性Jul 04, 2022 pm 04:59 PM

数据库的“完整性”是指数据的正确性和相容性。完整性是指数据库中数据在逻辑上的一致性、正确性、有效性和相容性。完整性对于数据库系统的重要性:1、数据库完整性约束能够防止合法用户使用数据库时向数据库中添加不合语义的数据;2、合理的数据库完整性设计,能够同时兼顾数据库的完整性和系统的效能;3、完善的数据库完整性有助于尽早发现应用软件的错误。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)