Java-JDBC连接数据库(MySQL)

1.1数据库连接

Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是SunMicrosystems的商标[1]。它JDBC是面向关系型数据库的。

1.1.1 Jdbc驱动程序共分四种类型

1.1.1.1 类型Jdbc-ODBC桥

1.      这种类型的驱动把所有JDBC的调用传递给ODBC,再让后者调用数据库本地驱动代码(也就是数据库厂商提供的数据库操作二进制代码库,例如Oracle中的oci.dll)

优点:只要有对应的ODBC驱动(大部分数据库厂商都会提供),几乎可以访问所有的数据库。

缺点:执行效率比较低,不适合大数据量访问的应用;由于需要客户端预装对应的ODBC驱动,不适合Internet/Intranet应用。

1.1.1.2 类型  本地驱动

2.      这种类型的驱动通过客户端加载数据库厂商提供的本地代码库(C/C++等)来访问数据库,而在驱动程序中则包含了Java代码。

优点:速度快于第一类驱动(但仍比不上第3、第4类驱动)。

缺点:由于需要客户端预装对应的数据库厂商代码库,仍不适合Internet/Intranet应用。

1.1.1.3 类型  网络协议驱动

3.      这种类型的驱动给客户端提供了一个网络API,客户端上的JDBC驱动程序使用套接字(Socket)来调用服务器上的中间件程序,后者在将其请求转化为所需的具体API调用。

优点:不需要在客户端加载数据库厂商提供的代码库,单个驱动程序可以对多个数据库进行访问,可扩展性较好。

缺点:在中间件层仍需对最终数据进行配置;由于多出一个中间件层,速度不如第四类驱动程序。

1.1.1.4 类型本地协议驱动

4.      这种类型的驱动使用Socket,直接在客户端和数据库间通信。

优点:访问速度最快;这是最直接、最纯粹的Java实现。

                           缺点:因为缺乏足够的文档和技术支持,几乎只有数据库厂商自己才能提供这种类型的JDBC驱动;需要针对不同的数据库使用不同的驱动程序。 

1.2Jdbc连接数据库

1.2.1 MySQL数据库

1.2.1.1 表结构

1.      创建数据库、表sql语句:

-- 设置数据库编码

SET NAMES gbk;

 

-- 创建数据库

CREATE DATABASE jdbc;

 

-- 使用数据库

USE jdbc;

 

-- 创建用户表

CREATE TABLE USER(

    user_id INT(11) NOT NULL AUTO_INCREMENT,

    user_name VARCHAR(30),

    user_pass VARCHAR(30),

    user_sex CHAR(1),

    create_time VARCHAR(19),

    update_time VARCHAR(19),

    PRIMARYKEY(user_id)

);

2.      表结构如下图所示:

字段名

说明

类型

备注

user_id

用户编号

INT(11)

主键,自增,增值为1

user_name

用户名

VARCHAR(30)

 

user_pass

密码

VARCHAR(30)

 

user_sex

性别

CHAR(1)

0:false 1:为true

create_time

创建时间

VARCHAR(19)

 

update_time

更新时间

VARCHAR(19)

 

1.2.1.2 MySQL驱动

1.      MySQL驱动:mysql-connector-java-3.1.12-bin.jar

2.      Junit下载地址:http://www.mysql.com

3.      mysql-connector-java-3.1.12-bin.jar包:

mysql-connector-java-3.1.12-bin.jar

1.2.1.3  公共连库帮助类ConnectionUtil类

  1. package com.common.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.ResultSetMetaData;  
  8. import java.sql.SQLException;  
  9. import java.sql.Statement;  
  10. import java.util.ArrayList;  
  11. import java.util.HashMap;  
  12. import java.util.List;  
  13. import java.util.Map;  
  14.   
  15. public class ConnectionUtil {  
  16.     // 数据库连接驱动  
  17.     public static final String DRIVER = "com.mysql.jdbc.Driver";  
  18.     // 数据库连接URL  
  19.     public static final String URL = "jdbc:mysql://localhost:3306/jdbc";  
  20.     // 数据库用户名  
  21.     public static final String USER = "root";  
  22.     // 数据库密码  
  23.     public static final String PASS = "root";  
  24.   
  25.     /** 
  26.      * 获得数据库连接 
  27.      *  
  28.      * @return 
  29.      */  
  30.     public static Connection getConnection() {  
  31.         // 声明Connection连接对象  
  32.         Connection conn = null;  
  33.         try {  
  34.             // 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册  
  35.             Class.forName(DRIVER);  
  36.             // 通过DriverManager的getConnection()方法获取数据库连接  
  37.             conn = DriverManager.getConnection(URL, USER, PASS);  
  38.         } catch (ClassNotFoundException e) {  
  39.             e.printStackTrace();  
  40.             System.out.println("数据库驱动没有找到!");  
  41.         } catch (SQLException e) {  
  42.             e.printStackTrace();  
  43.             System.out.println("数据库连接失败!");  
  44.         }  
  45.         return conn;  
  46.     }  
  47.   
  48.     /** 
  49.      * 关闭数据库链接 
  50.      *  
  51.      * @param conn 
  52.      * @param statement 
  53.      * @param rs 
  54.      */  
  55.     public static void close(Connection conn, Statement statement, ResultSet rs) {  
  56.         // 关闭数据集  
  57.         if (rs != null) {  
  58.             try {  
  59.                 rs.close();  
  60.             } catch (SQLException e) {  
  61.                 e.printStackTrace();  
  62.             }  
  63.         }  
  64.   
  65.         // 关闭预处理对象  
  66.         if (statement != null) {  
  67.             try {  
  68.                 statement.close();  
  69.             } catch (SQLException e) {  
  70.                 e.printStackTrace();  
  71.             }  
  72.         }  
  73.   
  74.         // 关闭连接对象  
  75.         if (conn != null) {  
  76.             try {  
  77.                 if (!conn.isClosed()) {  
  78.                     conn.close();  
  79.                 }  
  80.             } catch (SQLException e) {  
  81.                 e.printStackTrace();  
  82.             }  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 查询数据库信息列表 
  88.      *  
  89.      * @param sql 
  90.      *            查询数据的SQL语句 
  91.      * @param List<Object> params  参数 
  92.      * @return 
  93.      */  
  94.     public static List<Map<String, Object>> queryList(String sql, List<Object> params) {  
  95.         // 返回List数组  
  96.         List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();  
  97.         Map<String,Object> rows = null;  
  98.   
  99.         // 数据库的连接(会话),对象  
  100.         Connection conn = null;  
  101.         // 预编译的 SQL 语句的对象  
  102.         PreparedStatement statement = null;  
  103.         // 结果集  
  104.         ResultSet rs = null;  
  105.         try {  
  106.             conn = getConnection();  
  107.             // 创建PreparedStatement对象  
  108.             statement = conn.prepareStatement(sql);  
  109.             // 为查询语句设置参数  
  110.             setParameter(statement, params);  
  111.             // 获得结果集  
  112.             rs = statement.executeQuery();  
  113.             // 获得结果集的信息  
  114.             ResultSetMetaData rsmd = rs.getMetaData();  
  115.             // 获得列的总数  
  116.             int columnCount = rsmd.getColumnCount();  
  117.             // 遍历结果集  
  118.             while (rs.next()) {  
  119.                 rows = new HashMap<String, Object>();  
  120.                 for (int i = 0; i < columnCount; i++) {  
  121.                     // 获得数据库列名  
  122.                     String columnLalbe = rsmd.getColumnLabel(i+1);  
  123.                     rows.put(columnLalbe, rs.getObject(columnLalbe));  
  124.                 }  
  125.                 // 添加到  
  126.                 data.add(rows);  
  127.             }  
  128.         } catch (SQLException e) {  
  129.             e.printStackTrace();  
  130.             System.out.println("数据库查询出错");  
  131.         } finally {  
  132.             ConnectionUtil.close(conn, statement, rs);  
  133.         }  
  134.         // 返回数组对象  
  135.         return data;  
  136.     }  
  137.       
  138.     /** 
  139.      * 查询数据库总纪录数 
  140.      *  
  141.      * @param sql 
  142.      *            查询数据的SQL语句 
  143.      * @param List<Object> params 参数 
  144.      * @return 
  145.      */  
  146.     public static long queryCount(String sql, List<Object> params) {  
  147.         // 定义返回记录数  
  148.         long count = 0;  
  149.         // 数据库的连接(会话),对象  
  150.         Connection conn = null;  
  151.         // 预编译的 SQL 语句的对象  
  152.         PreparedStatement statement = null;  
  153.         // 结果集  
  154.         ResultSet rs = null;  
  155.         try {  
  156.             conn = getConnection();  
  157.             // 创建PreparedStatement对象  
  158.             statement = conn.prepareStatement(sql);  
  159.             // 为查询语句设置参数  
  160.             setParameter(statement, params);  
  161.             // 获得结果集  
  162.             rs = statement.executeQuery();  
  163.             // 遍历结果集  
  164.             while (rs.next()) {  
  165.                 count = rs.getLong(1);  
  166.             }  
  167.         } catch (SQLException e) {  
  168.             e.printStackTrace();  
  169.             System.out.println("数据库查询出错");  
  170.         } finally {  
  171.             ConnectionUtil.close(conn, statement, rs);  
  172.         }  
  173.         // 返回数组对象  
  174.         return count;  
  175.     }  
  176.   
  177.     /** 
  178.      * 添加、修改、删除 通用方法 
  179.      *  
  180.      * @param sql 
  181.      * @param List<Object> params 可变的参数 
  182.      * @return 返回值为更新记录数 
  183.      */  
  184.     public static int update(String sql, List<Object> params) {  
  185.         // 数据库的连接(会话),对象  
  186.         Connection conn = null;  
  187.         // 预编译的 SQL 语句的对象  
  188.         PreparedStatement statement = null;  
  189.         // 定义受影响的行数  
  190.         int rows = 0;  
  191.         try {  
  192.             // 获得数库连接  
  193.             conn = ConnectionUtil.getConnection();  
  194.             // 创建预编译SQL对象  
  195.             statement = conn.prepareStatement(sql);  
  196.             // 设置SQL话句参数  
  197.             setParameter(statement, params);  
  198.             // 返回受影响的行数  
  199.             rows = statement.executeUpdate();  
  200.         } catch (SQLException e) {  
  201.             e.printStackTrace();  
  202.             System.out.println("数据库操作异常!");  
  203.         }  
  204.         return rows;  
  205.     }  
  206.   
  207.     /** 
  208.      * 为预编译对象设置参数 
  209.      *  
  210.      * @param statement 
  211.      * @param object 
  212.      * @throws SQLException 
  213.      */  
  214.     public static void setParameter(PreparedStatement statement, List<Object> params) throws SQLException {  
  215.         if (params != null && params.size() > 0) {  
  216.             // 循环设置参数  
  217.             for (int i = 0; i < params.size(); i++) {  
  218.                 statement.setObject((i + 1), params.get(i));  
  219.             }  
  220.         }  
  221.     }  
  222. }  
package com.common.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;import 
java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConnectionUtil {	
// 数据库连接驱动	
public static final String DRIVER = "com.mysql.jdbc.Driver";	
// 数据库连接URL	
public static final String URL = "jdbc:mysql://localhost:3306/jdbc";	
// 数据库用户名	
public static final String USER = "root";	
// 数据库密码	
public static final String PASS = "root";	
/**	 * 获得数据库连接	 
* 	 * @return	
 */	
public static Connection getConnection() {		
// 声明Connection连接对象		
Connection conn = null;		
try {			
// 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册			
Class.forName(DRIVER);			
// 通过DriverManager的getConnection()方法获取数据库连接			
conn = DriverManager.getConnection(URL, USER, PASS);		
} catch (ClassNotFoundException e) {			
e.printStackTrace();			
System.out.println("数据库驱动没有找到!");		
} catch (SQLException e) {			
e.printStackTrace();			
System.out.println("数据库连接失败!");		}		return conn;	}	
/**	 * 关闭数据库链接	 * 	
 * @param conn	 * @param statement	 
* @param rs	 */	
public static void close(Connection conn, Statement statement, ResultSet rs) {		
// 关闭数据集		
if (rs != null) {			
try {				
      rs.close();			
} catch (SQLException e) {				
e.printStackTrace();			
}		
}		
// 关闭预处理对象		
if (statement != null) {			
  try {				
statement.close();			
} catch (SQLException e) {				
e.printStackTrace();			
}		
}		
// 关闭连接对象		
if (conn != null) {			
try {				
if (!conn.isClosed()) {					
conn.close();				}			
} catch (SQLException e) {				
e.printStackTrace();			
}		}	}	
/**	 
* 查询数据库信息列表	 * 	
 * @param sql	 *           
 查询数据的SQL语句	 * @param List<Object> params  参数	 
* @return	 */	
public static List<Map<String, Object>> queryList(String sql, List<Object> params) {		
// 返回List数组		
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();		
Map<String,Object> rows = null;		
// 数据库的连接(会话),对象		
Connection conn = null;		
// 预编译的 SQL 语句的对象		
PreparedStatement statement = null;		
// 结果集		
ResultSet rs = null;		
try {			
conn = getConnection();			
// 创建PreparedStatement对象			
statement = conn.prepareStatement(sql);			
// 为查询语句设置参数			
setParameter(statement, params);			
// 获得结果集			
rs = statement.executeQuery();			
// 获得结果集的信息			
ResultSetMetaData rsmd = rs.getMetaData();			
// 获得列的总数			
int columnCount = rsmd.getColumnCount();			
// 遍历结果集			
while (rs.next()) {				
rows = new HashMap<String, Object>();				
for (int i = 0; i < columnCount; i++) {					
// 获得数据库列名					
String columnLalbe = rsmd.getColumnLabel(i+1);					
rows.put(columnLalbe, rs.getObject(columnLalbe));				
}				
// 添加到				
data.add(rows);			
}		
} catch (SQLException e) {			
e.printStackTrace();			
System.out.println("数据库查询出错");		
} finally {			
ConnectionUtil.close(conn, statement, rs);		}		
// 返回数组对象		
return data;	}		
/**	 * 查询数据库总纪录数	 * 	 
* @param sql	 *            查询数据的SQL语句	
 * @param List<Object> params 参数	 
* @return	 */	
public static long queryCount(String sql, List<Object> params) {		
// 定义返回记录数		
long count = 0;		
// 数据库的连接(会话),对象		
Connection conn = null;		
// 预编译的 SQL 语句的对象		
PreparedStatement statement = null;		
// 结果集		
ResultSet rs = null;		
try {			
conn = getConnection();			
// 创建PreparedStatement对象			
statement = conn.prepareStatement(sql);			
// 为查询语句设置参数			
setParameter(statement, params);			
// 获得结果集			
rs = statement.executeQuery();			
// 遍历结果集			
while (rs.next()) {				
count = rs.getLong(1);			
}		
} catch (SQLException e) {			
e.printStackTrace();			
System.out.println("数据库查询出错");		
} finally {			
ConnectionUtil.close(conn, statement, rs);		
}		
// 返回数组对象		
return count;	}	
 
/**	 * 添加、修改、删除 通用方法	 * 	
 * @param sql	 * @param List<Object> params 可变的参数	 
* @return 返回值为更新记录数	 */	
public static int update(String sql, List<Object> params) {		
// 数据库的连接(会话),对象		
Connection conn = null;		
// 预编译的 SQL 语句的对象		
PreparedStatement statement = null;		
// 定义受影响的行数		
int rows = 0;		
try {			
// 获得数库连接			
conn = ConnectionUtil.getConnection();			
// 创建预编译SQL对象			
statement = conn.prepareStatement(sql);			
// 设置SQL话句参数			
setParameter(statement, params);			
// 返回受影响的行数			
rows = statement.executeUpdate();		
} catch (SQLException e) {			
e.printStackTrace();			
System.out.println("数据库操作异常!");		
}		return rows;	}	
/**	 * 为预编译对象设置参数	 * 	
 * @param statement	 * @param object	
 * @throws SQLException	 */	
public static void setParameter(PreparedStatement statement, List<Object> params) throws SQLException {		
if (params != null && params.size() > 0) {			
// 循环设置参数			
for (int i = 0; i < params.size(); i++) {				
statement.setObject((i + 1), params.get(i));			
}		
}	
}}

1.2.1.4 Dao层UserDao接口类

  1. package com.user.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.user.model.User;  
  6. import com.user.vo.UserVO;  
  7.   
  8. public interface UserDao {  
  9.       
  10.     /** 
  11.      * 查询用户列表 
  12.      * @param userVo    VO对象 
  13.      * @param page      页数 
  14.      * @param maxRows   每页显示记数 
  15.      * @param sort      排序字段 
  16.      * @param order     排序方式(asc或desc) 
  17.      * @return  
  18.      */  
  19.     public List<User> searchUser(UserVO userVo, int page, int maxRows,  
  20.             String sort, String order);  
  21.       
  22.     /** 
  23.      * 或者用户数量 
  24.      * @param userVo    VO对象 
  25.      * @return  
  26.      */  
  27.     public long getCountUser(UserVO userVo);  
  28.       
  29.     /** 
  30.      * 获得最大的用户编号 
  31.      * @return 
  32.      */  
  33.     public long getMaxUserId();  
  34.       
  35.     /** 
  36.      * 根据用户名查询用户count用户个数 
  37.      * @param userName 用户名 
  38.      * @return 
  39.      */  
  40.     public long getUserCountByName(String userName);  
  41.       
  42.     /**  
  43.      * 根据名称获得用户对象 
  44.      * @param userName 用户名 
  45.      * @return 
  46.      */  
  47.     public User getUserByName(String userName);  
  48.       
  49.     /** 
  50.      * 判断用户名是否唯一 
  51.      * @param userName 
  52.      * @return 是唯一返回true,不是唯一返回false 
  53.      */  
  54.     public boolean getUniqueUserName(String userName,String userId);  
  55.       
  56.       
  57.     /** 
  58.      * 根据用户编号获得用户对象 
  59.      * @param userId 用户号 
  60.      * @return 
  61.      */  
  62.     public User getUserById(String userId);  
  63.       
  64.       
  65.     /** 
  66.      * 保存用户 
  67.      * @param user 用户对象 
  68.      * @return 
  69.      */  
  70.     public boolean saveUser(User user);  
  71.       
  72.     /** 
  73.      * 更新用户 
  74.      * @param user 用户对象 
  75.      * @return 
  76.      */  
  77.     public boolean updateUser(User user);  
  78.       
  79.     /** 
  80.      * 删除用户  
  81.      * @param userIds 用户编号字符串,以“,”分隔 
  82.      * @return 
  83.      */  
  84.     public boolean deleteUser(String userIds);  
  85. }  
package com.user.dao;import java.util.List;import com.user.model.User;import com.user.vo.UserVO;public interface UserDao {		/**	 * 查询用户列表	 * @param userVo 	VO对象	 * @param page 		页数	 * @param maxRows 	每页显示记数	 * @param sort 		排序字段	 * @param order 	排序方式(asc或desc)	 * @return 	 */	public List<User> searchUser(UserVO userVo, int page, int maxRows,			String sort, String order);		/**	 * 或者用户数量	 * @param userVo 	VO对象	 * @return 	 */	public long getCountUser(UserVO userVo);		/**	 * 获得最大的用户编号	 * @return	 */	public long getMaxUserId();		/**	 * 根据用户名查询用户count用户个数	 * @param userName 用户名	 * @return	 */	public long getUserCountByName(String userName);		/** 	 * 根据名称获得用户对象	 * @param userName 用户名	 * @return	 */	public User getUserByName(String userName);		/**	 * 判断用户名是否唯一	 * @param userName	 * @return 是唯一返回true,不是唯一返回false	 */	public boolean getUniqueUserName(String userName,String userId);			/**	 * 根据用户编号获得用户对象	 * @param userId 用户号	 * @return	 */	public User getUserById(String userId);			/**	 * 保存用户	 * @param user 用户对象	 * @return	 */	public boolean saveUser(User user);		/**	 * 更新用户	 * @param user 用户对象	 * @return	 */	public boolean updateUser(User user);		/**	 * 删除用户 	 * @param userIds 用户编号字符串,以“,”分隔	 * @return	 */	public boolean deleteUser(String userIds);}

1.2.1.5 Dao层UserDaoImpl接口实现类

  1. package com.user.dao.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.common.util.ConnectionUtil;  
  8. import com.common.util.StringUtil;  
  9. import com.user.dao.UserDao;  
  10. import com.user.model.User;  
  11. import com.user.util.UserBeanUtil;  
  12. import com.user.vo.UserVO;  
  13.   
  14. public class UserDaoImpl implements UserDao{  
  15.       
  16.     /** 
  17.      * 查询用户列表 
  18.      * @param userVo    VO对象 
  19.      * @param page      页数 
  20.      * @param maxRows   每页显示记数 
  21.      * @param sort      排序字段 
  22.      * @param order     排序方式(asc或desc) 
  23.      * @return  
  24.      */  
  25.     public List<User> searchUser(UserVO userVo, int page, int maxRows,  
  26.             String sort, String order) {  
  27.   
  28.         StringBuffer sqlBuffer = new StringBuffer("select * from user where 1=1");  
  29.         // 添加参数  
  30.         List<Object> params = new ArrayList<Object>();  
  31.         if (userVo != null) {  
  32.             // 用户名  
  33.             if (!StringUtil.isEmpty(userVo.getUserName())) {  
  34.                 sqlBuffer.append(" and user_name like ? ");  
  35.                 params.add("%" + userVo.getUserName() + "%");  
  36.             }  
  37.   
  38.             // 创建时间  
  39.             if (!StringUtil.isEmpty(userVo.getStartCreateTime())) {  
  40.                 sqlBuffer.append(" and create_time >=?");  
  41.                 params.add(userVo.getStartCreateTime());  
  42.             }  
  43.   
  44.             if (!StringUtil.isEmpty(userVo.getEndCreateTime())) {  
  45.                 sqlBuffer.append(" and create_time <=?");  
  46.                 params.add(userVo.getEndCreateTime());  
  47.             }  
  48.   
  49.             // 更新时间  
  50.             if (!StringUtil.isEmpty(userVo.getStartUpdateTime())) {  
  51.                 sqlBuffer.append(" and update_tiume >=?");  
  52.                 params.add(userVo.getStartUpdateTime());  
  53.             }  
  54.   
  55.             if (!StringUtil.isEmpty(userVo.getEndUpdateTime())) {  
  56.                 sqlBuffer.append(" and update_tiume <=?");  
  57.                 params.add(userVo.getEndUpdateTime());  
  58.             }  
  59.               
  60.             // 排序  
  61.             if(!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)){  
  62.                 sqlBuffer.append(" order by ").append(UserBeanUtil.fieldToColumn(sort)).append(" ").append(order);  
  63.             }  
  64.               
  65.              // 分页  
  66.              if(page > 0 && maxRows > 0){  
  67.                  // 公式:firstRows = (page - 1) * maxRows  
  68.                  int firstRows = (page - 1) * maxRows ;  
  69.                  sqlBuffer.append(" limit ").append(firstRows).append(",").append(maxRows);  
  70.              }  
  71.         }  
  72.           
  73.         // 查询用户列表  
  74.         List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(),params);  
  75.         return UserBeanUtil.copyProperty(datas);  
  76.     }  
  77.       
  78.     /** 
  79.      * 条件查询用户数量 
  80.      * @param userVo    VO对象 
  81.      * @return  
  82.      */  
  83.     public long getCountUser(UserVO userVo) {  
  84.          StringBuffer sqlBuffer = new StringBuffer("select count(user_id) from user where 1=1");  
  85.         // 添加参数  
  86.         List<Object> params = new ArrayList<Object>();  
  87.         if (userVo != null) {  
  88.             // 用户名  
  89.             if (!StringUtil.isEmpty(userVo.getUserName())) {  
  90.                 sqlBuffer.append(" and user_name like ? ");  
  91.                 params.add("%" + userVo.getUserName() + "%");  
  92.             }  
  93.   
  94.             // 创建时间  
  95.             if (!StringUtil.isEmpty(userVo.getStartCreateTime())) {  
  96.                 sqlBuffer.append(" and create_time >=?");  
  97.                 params.add(userVo.getStartCreateTime());  
  98.             }  
  99.   
  100.             if (!StringUtil.isEmpty(userVo.getEndCreateTime())) {  
  101.                 sqlBuffer.append(" and create_time <=?");  
  102.                 params.add(userVo.getEndCreateTime());  
  103.             }  
  104.   
  105.             // 更新时间  
  106.             if (!StringUtil.isEmpty(userVo.getStartUpdateTime())) {  
  107.                 sqlBuffer.append(" and update_tiume >=?");  
  108.                 params.add(userVo.getStartUpdateTime());  
  109.             }  
  110.   
  111.             if (!StringUtil.isEmpty(userVo.getEndUpdateTime())) {  
  112.                 sqlBuffer.append(" and update_tiume <=?");  
  113.                 params.add(userVo.getEndUpdateTime());  
  114.             }  
  115.             // 返回总记录数  
  116.             return ConnectionUtil.queryCount(sqlBuffer.toString(),params);  
  117.         }  
  118.         return 0;  
  119.     }  
  120.       
  121.     /** 
  122.      * 获得最大的用户编号 
  123.      * @return 
  124.      */  
  125.     public long getMaxUserId(){  
  126.         StringBuffer sqlBuffer = new StringBuffer("select max(user_id) from user;");  
  127.         return ConnectionUtil.queryCount(sqlBuffer.toString(), null);  
  128.     }  
  129.       
  130.     /** 
  131.      * 根据用户名查询用户count用户个数 
  132.      * @param userName 用户名 
  133.      * @return 
  134.      */  
  135.     public long getUserCountByName(String userName){  
  136.         if(StringUtil.isEmpty(userName)){  
  137.             return Integer.MAX_VALUE;  
  138.         }  
  139.         // 接写sql语句  
  140.         StringBuffer sqlBuffer = new StringBuffer("select count(user_id) from user where 1=1 and user_name = ?");  
  141.         // 添加参数  
  142.         List<Object> params = new ArrayList<Object>();  
  143.         params.add(userName);  
  144.         // 返回查询的用  
  145.         return ConnectionUtil.queryCount(sqlBuffer.toString(), params);  
  146.     }  
  147.       
  148.     /**  
  149.      * 根据名称获得用户对象 
  150.      * @param userName 用户名 
  151.      * @return 
  152.      */  
  153.     public User getUserByName(String userName){  
  154.         if(StringUtil.isEmpty(userName)){  
  155.             return null;  
  156.         }  
  157.         // 接写sql语句  
  158.         StringBuffer sqlBuffer = new StringBuffer("select * from user where 1=1 and user_name = ?");  
  159.         // 添加参数  
  160.         List<Object> params = new ArrayList<Object>();  
  161.         params.add(userName);  
  162.         // 查询用户列表  
  163.         List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(),params);  
  164.         List<User> userList = UserBeanUtil.copyProperty(datas);  
  165.   
  166.         // 返回user  
  167.         if(userList != null && userList.size() > 0){  
  168.             return userList.get(0);           
  169.         }  
  170.         return null;  
  171.     }  
  172.       
  173.     /** 
  174.      * 判断用户名是否唯一 
  175.      * @param userName 
  176.      * @return 是唯一返回true,不是唯一返回false 
  177.      */  
  178.     public boolean getUniqueUserName(String userName,String userId){  
  179.         // 参数为空判断   
  180.         if(StringUtil.isEmpty(userName)){  
  181.             return false;  
  182.         }  
  183.           
  184.         if(StringUtil.isEmpty(userId)){  
  185.             return false;  
  186.         }  
  187.           
  188.         // 接写sql语句  
  189.         StringBuffer sqlBuffer = new StringBuffer("select count(*) from user where user_name = ? and user_id != ? ");  
  190.         // 添加参数  
  191.         List<Object> params = new ArrayList<Object>();  
  192.         params.add(userName);  
  193.         params.add(userId);  
  194.           
  195.         // 查询用户列表  
  196.         long count = ConnectionUtil.queryCount(sqlBuffer.toString(),params);  
  197.         return count <= 0 ? true : false;  
  198.     }   
  199.       
  200.       
  201.     /** 
  202.      * 根据用户编号获得用户对象 
  203.      * @param userId 用户号 
  204.      * @return 
  205.      */  
  206.     public User getUserById(String userId){  
  207.         // 为空判断  
  208.         if(StringUtil.isEmpty(userId)){  
  209.             return null;  
  210.         }  
  211.           
  212.         // 接接sql语句  
  213.         StringBuffer sqlBuffer = new StringBuffer("select * from user where user_id = ?");  
  214.           
  215.         // 添加参数  
  216.         List<Object> params = new ArrayList<Object>();  
  217.         params.add(userId);  
  218.           
  219.         // 查询用户  
  220.         List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(), params);  
  221.         List<User> userList = UserBeanUtil.copyProperty(datas);  
  222.           
  223.         if(userList != null && userList.size() > 0){  
  224.             return userList.get(0);           
  225.         }  
  226.         return null;  
  227.     }  
  228.       
  229.       
  230.     /** 
  231.      * 保存用户 
  232.      * @param user 用户对象 
  233.      * @return 
  234.      */  
  235.     public boolean saveUser(User user){  
  236.         // 用户为空判断  
  237.         if(user == null){  
  238.             return false;  
  239.         }  
  240.           
  241.         // 拼接sql语句  
  242.         StringBuffer sqlBuffer = new StringBuffer("insert into user(user_id,user_name,user_pass,user_sex,create_time,update_time)");  
  243.         sqlBuffer.append("values(?,?,?,?,?,?)");  
  244.           
  245.         // 添加参数  
  246.         List<Object> params = new ArrayList<Object>();  
  247.           
  248.         // 用户编号  
  249.         if(!StringUtil.isEmpty(user.getUserId()+"")){  
  250.             params.add(user.getUserId());  
  251.         }  
  252.           
  253.         // 用户名  
  254.         if(!StringUtil.isEmpty(user.getUserName())){  
  255.             params.add(user.getUserName());  
  256.         }  
  257.           
  258.         // 密码  
  259.         if(!StringUtil.isEmpty(user.getPassword())){  
  260.             params.add(user.getPassword());  
  261.         }  
  262.           
  263.         // 性别  
  264.         if(!StringUtil.isEmpty(user.isSex() + "")){  
  265.             params.add(user.isSex());  
  266.         }  
  267.           
  268.         // 创建时间  
  269.         if(!StringUtil.isEmpty(user.getCreateTime())){  
  270.             params.add(user.getCreateTime());  
  271.         }  
  272.           
  273.         // 更新时间  
  274.         if(!StringUtil.isEmpty(user.getUpdateTime())){  
  275.             params.add(user.getUpdateTime());  
  276.         }  
  277.           
  278.         // 判断param.size参数的长度是否为6,检验数填写是否完成  
  279.         if(params.size() != 6){  
  280.             return false;  
  281.         }  
  282.         // 保存  
  283.         int result = ConnectionUtil.update(sqlBuffer.toString(), params);  
  284.         // 返回执行结果  
  285.         return result >= 1 ? true : false;  
  286.     }  
  287.       
  288.     /** 
  289.      * 更新用户 
  290.      * @param user 用户对象 
  291.      * @return 
  292.      */  
  293.     public boolean updateUser(User user){  
  294.         // 用户为空判断  
  295.         if(user == null){  
  296.             return false;  
  297.         }  
  298.           
  299.         // 拼接sql语句  
  300.         StringBuffer sqlBuffer = new StringBuffer("update user set user_name=?,user_pass=?,user_sex=?,create_time=?,update_time=?");  
  301.         sqlBuffer.append(" where user_id=?");  
  302.         // 添加参数  
  303.         List<Object> params = new ArrayList<Object>();  
  304.           
  305.         // 用户名  
  306.         if(!StringUtil.isEmpty(user.getUserName())){  
  307.             params.add(user.getUserName());  
  308.         }  
  309.           
  310.         // 密码  
  311.         if(!StringUtil.isEmpty(user.getPassword())){  
  312.             params.add(user.getPassword());  
  313.         }  
  314.           
  315.         // 性别  
  316.         if(!StringUtil.isEmpty(user.isSex() + "")){  
  317.             params.add(user.isSex());  
  318.         }  
  319.           
  320.         // 创建时间  
  321.         if(!StringUtil.isEmpty(user.getCreateTime())){  
  322.             params.add(user.getCreateTime());  
  323.         }  
  324.           
  325.         // 更新时间  
  326.         if(!StringUtil.isEmpty(user.getUpdateTime())){  
  327.             params.add(user.getUpdateTime());  
  328.         }  
  329.           
  330.         // 用户编号  
  331.         if(!StringUtil.isEmpty(user.getUserId()+"")){  
  332.             params.add(user.getUserId());  
  333.         }  
  334.           
  335.         // 判断param.size参数的长度是否为6,检验数填写是否完成  
  336.         if(params.size() != 6){  
  337.             return false;  
  338.         }  
  339.         // 保存  
  340.         int result = ConnectionUtil.update(sqlBuffer.toString(), params);  
  341.         // 返回执行结果  
  342.         return result >= 1 ? true : false;  
  343.     }  
  344.       
  345.     /** 
  346.      * 删除用户  
  347.      * @param userIds 用户编号字符串,以“,”分隔 
  348.      * @return 
  349.      */  
  350.     public boolean deleteUser(String userIds){  
  351.         // 判断id是否存在  
  352.         if(StringUtil.isEmpty(userIds)){  
  353.             return false;  
  354.         }  
  355.           
  356.         // 添加参数  
  357.         StringBuffer idsBuffer = new StringBuffer("");  
  358.         List<Object> params = new ArrayList<Object>();  
  359.           
  360.         String [] ids = userIds.split(",");  
  361.         if(ids == null || ids.length < 0){  
  362.             return false;  
  363.         }  
  364.           
  365.         for(int i = 0;i<ids.length;i++){  
  366.             idsBuffer.append("?,");  
  367.             params.add(ids[i]);  
  368.         }  
  369.           
  370.         // 拼写sql语句  
  371.         StringBuffer sqlBuffer = new StringBuffer("delete from user where user_id in (");  
  372.         sqlBuffer.append(idsBuffer.substring(0, idsBuffer.length()-1)).append(")");  
  373.           
  374.         // 执行删除操作  
  375.         int result = ConnectionUtil.update(sqlBuffer.toString(), params);  
  376.         return result >0 ? true : false;  
  377.     }  
  378. }  
package com.user.dao.impl;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.common.util.ConnectionUtil;import com.common.util.StringUtil;import com.user.dao.UserDao;import com.user.model.User;import com.user.util.UserBeanUtil;import com.user.vo.UserVO;public class UserDaoImpl implements UserDao{		/**	 * 查询用户列表	 * @param userVo 	VO对象	 * @param page 		页数	 * @param maxRows 	每页显示记数	 * @param sort 		排序字段	 * @param order 	排序方式(asc或desc)	 * @return 	 */	public List<User> searchUser(UserVO userVo, int page, int maxRows,			String sort, String order) {		StringBuffer sqlBuffer = new StringBuffer("select * from user where 1=1");		// 添加参数		List<Object> params = new ArrayList<Object>();		if (userVo != null) {			// 用户名			if (!StringUtil.isEmpty(userVo.getUserName())) {				sqlBuffer.append(" and user_name like ? ");				params.add("%" + userVo.getUserName() + "%");			}			// 创建时间			if (!StringUtil.isEmpty(userVo.getStartCreateTime())) {				sqlBuffer.append(" and create_time >=?");				params.add(userVo.getStartCreateTime());			}			if (!StringUtil.isEmpty(userVo.getEndCreateTime())) {				sqlBuffer.append(" and create_time <=?");				params.add(userVo.getEndCreateTime());			}			// 更新时间			if (!StringUtil.isEmpty(userVo.getStartUpdateTime())) {				sqlBuffer.append(" and update_tiume >=?");				params.add(userVo.getStartUpdateTime());			}			if (!StringUtil.isEmpty(userVo.getEndUpdateTime())) {				sqlBuffer.append(" and update_tiume <=?");				params.add(userVo.getEndUpdateTime());			}						// 排序			if(!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)){				sqlBuffer.append(" order by ").append(UserBeanUtil.fieldToColumn(sort)).append(" ").append(order);			}						 // 分页			 if(page > 0 && maxRows > 0){				 // 公式:firstRows = (page - 1) * maxRows				 int firstRows = (page - 1) * maxRows ;				 sqlBuffer.append(" limit ").append(firstRows).append(",").append(maxRows);			 }		}				// 查询用户列表		List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(),params);		return UserBeanUtil.copyProperty(datas);	}		/**	 * 条件查询用户数量	 * @param userVo 	VO对象	 * @return 	 */	public long getCountUser(UserVO userVo) {		 StringBuffer sqlBuffer = new StringBuffer("select count(user_id) from user where 1=1");		// 添加参数		List<Object> params = new ArrayList<Object>();		if (userVo != null) {			// 用户名			if (!StringUtil.isEmpty(userVo.getUserName())) {				sqlBuffer.append(" and user_name like ? ");				params.add("%" + userVo.getUserName() + "%");			}			// 创建时间			if (!StringUtil.isEmpty(userVo.getStartCreateTime())) {				sqlBuffer.append(" and create_time >=?");				params.add(userVo.getStartCreateTime());			}			if (!StringUtil.isEmpty(userVo.getEndCreateTime())) {				sqlBuffer.append(" and create_time <=?");				params.add(userVo.getEndCreateTime());			}			// 更新时间			if (!StringUtil.isEmpty(userVo.getStartUpdateTime())) {				sqlBuffer.append(" and update_tiume >=?");				params.add(userVo.getStartUpdateTime());			}			if (!StringUtil.isEmpty(userVo.getEndUpdateTime())) {				sqlBuffer.append(" and update_tiume <=?");				params.add(userVo.getEndUpdateTime());			}			// 返回总记录数			return ConnectionUtil.queryCount(sqlBuffer.toString(),params);		}		return 0;	}		/**	 * 获得最大的用户编号	 * @return	 */	public long getMaxUserId(){		StringBuffer sqlBuffer = new StringBuffer("select max(user_id) from user;");		return ConnectionUtil.queryCount(sqlBuffer.toString(), null);	}		/**	 * 根据用户名查询用户count用户个数	 * @param userName 用户名	 * @return	 */	public long getUserCountByName(String userName){		if(StringUtil.isEmpty(userName)){			return Integer.MAX_VALUE;		}		// 接写sql语句		StringBuffer sqlBuffer = new StringBuffer("select count(user_id) from user where 1=1 and user_name = ?");		// 添加参数		List<Object> params = new ArrayList<Object>();		params.add(userName);		// 返回查询的用		return ConnectionUtil.queryCount(sqlBuffer.toString(), params);	}		/** 	 * 根据名称获得用户对象	 * @param userName 用户名	 * @return	 */	public User getUserByName(String userName){		if(StringUtil.isEmpty(userName)){			return null;		}		// 接写sql语句		StringBuffer sqlBuffer = new StringBuffer("select * from user where 1=1 and user_name = ?");		// 添加参数		List<Object> params = new ArrayList<Object>();		params.add(userName);		// 查询用户列表		List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(),params);		List<User> userList = UserBeanUtil.copyProperty(datas);		// 返回user		if(userList != null && userList.size() > 0){			return userList.get(0);					}		return null;	}		/**	 * 判断用户名是否唯一	 * @param userName	 * @return 是唯一返回true,不是唯一返回false	 */	public boolean getUniqueUserName(String userName,String userId){		// 参数为空判断 		if(StringUtil.isEmpty(userName)){			return false;		}				if(StringUtil.isEmpty(userId)){			return false;		}				// 接写sql语句		StringBuffer sqlBuffer = new StringBuffer("select count(*) from user where user_name = ? and user_id != ? ");		// 添加参数		List<Object> params = new ArrayList<Object>();		params.add(userName);		params.add(userId);				// 查询用户列表		long count = ConnectionUtil.queryCount(sqlBuffer.toString(),params);		return count <= 0 ? true : false;	} 			/**	 * 根据用户编号获得用户对象	 * @param userId 用户号	 * @return	 */	public User getUserById(String userId){		// 为空判断		if(StringUtil.isEmpty(userId)){			return null;		}				// 接接sql语句		StringBuffer sqlBuffer = new StringBuffer("select * from user where user_id = ?");				// 添加参数		List<Object> params = new ArrayList<Object>();		params.add(userId);				// 查询用户		List<Map<String, Object>> datas = ConnectionUtil.queryList(sqlBuffer.toString(), params);		List<User> userList = UserBeanUtil.copyProperty(datas);				if(userList != null && userList.size() > 0){			return userList.get(0);					}		return null;	}			/**	 * 保存用户	 * @param user 用户对象	 * @return	 */	public boolean saveUser(User user){		// 用户为空判断		if(user == null){			return false;		}				// 拼接sql语句		StringBuffer sqlBuffer = new StringBuffer("insert into user(user_id,user_name,user_pass,user_sex,create_time,update_time)");		sqlBuffer.append("values(?,?,?,?,?,?)");				// 添加参数		List<Object> params = new ArrayList<Object>();				// 用户编号		if(!StringUtil.isEmpty(user.getUserId()+"")){			params.add(user.getUserId());		}				// 用户名		if(!StringUtil.isEmpty(user.getUserName())){			params.add(user.getUserName());		}				// 密码		if(!StringUtil.isEmpty(user.getPassword())){			params.add(user.getPassword());		}				// 性别		if(!StringUtil.isEmpty(user.isSex() + "")){			params.add(user.isSex());		}				// 创建时间		if(!StringUtil.isEmpty(user.getCreateTime())){			params.add(user.getCreateTime());		}				// 更新时间		if(!StringUtil.isEmpty(user.getUpdateTime())){			params.add(user.getUpdateTime());		}				// 判断param.size参数的长度是否为6,检验数填写是否完成		if(params.size() != 6){			return false;		}		// 保存		int result = ConnectionUtil.update(sqlBuffer.toString(), params);		// 返回执行结果		return result >= 1 ? true : false;	}		/**	 * 更新用户	 * @param user 用户对象	 * @return	 */	public boolean updateUser(User user){		// 用户为空判断		if(user == null){			return false;		}				// 拼接sql语句		StringBuffer sqlBuffer = new StringBuffer("update user set user_name=?,user_pass=?,user_sex=?,create_time=?,update_time=?");		sqlBuffer.append(" where user_id=?");		// 添加参数		List<Object> params = new ArrayList<Object>();				// 用户名		if(!StringUtil.isEmpty(user.getUserName())){			params.add(user.getUserName());		}				// 密码		if(!StringUtil.isEmpty(user.getPassword())){			params.add(user.getPassword());		}				// 性别		if(!StringUtil.isEmpty(user.isSex() + "")){			params.add(user.isSex());		}				// 创建时间		if(!StringUtil.isEmpty(user.getCreateTime())){			params.add(user.getCreateTime());		}				// 更新时间		if(!StringUtil.isEmpty(user.getUpdateTime())){			params.add(user.getUpdateTime());		}				// 用户编号		if(!StringUtil.isEmpty(user.getUserId()+"")){			params.add(user.getUserId());		}				// 判断param.size参数的长度是否为6,检验数填写是否完成		if(params.size() != 6){			return false;		}		// 保存		int result = ConnectionUtil.update(sqlBuffer.toString(), params);		// 返回执行结果		return result >= 1 ? true : false;	}		/**	 * 删除用户 	 * @param userIds 用户编号字符串,以“,”分隔	 * @return	 */	public boolean deleteUser(String userIds){		// 判断id是否存在		if(StringUtil.isEmpty(userIds)){			return false;		}				// 添加参数		StringBuffer idsBuffer = new StringBuffer("");		List<Object> params = new ArrayList<Object>();				String [] ids = userIds.split(",");		if(ids == null || ids.length < 0){			return false;		}				for(int i = 0;i<ids.length;i++){			idsBuffer.append("?,");			params.add(ids[i]);		}				// 拼写sql语句		StringBuffer sqlBuffer = new StringBuffer("delete from user where user_id in (");		sqlBuffer.append(idsBuffer.substring(0, idsBuffer.length()-1)).append(")");				// 执行删除操作		int result = ConnectionUtil.update(sqlBuffer.toString(), params);		return result >0 ? true : false;	}}

1.2.1.6 Service层UserService接口类

1.      Service层的UserService接口代码和Dao层UserDao接口代码一样,可以直接复制过去,此处,不再写重复代码。

1.2.1.7 Service层UserServiceImpl接口实现类

  1. package com.user.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.user.dao.UserDao;  
  6. import com.user.dao.impl.UserDaoImpl;  
  7. import com.user.model.User;  
  8. import com.user.service.UserService;  
  9. import com.user.vo.UserVO;  
  10.   
  11.   
  12. public class UserServiceImpl implements UserService{  
  13.       
  14.     private UserDao userDao = new UserDaoImpl();  
  15.   
  16.     /** 
  17.      * 查询用户列表 
  18.      * @param userVo    VO对象 
  19.      * @param page      页数 
  20.      * @param maxRows   每页显示记数 
  21.      * @param sort      排序字段 
  22.      * @param order     排序方式(asc或desc) 
  23.      * @return  
  24.      */  
  25.     public List<User> searchUser(UserVO userVo, int page, int maxRows,  
  26.             String sort, String order){  
  27.         return userDao.searchUser(userVo, page, maxRows, sort, order);  
  28.     }  
  29.       
  30.     /** 
  31.      * 或者用户数量 
  32.      * @param userVo    VO对象 
  33.      * @return  
  34.      */  
  35.     public long getCountUser(UserVO userVo){  
  36.         return userDao.getCountUser(userVo);  
  37.     }  
  38.       
  39.     /** 
  40.      * 获得最大的用户编号 
  41.      * @return 
  42.      */  
  43.     public long getMaxUserId(){  
  44.         return userDao.getMaxUserId();  
  45.     }  
  46.       
  47.     /** 
  48.      * 根据用户名查询用户count用户个数 
  49.      * @param userName 用户名 
  50.      * @return 
  51.      */  
  52.     public long getUserCountByName(String userName){  
  53.         return userDao.getUserCountByName(userName);  
  54.     }  
  55.       
  56.     /**  
  57.      * 根据名称获得用户对象 
  58.      * @param userName 用户名 
  59.      * @return 
  60.      */  
  61.     public User getUserByName(String userName){  
  62.         return userDao.getUserByName(userName);  
  63.     }  
  64.       
  65.     /** 
  66.      * 判断用户名是否唯一 
  67.      * @param userName 
  68.      * @return 是唯一返回true,不是唯一返回false 
  69.      */  
  70.     public boolean getUniqueUserName(String userName,String userId){  
  71.         return userDao.getUniqueUserName(userName, userId);  
  72.     }  
  73.       
  74.       
  75.     /** 
  76.      * 根据用户编号获得用户对象 
  77.      * @param userId 用户号 
  78.      * @return 
  79.      */  
  80.     public User getUserById(String userId){  
  81.         return userDao.getUserById(userId);  
  82.     }  
  83.       
  84.       
  85.     /** 
  86.      * 保存用户 
  87.      * @param user 用户对象 
  88.      * @return 
  89.      */  
  90.     public boolean saveUser(User user){  
  91.         return userDao.saveUser(user);  
  92.     }  
  93.       
  94.     /** 
  95.      * 更新用户 
  96.      * @param user 用户对象 
  97.      * @return 
  98.      */  
  99.     public boolean updateUser(User user){  
  100.         return userDao.updateUser(user);  
  101.     }  
  102.       
  103.     /** 
  104.      * 删除用户  
  105.      * @param userIds 用户编号字符串,以“,”分隔 
  106.      * @return 
  107.      */  
  108.     public boolean deleteUser(String userIds){  
  109.         return userDao.deleteUser(userIds);  
  110.     }  
  111.   
  112.   
  113.     /** 
  114.      * userDao 对象 getter方法 ... 
  115.      * @return 
  116.      */  
  117.     public UserDao getUserDao() {  
  118.         return userDao;  
  119.     }  
  120.       
  121.     /** 
  122.      * userDao 对象 setter方法 ... 
  123.      * @return 
  124.      */  
  125.     public void setUserDao(UserDao userDao) {  
  126.         this.userDao = userDao;  
  127.     }  
  128. }  
package com.user.service.impl;import java.util.List;import com.user.dao.UserDao;import com.user.dao.impl.UserDaoImpl;import com.user.model.User;import com.user.service.UserService;import com.user.vo.UserVO;public class UserServiceImpl implements UserService{		private UserDao userDao = new UserDaoImpl();	/**	 * 查询用户列表	 * @param userVo 	VO对象	 * @param page 		页数	 * @param maxRows 	每页显示记数	 * @param sort 		排序字段	 * @param order 	排序方式(asc或desc)	 * @return 	 */	public List<User> searchUser(UserVO userVo, int page, int maxRows,			String sort, String order){		return userDao.searchUser(userVo, page, maxRows, sort, order);	}		/**	 * 或者用户数量	 * @param userVo 	VO对象	 * @return 	 */	public long getCountUser(UserVO userVo){		return userDao.getCountUser(userVo);	}		/**	 * 获得最大的用户编号	 * @return	 */	public long getMaxUserId(){		return userDao.getMaxUserId();	}		/**	 * 根据用户名查询用户count用户个数	 * @param userName 用户名	 * @return	 */	public long getUserCountByName(String userName){		return userDao.getUserCountByName(userName);	}		/** 	 * 根据名称获得用户对象	 * @param userName 用户名	 * @return	 */	public User getUserByName(String userName){		return userDao.getUserByName(userName);	}		/**	 * 判断用户名是否唯一	 * @param userName	 * @return 是唯一返回true,不是唯一返回false	 */	public boolean getUniqueUserName(String userName,String userId){		return userDao.getUniqueUserName(userName, userId);	}			/**	 * 根据用户编号获得用户对象	 * @param userId 用户号	 * @return	 */	public User getUserById(String userId){		return userDao.getUserById(userId);	}			/**	 * 保存用户	 * @param user 用户对象	 * @return	 */	public boolean saveUser(User user){		return userDao.saveUser(user);	}		/**	 * 更新用户	 * @param user 用户对象	 * @return	 */	public boolean updateUser(User user){		return userDao.updateUser(user);	}		/**	 * 删除用户 	 * @param userIds 用户编号字符串,以“,”分隔	 * @return	 */	public boolean deleteUser(String userIds){		return userDao.deleteUser(userIds);	}	/**	 * userDao 对象 getter方法 ...	 * @return	 */	public UserDao getUserDao() {		return userDao;	}		/**	 * userDao 对象 setter方法 ...	 * @return	 */	public void setUserDao(UserDao userDao) {		this.userDao = userDao;	}}

1.2.1.8 User类型帮助类UserBeanUtil类

  1. package com.user.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.user.model.User;  
  8.   
  9. /** 
  10.  * 用户实体 
  11.  */  
  12. public class UserBeanUtil {  
  13.     /** 
  14.      * 拷贝属性值,返回对象列表 
  15.      * @param datas 
  16.      * @return 
  17.      */  
  18.     public static List<User> copyProperty(List<Map<String, Object>> datas){  
  19.         // 返回的用户列表  
  20.         List<User> userList = new ArrayList<User>();  
  21.         if(datas != null && datas.size() > 0){  
  22.             for (Map<String, Object> map : datas) {  
  23.                 // map 为判断  
  24.                 if(!map.isEmpty()){  
  25.                     //遍历map  
  26.                     User user = new User();  
  27.                     for(Map.Entry<String, Object> entry : map.entrySet()){  
  28.                         if("user_id".equals(entry.getKey())){  
  29.                             user.setUserId(Integer.parseInt(entry.getValue()+""));  
  30.                         }  
  31.                           
  32.                         if("user_name".equals(entry.getKey())){  
  33.                             user.setUserName(entry.getValue()+"");  
  34.                         }  
  35.                           
  36.                         if("user_pass".equals(entry.getKey())){  
  37.                             user.setPassword(entry.getValue()+"");  
  38.                         }  
  39.                           
  40.                         if("user_sex".equals(entry.getKey())){  
  41.                             user.setSex((entry.getValue()+"").equals("0") ? false : true);  
  42.                         }  
  43.                           
  44.                         if("create_time".equals(entry.getKey())){  
  45.                             user.setCreateTime(entry.getValue()+"");  
  46.                         }  
  47.                           
  48.                         if("update_time".equals(entry.getKey())){  
  49.                             user.setUpdateTime(entry.getValue()+"");  
  50.                         }  
  51.                     }  
  52.                     userList.add(user);  
  53.                 }  
  54.             }  
  55.         }  
  56.         return userList;  
  57.     }  
  58.       
  59.     /** 
  60.      * 字段名转化为列名(注:如果使用ORM映射框架,将不在需要该方法) 
  61.      * @param field  
  62.      * @return  
  63.      */  
  64.     public static String fieldToColumn(String field){  
  65.         // 用户编号  
  66.         if("userId".equals(field)){  
  67.             return "user_id";  
  68.         }  
  69.           
  70.         // 用户名  
  71.         if("userName".equals(field)){  
  72.             return "user_name";  
  73.         }  
  74.           
  75.         // 密码  
  76.         if("password".equals(field)){  
  77.             return "user_pass";  
  78.         }  
  79.           
  80.         // 性别  
  81.         if("sex".equals(field)){  
  82.             return "user_sex";  
  83.         }  
  84.           
  85.         // 创建时间  
  86.         if("createTime".equals(field)){  
  87.             return "create_time";  
  88.         }  
  89.         // 更新时间  
  90.         if("updateTime".equals(field)){  
  91.             return "update_time";  
  92.         }  
  93.         return "user_id";  
  94.     }  
  95. }  
package com.user.util;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.user.model.User;/** * 用户实体 */public class UserBeanUtil {	/**	 * 拷贝属性值,返回对象列表	 * @param datas	 * @return	 */	public static List<User> copyProperty(List<Map<String, Object>> datas){		// 返回的用户列表		List<User> userList = new ArrayList<User>();		if(datas != null && datas.size() > 0){			for (Map<String, Object> map : datas) {				// map 为判断				if(!map.isEmpty()){					//遍历map					User user = new User();					for(Map.Entry<String, Object> entry : map.entrySet()){						if("user_id".equals(entry.getKey())){							user.setUserId(Integer.parseInt(entry.getValue()+""));						}												if("user_name".equals(entry.getKey())){							user.setUserName(entry.getValue()+"");						}												if("user_pass".equals(entry.getKey())){							user.setPassword(entry.getValue()+"");						}												if("user_sex".equals(entry.getKey())){							user.setSex((entry.getValue()+"").equals("0") ? false : true);						}												if("create_time".equals(entry.getKey())){							user.setCreateTime(entry.getValue()+"");						}												if("update_time".equals(entry.getKey())){							user.setUpdateTime(entry.getValue()+"");						}					}					userList.add(user);				}			}		}		return userList;	}		/**	 * 字段名转化为列名(注:如果使用ORM映射框架,将不在需要该方法)	 * @param field 	 * @return 	 */	public static String fieldToColumn(String field){		// 用户编号		if("userId".equals(field)){			return "user_id";		}				// 用户名		if("userName".equals(field)){			return "user_name";		}				// 密码		if("password".equals(field)){			return "user_pass";		}				// 性别		if("sex".equals(field)){			return "user_sex";		}				// 创建时间		if("createTime".equals(field)){			return "create_time";		}		// 更新时间		if("updateTime".equals(field)){			return "update_time";		}		return "user_id";	}}

1.2.1.9 实体类User类型

  1. package com.user.model;  
  2.   
  3. /** 
  4.  * 用户 实体类 
  5.  * PO:持久对象,与数据库中的表相映射的user对象 
  6.  * @author aebiz 
  7.  */  
  8. public class User {  
  9.     // 用户编号  
  10.     private int userId;  
  11.     // 用户名  
  12.     private String userName;  
  13.     // 密码  
  14.     private String password;  
  15.     // 性别 0:false 1:为true  
  16.     private boolean sex;  
  17.     // 创建时间  
  18.     private String createTime;  
  19.     // 更新时间  
  20.     private String updateTime;  
  21.       
  22.       
  23.     /** 
  24.      *  无参构造函数 
  25.      */  
  26.     public User() {  
  27.         super();  
  28.     }  
  29.   
  30.     /** 
  31.      * 全参构造函数 
  32.      * @param userId 
  33.      * @param userName 
  34.      * @param password 
  35.      * @param sex 
  36.      * @param createTime 
  37.      * @param updateTime 
  38.      */  
  39.     public User(int userId, String userName, String password, boolean sex,  
  40.             String createTime, String updateTime) {  
  41.         super();  
  42.         this.userId = userId;  
  43.         this.userName = userName;  
  44.         this.password = password;  
  45.         this.sex = sex;  
  46.         this.createTime = createTime;  
  47.         this.updateTime = updateTime;  
  48.     }  
  49.   
  50.     public int getUserId() {  
  51.         return userId;  
  52.     }  
  53.   
  54.     public void setUserId(int userId) {  
  55.         this.userId = userId;  
  56.     }  
  57.   
  58.     public String getUserName() {  
  59.         return userName;  
  60.     }  
  61.   
  62.     public void setUserName(String userName) {  
  63.         this.userName = userName;  
  64.     }  
  65.   
  66.     public String getPassword() {  
  67.         return password;  
  68.     }  
  69.   
  70.     public void setPassword(String password) {  
  71.         this.password = password;  
  72.     }  
  73.   
  74.     public boolean isSex() {  
  75.         return sex;  
  76.     }  
  77.   
  78.     public void setSex(boolean sex) {  
  79.         this.sex = sex;  
  80.     }  
  81.   
  82.     public String getCreateTime() {  
  83.         return createTime;  
  84.     }  
  85.   
  86.     public void setCreateTime(String createTime) {  
  87.         this.createTime = createTime;  
  88.     }  
  89.   
  90.     public String getUpdateTime() {  
  91.         return updateTime;  
  92.     }  
  93.   
  94.     public void setUpdateTime(String updateTime) {  
  95.         this.updateTime = updateTime;  
  96.     }  
  97. }  
package com.user.model;/** * 用户 实体类 * PO:持久对象,与数据库中的表相映射的user对象 * @author aebiz */public class User {	// 用户编号	private int userId;	// 用户名	private String userName;	// 密码	private String password;	// 性别 0:false 1:为true	private boolean sex;	// 创建时间	private String createTime;	// 更新时间	private String updateTime;			/**	 *  无参构造函数	 */	public User() {		super();	}	/**	 * 全参构造函数	 * @param userId	 * @param userName	 * @param password	 * @param sex	 * @param createTime	 * @param updateTime	 */	public User(int userId, String userName, String password, boolean sex,			String createTime, String updateTime) {		super();		this.userId = userId;		this.userName = userName;		this.password = password;		this.sex = sex;		this.createTime = createTime;		this.updateTime = updateTime;	}	public int getUserId() {		return userId;	}	public void setUserId(int userId) {		this.userId = userId;	}	public String getUserName() {		return userName;	}	public void setUserName(String userName) {		this.userName = userName;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public boolean isSex() {		return sex;	}	public void setSex(boolean sex) {		this.sex = sex;	}	public String getCreateTime() {		return createTime;	}	public void setCreateTime(String createTime) {		this.createTime = createTime;	}	public String getUpdateTime() {		return updateTime;	}	public void setUpdateTime(String updateTime) {		this.updateTime = updateTime;	}}

1.2.1.10 业务层之间的数据传递UserVO类型

  1. package com.user.vo;  
  2.   
  3. import com.user.model.User;  
  4. /** 
  5.  * UserVO 
  6.  * OV:value object值对象,VO用在商业逻辑层和表示层。 
  7.  * 各层操作属于该层自己的数据对象,这样就可以降低各层之间的耦合,便于以后系统的维护和扩展。 
  8.  * @author yuanxw 
  9.  * 
  10.  */  
  11. public class UserVO extends User {  
  12.     // 开始时间  
  13.     private String startCreateTime;  
  14.     // 结束时间  
  15.     private String endCreateTime;  
  16.   
  17.     // 开始更新时间  
  18.     private String startUpdateTime;  
  19.     // 结束更新时间  
  20.     private String endUpdateTime;  
  21.       
  22.     /** 
  23.      * 空构造函数 
  24.      */  
  25.     public UserVO() {  
  26.         super();  
  27.     }  
  28.   
  29.     /** 
  30.      * User父类全参构造函数 
  31.      * @param userId 
  32.      * @param userName 
  33.      * @param password 
  34.      * @param sex 
  35.      * @param createTime 
  36.      * @param updateTime 
  37.      */  
  38.     public UserVO(int userId, String userName, String password, boolean sex,  
  39.             String createTime, String updateTime) {  
  40.         this.setUserId(userId);  
  41.         this.setUserName(userName);  
  42.         this.setPassword(password);  
  43.         this.setSex(sex);  
  44.         this.setCreateTime(createTime);  
  45.         this.setUpdateTime(updateTime);  
  46.     }  
  47.       
  48.   
  49.     public String getStartCreateTime() {  
  50.         return startCreateTime;  
  51.     }  
  52.   
  53.     public void setStartCreateTime(String startCreateTime) {  
  54.         this.startCreateTime = startCreateTime;  
  55.     }  
  56.   
  57.     public String getEndCreateTime() {  
  58.         return endCreateTime;  
  59.     }  
  60.   
  61.     public void setEndCreateTime(String endCreateTime) {  
  62.         this.endCreateTime = endCreateTime;  
  63.     }  
  64.   
  65.     public String getStartUpdateTime() {  
  66.         return startUpdateTime;  
  67.     }  
  68.   
  69.     public void setStartUpdateTime(String startUpdateTime) {  
  70.         this.startUpdateTime = startUpdateTime;  
  71.     }  
  72.   
  73.     public String getEndUpdateTime() {  
  74.         return endUpdateTime;  
  75.     }  
  76.   
  77.     public void setEndUpdateTime(String endUpdateTime) {  
  78.         this.endUpdateTime = endUpdateTime;  
  79.     }  
  80. }  
package com.user.vo;import com.user.model.User;/** * UserVO * OV:value object值对象,VO用在商业逻辑层和表示层。 * 各层操作属于该层自己的数据对象,这样就可以降低各层之间的耦合,便于以后系统的维护和扩展。 * @author yuanxw * */public class UserVO extends User {	// 开始时间	private String startCreateTime;	// 结束时间	private String endCreateTime;	// 开始更新时间	private String startUpdateTime;	// 结束更新时间	private String endUpdateTime;		/**	 * 空构造函数	 */	public UserVO() {		super();	}	/**	 * User父类全参构造函数	 * @param userId	 * @param userName	 * @param password	 * @param sex	 * @param createTime	 * @param updateTime	 */	public UserVO(int userId, String userName, String password, boolean sex,			String createTime, String updateTime) {		this.setUserId(userId);		this.setUserName(userName);		this.setPassword(password);		this.setSex(sex);		this.setCreateTime(createTime);		this.setUpdateTime(updateTime);	}		public String getStartCreateTime() {		return startCreateTime;	}	public void setStartCreateTime(String startCreateTime) {		this.startCreateTime = startCreateTime;	}	public String getEndCreateTime() {		return endCreateTime;	}	public void setEndCreateTime(String endCreateTime) {		this.endCreateTime = endCreateTime;	}	public String getStartUpdateTime() {		return startUpdateTime;	}	public void setStartUpdateTime(String startUpdateTime) {		this.startUpdateTime = startUpdateTime;	}	public String getEndUpdateTime() {		return endUpdateTime;	}	public void setEndUpdateTime(String endUpdateTime) {		this.endUpdateTime = endUpdateTime;	}}

1.2.2 JDBC单元测试

1.2.2.1 Junit概述

1.     JUnit就是为Java程序开发者实现单元测试提供一种框架,使得Java单元测试更规范有效,并且更有利于测试的集成。

2.      Junit下载地址:http://junit.org/

3.      Junit-4.11.jar最新jar包:

Junit-4.11.jar

1.2.2.2 Junit测试Jdbc

  1. package com.user.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.user.model.User;  
  6. import com.user.service.UserService;  
  7. import com.user.service.impl.UserServiceImpl;  
  8. import com.user.vo.UserVO;  
  9.   
  10. import junit.framework.TestCase;  
  11.   
  12. /** 
  13.  * Junit测试 
  14.  * 使用JUnit,主要都是通过继承TestCase类别来撰写测试用例,使用testXXX()名称来撰写单元测试。 
  15.  * JUnit可以大量减少Java代码中程序错误的个数,JUnit是一种流行的单元测试框架,用于在发布代码之前对其进行单元测试。 
  16.  * 使用JUnit好处: 
  17.  * 1.可以使测试代码与产品代码分开。 
  18.  * 2.针对某一个类的测试代码通过较少的改动便可以应用于另一个类的测试。 
  19.  * 3.易于集成到测试人员的构建过程中,JUnit和Ant的结合可以实施增量开发。 
  20.  * 4.JUnit是公开源代码的,可以进行二次开发。 
  21.  * 5.可以方便地对JUnit进行扩展。 
  22.  * @author yuanxw 
  23.  * 
  24.  */  
  25. public class UserTest extends TestCase{  
  26.     private UserService userService = new UserServiceImpl();  
  27.       
  28.     /** 
  29.      * 测试  查询用户列表 
  30.      */  
  31.     public void testSearchUser(){  
  32.         System.out.println("===========testSearchUser============");  
  33.         // 设置查询条件  
  34.         UserVO userVo = new UserVO();  
  35.         userVo.setStartCreateTime("2013-07-24 17:40:16");  
  36.         userVo.setEndCreateTime("2013-08-27 11:25:27");  
  37.           
  38.         // 从查询页码  
  39.         int page = 1;  
  40.         // 显示记录数  
  41.         int maxRows = 10;  
  42.           
  43.         // 排序属性  
  44.         String sort = "userId";  
  45.         // 排序方式  
  46.         String order = "desc";  
  47.         // 用户集合  
  48.         List<User> userList = userService.searchUser(userVo, page, maxRows, sort, order);  
  49.         if(userList != null && userList.size() > 0){  
  50.             for (User user : userList) {  
  51.                 System.out.println("userId=====>" + user.getUserId());  
  52.                 System.out.println("userName===>"+ user.getUserName());  
  53.                 System.out.println("password===>"+ user.getPassword());  
  54.                 System.out.println("sex========>"+ user.isSex());  
  55.                 System.out.println("createTime=>"+ user.getCreateTime());  
  56.                 System.out.println("updateTime=>"+ user.getUpdateTime());  
  57.                 System.out.println("=====================================\r");  
  58.             }  
  59.         }  
  60.         System.out.println("===========testSearchUser============");  
  61.     }  
  62.       
  63.     /** 
  64.      * 测试  条件查询用户数量 
  65.      */  
  66.     public void testGetCountUser(){  
  67.         System.out.println("===========testGetCountUser============");  
  68.         // 设置查询条件  
  69.         UserVO userVo = new UserVO();  
  70.         userVo.setStartCreateTime("2013-07-24 17:40:16");  
  71.         userVo.setEndCreateTime("2013-08-27 11:25:27");  
  72.           
  73.         long count= userService.getCountUser(userVo);  
  74.         System.out.println("count===>" + count);  
  75.         System.out.println("===========testGetCountUser============");  
  76.     }  
  77.     /** 
  78.      * 测试  获得最大的用户编号 
  79.      */  
  80.     public void testGetMaxUserId(){  
  81.         System.out.println("===========tetGetMaxUserId============");  
  82.         long maxId = userService.getMaxUserId();  
  83.         System.out.println("maxId===>" + maxId);  
  84.         System.out.println("===========tetGetMaxUserId============");  
  85.     }  
  86.   
  87.   
  88.     /** 
  89.      * 测试  根据用户名查询用户count用户个数 
  90.      */  
  91.     public void testGetUserCountByName(){  
  92.         System.out.println("===========testGetUserCountByName============");  
  93.         long count = userService.getUserCountByName("yuan_xw");  
  94.         System.out.println("count===>" + count);  
  95.         System.out.println("===========testGetUserCountByName============");  
  96.     }  
  97.       
  98.     /** 
  99.      * 根据名称获得用户对象 
  100.      * @param userName 
  101.      */  
  102.     public void testGetUserByName(){  
  103.         System.out.println("===========testGetUserByName============");  
  104.         User user = userService.getUserByName("yuan_xw");  
  105.         if(user != null){  
  106.             System.out.println("userId=====>" + user.getUserId());  
  107.             System.out.println("userName===>"+ user.getUserName());  
  108.             System.out.println("password===>"+ user.getPassword());  
  109.             System.out.println("sex========>"+ user.isSex());  
  110.             System.out.println("createTime=>"+ user.getCreateTime());  
  111.             System.out.println("updateTime=>"+ user.getUpdateTime());  
  112.         }  
  113.         System.out.println("===========testGetUserByName============");  
  114.     }  
  115.       
  116.     /** 
  117.      * 测试  判断用户名是否唯一 
  118.      * 是唯一返回true,不是唯一返回false 
  119.      */  
  120.     public void testGetUniqueUserName(){  
  121.         System.out.println("===========testGetUniqueUserName============");  
  122.         boolean result = userService.getUniqueUserName("yuan_xw", "1");  
  123.         System.out.println("result===>" + result);  
  124.         System.out.println("===========testGetUniqueUserName============");  
  125.     }  
  126.       
  127.     /** 
  128.      * 测试  根据用户编号获得用户对象 
  129.      */  
  130.     public void testGetUserById(){  
  131.         System.out.println("===========testGetUserById============");  
  132.         User user = userService.getUserById("1");  
  133.         if(user != null){  
  134.             System.out.println("userId=====>" + user.getUserId());  
  135.             System.out.println("userName===>"+ user.getUserName());  
  136.             System.out.println("password===>"+ user.getPassword());  
  137.             System.out.println("sex========>"+ user.isSex());  
  138.             System.out.println("createTime=>"+ user.getCreateTime());  
  139.             System.out.println("updateTime=>"+ user.getUpdateTime());  
  140.         }  
  141.         System.out.println("===========testGetUserById============");  
  142.     }  
  143.     /** 
  144.      * 测试  保存用户 
  145.      * 保存成功:true 保存失败:false 
  146.      */  
  147.     public void testSaveUser(){  
  148.         System.out.println("===========testSaveUser============");  
  149.         // 获得最大的用户编号  
  150.         long maxId = userService.getMaxUserId();  
  151.           
  152.         // 创建保存用户  
  153.         UserVO userVO = new UserVO((int) (maxId+1), "test", "123456",  
  154.             false, "2013-09-12 22:54:29", "2013-09-12 22:54:29");  
  155.         // 保存用户  
  156.         boolean result = userService.saveUser(userVO);  
  157.         System.out.println("result===>" + result);  
  158.         System.out.println("===========testSaveUser============");  
  159.     }  
  160.       
  161.     /** 
  162.      * 测试  更新用户 
  163.      * 更新成功:true 更新失败:false 
  164.      */  
  165.     public void testUpdateUser(){  
  166.         System.out.println("===========testUpdateUser============");  
  167.         // 获得最大的用户编号  
  168.         long maxId = userService.getMaxUserId();  
  169.           
  170.         // 创建修改用户  
  171.         UserVO userVO = new UserVO((int) (maxId), "update_test", "999999",  
  172.                 true, "2013-09-12 22:54:29", "2013-09-12 23:03:00");  
  173.         // 保存用户  
  174.         boolean result = userService.updateUser(userVO);  
  175.         System.out.println("result===>" + result);  
  176.         System.out.println("===========testUpdateUser============");  
  177.     }  
  178.       
  179.     /** 
  180.      * 测试  删除用户  
  181.      */  
  182.     public void testDeleteUser(){  
  183.         System.out.println("===========testDeleteUser============");  
  184.         // 用户编号字符串,以“,”分隔  
  185.         long maxId = userService.getMaxUserId();  
  186.         String userIds = maxId+"";  
  187.         boolean result = userService.deleteUser(userIds);  
  188.         System.out.println("result===>" + result);  
  189.         System.out.println("===========testDeleteUser============");  
  190.     }  
  191. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章