spring整合jdbc方法一
阅读原文时间:2023年07月08日阅读:1

用了一段时间的spring这,闲来没事做一下spring整合jdbc

目录文件

导入jar包

由于spring的jar包是在myeclipse中自动导入的有些暂时用不到的也没有处理。

Emp类

package com.v2uc.entity;

import java.util.Date;

public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;

public Integer getEmpno() {  
    return empno;  
}  
public void setEmpno(Integer empno) {  
    this.empno = empno;  
}  
public String getEname() {  
    return ename;  
}  
public void setEname(String ename) {  
    this.ename = ename;  
}  
public String getJob() {  
    return job;  
}  
public void setJob(String job) {  
    this.job = job;  
}  
public Integer getMgr() {  
    return mgr;  
}  
public void setMgr(Integer mgr) {  
    this.mgr = mgr;  
}  
public Date getHiredate() {  
    return hiredate;  
}  
public void setHiredate(Date hiredate) {  
    this.hiredate = hiredate;  
}  
public Double getSal() {  
    return sal;  
}  
public void setSal(Double sal) {  
    this.sal = sal;  
}  
public Double getComm() {  
    return comm;  
}  
public void setComm(Double comm) {  
    this.comm = comm;  
}  
public Integer getDeptno() {  
    return deptno;  
}  
public void setDeptno(Integer deptno) {  
    this.deptno = deptno;  
}

}

映射文件EmpMapping

package com.v2uc.entity;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class EmpMapping implements RowMapper {
public Emp mapRow(ResultSet rs, int rowIndex) throws SQLException{
Emp emp = new Emp();
emp.setEmpno(rs.getInt("EMPNO"));
emp.setEname(rs.getString("ENAME"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("MGR"));
emp.setHiredate(rs.getDate("HIREDATE"));
emp.setSal(rs.getDouble("SAL"));
emp.setComm(rs.getDouble("COMM"));
emp.setDeptno(rs.getInt("DEPTNO"));
return emp;
}
}

EmpDao接口

package com.v2uc.dao;

import java.util.List;
import com.v2uc.entity.Emp;

public interface EmpDao {

public void save(Emp emp);  
public void update(Emp emp);  
public void delete(int no);  
public Emp findByNo(int no);  
public List<Emp> findAll();

}

JdbcEmpDao实现

package com.v2uc.dao;

import java.util.List;

import javax.activation.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.v2uc.entity.Emp;
import com.v2uc.entity.EmpMapping;

public class JdbcEmpDao extends JdbcDaoSupport implements EmpDao {

private DataSource dataSource;

public void setDataSource(DataSource dataSource){  
    this.dataSource = dataSource;  
}

@Override  
public void save(Emp emp) {  
    // TODO Auto-generated method stub

}

@Override  
public void update(Emp emp) {  
    // TODO Auto-generated method stub

}

@Override  
public void delete(int no) {  
    // TODO Auto-generated method stub

}

@Override  
public Emp findByNo(int no) {  
    // TODO Auto-generated method stub  
    return null;  
}

@Override  
public List<Emp> findAll() {  
    String sql = "select \* from t\_emp";  
    RowMapper<Emp> mapper = new EmpMapping();  
    List<Emp> list = super.getJdbcTemplate().query(sql,mapper);  
    return list;  
}

}

xml配置


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">


Test单元测试

package com.v2uc.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.v2uc.dao.EmpDao;
import com.v2uc.entity.Emp;

public class Test {

@Test  
public void test() {  
    String conf = "applicationContext.xml";  
    ApplicationContext ac = new ClassPathXmlApplicationContext(conf);  
    EmpDao empDao = ac.getBean("jdbcEmpDao1",EmpDao.class);  
    List<Emp> list = empDao.findAll();  
    for(Emp emp : list){  
        System.out.println(emp.getEname());  
    }

}

}

遇到问题

Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')

maven 依赖换版本
mysql mysql-connector-java 5.1.6

更换5.1.6的jar包即可解决该问题

简单原创地址:http://www.cnblogs.com/maoyali/p/8813710.html