spring5 (18)- DAO-jdbc

jdbc 案例

 要求:使用jdbc完成CRUD操作
 拷贝依赖jar包,并BuildPath:
 mysql-connector-java-5.x.11.jar:mysql驱动包
 durid 连接池
 mysql-jdbc-版本.RELEASE.jar:支持jdbc
 spring-tx-版本.RELEASE.jar:支持事务

代码演示

(1)employee类

package com.keen.proxy.domain;

public class Employee {
    private int id;
    private String name;
    private int age;
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

}

(2)employeeDao接口

package com.keen.proxy.dao;

import java.util.List;

import com.keen.proxy.domain.Employee;

public interface EmployeeDAO {
    //增删改查
     void save(Employee e);
    
     void update(Employee e);
     
     void delete(int id);
      
     Employee get(int id);
     
     List listAll();
    
    
}

(3)employeDao实现类

package com.keen.proxy.dao;


import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

//import com.keen.proxy.dao.EmployeeDAO;
import com.keen.proxy.domain.Employee;
@Repository
public class EmployeeDAOimpl implements EmployeeDAO{
    private  JdbcTemplate jdbcTemplate;
    //属性:datasource
    public void setDataSource (DataSource ds)   {
        this.jdbcTemplate = new  JdbcTemplate(ds);
    }
    
    @Override
    public void save(Employee e) {
        jdbcTemplate.update("insert into employee (name ,age) value(?,?)", e.getName(),e.getAge());

        
    }

    @Override
    public void update(Employee e) {
        jdbcTemplate.update("UPDATE employee set name = ?,age = ? where id = ?", e.getName(),e.getAge(),e.getId());

        
    }

    @Override
    public void delete(int id) {
        jdbcTemplate.update("DELETE from employee where id = ?", id);

        
    }

    @Override
    public Employee get(int id) {

    List list = jdbcTemplate.query("select id,name,age from employee", new Object[] {},new RowMapper(){
             //把每行结果映射成一个employee对象
            @Override
            public Employee mapRow(ResultSet rs, int rowNum ) throws SQLException {
                Employee e = new Employee();
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setAge(rs.getInt("age"));
                return e;
               }
            });
        return list.size()==1? list.get(0) : null;
    }

    @Override
    public List listAll() {

    return  jdbcTemplate.query("select id,name,age from employee", new Object[] {},new RowMapper(){
             //把每行结果映射成一个employee对象
            @Override
            public Employee mapRow(ResultSet rs, int rowNum ) throws SQLException {
                Employee e = new Employee();
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setAge(rs.getInt("age"));
                return e;
               }
            });
        
    }

    

}

(4)xml配置文件








  
  
  
  
  



    




(5) 测试类

package com.keen.proxy;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.keen.proxy.dao.EmployeeDAO;
import com.keen.proxy.domain.Employee;

@SpringJUnitConfig
public class AutoTest {
    @Autowired
    private EmployeeDAO dao;
    
    @Test
    void testSave() throws Exception {
        Employee e = new Employee();
        e.setName("乔峰");
        e.setAge(30);
        dao.save(e);
        
    }
    @Test
    void testUpdate() throws Exception {
        Employee e = new Employee();
        e.setName("张一山");
        e.setAge(22);
        e.setId(5);
        dao.update(e);
        
    }
    @Test
    void testDelete() throws Exception {
        dao.delete(6);
        
        
    }
    @Test
    void testGet() throws Exception {
        Employee e = dao.get(1);
        System.out.println(e);
        
    }
    @Test
    void testListAll() throws Exception {
       List list = dao.listAll();
       for(Employee e : list) {
           System.out.println(e);
       }
    }
    
}

(6)resource 文件下写一个db.properties(连接数据库的参数文件)

你可能感兴趣的:(spring5 (18)- DAO-jdbc)