Idea配合Maven创建Java项目进行数据库进行增删改查

项目框架:

Idea配合Maven创建Java项目进行数据库进行增删改查_第1张图片

在Maven项目里,可以通过在配置文件中写入依赖来获取jar包(也可以把jar包放入main下的resourses文件夹(和导入依赖是一样的),还有c3p0的配置文件,也是放在main下的resourses文件夹下,当进行compile后,结构和我们之前用eclipse看到的结构是一样的)

pom.xml配置文件:



    4.0.0

    com.yht
    EmpCRUD
    1.0.0

    

        
        
            org.projectlombok
            lombok
            1.16.20
            provided
        

        
        com.mchange
        c3p0
        0.9.5.2
    

        
            mysql
            mysql-connector-java
            5.1.38
        

        
            commons-dbutils
            commons-dbutils
            1.6
        

        
            com.mchange
            mchange-commons-java
            0.2.15
        
        
        
            junit
            junit
            4.12
            test
        
    

c3p0-comfig.xml:






    
        
        
        
        com.mysql.jdbc.Driver
        jdbc:mysql:///dbmaven
        root
        mkl

        
        
        5
        10
        2000
        1000

    

    
    
    
        com.mysql.jdbc.Driver
        jdbc:mysql:///dbc3p0
        root
        yht

        
        
        5
        10
        2000
        1000
    

Emp:

package com.yht.bean;


import lombok.*;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    private String birthday;
    private Double salary;
}

EmpDao:

package com.yht.dao;

import com.yht.bean.Emp;

import java.util.List;

public interface EmpDao {
    public int insertEmp(Emp emp);

    public int updateEmp(Emp emp);

    public int deleteEmp(Integer id);

    public List findAllEmp();

    public Emp findEmpById(Integer id);
}

EmpDaoImpl:

package com.yht.dao.impl;

import com.yht.bean.Emp;
import com.yht.dao.EmpDao;
import com.yht.utils.C3p0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmpDaoImpl implements EmpDao {

    public int insertEmp(Emp emp) {
        int result = 0;
        QueryRunner qr = new QueryRunner(C3p0Util.getDataSource());

        String sql = "insert into emp values(null,?,?,?,?)";

        try {
            result = qr.update(sql,emp.getName(),emp.getAge(),emp.getBirthday(),emp.getSalary());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int updateEmp(Emp emp) {
        int result = 0;
        QueryRunner qr = new QueryRunner(C3p0Util.getDataSource());
        String sql = "update emp set salary = ? where id = ?";

        try {
            result = qr.update(sql,emp.getSalary(),emp.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int deleteEmp(Integer id) {
        int result = 0;
        QueryRunner qr = new QueryRunner(C3p0Util.getDataSource());
        String sql = "delete from emp where id = ?";
        try {
            result = qr.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  result;
    }

    public List findAllEmp() {
        List list = new ArrayList();
        QueryRunner qr = new QueryRunner(C3p0Util.getDataSource());
        BeanListHandler blh = new BeanListHandler(Emp.class);
        String sql = "select * from emp";
        try {
            list =(List)qr.query(sql,blh);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  list;
    }

    public Emp findEmpById(Integer id) {
        Emp emp = new Emp();
        QueryRunner qr = new QueryRunner(C3p0Util.getDataSource());
        String sql = "select * from emp where id = ?";
        try {
            emp = (Emp)qr.query(sql,new BeanHandler(Emp.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return emp;
    }
}

 EmpTest:

package com.yht.test;

import com.yht.bean.Emp;
import com.yht.dao.EmpDao;
import com.yht.dao.impl.EmpDaoImpl;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class EmpTest {

    @Test
    public void insertEmpTest(){
        Emp emp = new Emp();
        emp.setName("罗宾");
        emp.setAge(20);
        emp.setBirthday("2016-03-02");
        emp.setSalary(20000.0);
        EmpDao dao = new EmpDaoImpl();
        int result = dao.insertEmp(emp);
        if(result==1){
            System.out.println("插入测试成功了");
        }
    }

    @Test
    public void updateEmpTest(){
        Emp emp = new Emp();
        emp.setId(2);
        emp.setSalary(100000.0);
        EmpDao dao = new EmpDaoImpl();
        int result = dao.updateEmp(emp);
        if(result==1){
            System.out.println("更改测试成功了");
        }
    }

    @Test
    public void deleteEmpTest(){
        int id = 8;
        EmpDao dao = new EmpDaoImpl();
        int result = dao.deleteEmp(id);
        if(result==1){
            System.out.println("删除成功了");
        }
    }

    @Test
    public void findAllEmpTest(){
        List list = new ArrayList();
        EmpDao dao = new EmpDaoImpl();
        list = dao.findAllEmp();
        for(Emp emp : list){
            System.out.println(emp);
        }
    }

    @Test
    public void findEmpByIdTest(){
        Emp emp = new Emp();
        EmpDao dao = new EmpDaoImpl();
        Integer id = 1;
        emp = dao.findEmpById(id);
        System.out.println(emp);
    }
}

 

测试:

单独测试方法1:

Idea配合Maven创建Java项目进行数据库进行增删改查_第2张图片

单独测试方法2:

Idea配合Maven创建Java项目进行数据库进行增删改查_第3张图片

全部测试:

Idea配合Maven创建Java项目进行数据库进行增删改查_第4张图片

 

你可能感兴趣的:(#SSM思维框架,Idea,maven,java项目,增删改查)