題目需求:
使用IDEA 創建 MAVEN JAVA 項目
根據下圖 在mysql數據庫中創建員工表(T_Employee)
創建實體類 Employee 並創建DAO 層(使用JDBC技術)
以及測試類 對創建的DAO實現類進行增刪改查進行測試
項目結構
1.在數據庫中創建表(我使用的Navicat)
CREATE TABLE eee(
id INT(11) PRIMARY KEY auto_increment,
name VARCHAR(30),
age INT(11),
birthday date,
salary DECIMAL(10,0)
)
2.創建實體類Employee
package com.ujiuye.bean;
import lombok.*;
import java.util.Date;
//idea插件:lombok
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee {
public int id;
public String name;
public int age;
public Date birthday;
public int salary;
}
3.準備工作
3.1導包/添加依賴----pom.xml
學Maven之前我們需要在項目中創建一個名爲lib的文件夾,把JDBC,C3P0,dbutils所需的jar包放入文件夾下,但是現在我們學了Maven就不需要這樣導包了,只需要在項目的pom.xml文件中添加需要的依賴即可
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ujiuye</groupId>
<artifactId>employee</artifactId>
<version>1.0.0</version>
<name>employee</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
3.2c3p0-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<!-- 必須項 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///db3</property>
<property name="user">root</property>
<property name="password">1234</property>
<!-- 連接池參數 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="checkoutTimeout">2000</property>
<property name="maxIdleTime">1000</property>
</default-config>
</c3p0-config>
3.3項目所需的工具類
3.3.1c3p0獲取連接的工具類
package com.ujiuye.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Utils {
private static DataSource ds = new ComboPooledDataSource();
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection(){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
3.3.2字符串日期互相轉換的工具類
package com.ujiuye.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static Date stringToDate(String str){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String dateToString(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
3.4EmployeeDao.java文件
package com.ujiuye.dao;
import com.ujiuye.bean.Employee;
import java.util.List;
public interface EmployeeDao {
public List<Employee> findAllEmployee();
public int insert(Employee employee);
public int update(Employee employee);
public int deleteById(int id);
}
3.5EmployeeImpl.java文件
package com.ujiuye.dao.impl;
import com.ujiuye.bean.Employee;
import com.ujiuye.dao.EmployeeDao;
import com.ujiuye.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class EmployeeDaoImpl implements EmployeeDao{
@Override
public List<Employee> findAllEmployee() {
List<Employee> list = null;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from employee";
try {
list = qr.query(sql,new BeanListHandler<>(Employee.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int insert(Employee employee) {
int result = 0;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "insert into employee values(null,?,?,?,?)";
try {
result = qr.update(sql,new Object[]{employee.getName(),employee.getAge(),employee.getBirthday(),employee.getSalary()});
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public int update(Employee employee) {
int result = 0;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "update employee set name=?,age=?,birthday=?,salary=? where id=?";
try {
result = qr.update(sql,new Object[]{employee.getName(),employee.getAge(),employee.getBirthday(),employee.getSalary(),employee.getId()});
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public int deleteById(int id) {
int result = 0;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "delete from employee where id = ?";
try {
result = qr.update(sql,id);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
4.測試類
package com.ujiuye.bean;
import com.ujiuye.dao.EmployeeDao;
import com.ujiuye.dao.impl.EmployeeDaoImpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestEmployee {
EmployeeDao dao = new EmployeeDaoImpl();
@Test
public void testFindAllEmployee(){
List<Employee> list = dao.findAllEmployee();
for(Employee e : list){
System.out.println(e);
}
}
@Test
public void testInsert(){
Employee e = new Employee(5,"王二小",18,new Date(),1000);
int result = dao.insert(e);
System.out.println(result);
}
@Test
public void testUpdate(){
Employee e = new Employee(5,"tom",18,new Date(),1000);
int i = dao.update(e);
System.out.println(i+"更新");
}
@Test
public void testDelete(){
int i = dao.deleteById(6);
System.out.println(i+"刪除");
}
}