1.首先先添加两个包:
c3p0-0.9.1.2.jar 和 mysql-connector-java-5.1.7-bin.jar
创建一个file jdbc.properties
DriverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc\:mysql\://localhost\:3306/0902?useUnicode\=true&characterEncoding\=UTF-8 user=root pass=root
2.创建一个java类
Emp.java
package cn.nm.domain; import java.io.Serializable; import java.util.Date; public class Emp implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String name; private String sex; private String pass; private Date hiredate; public Emp() { super(); // TODO Auto-generated constructor stub } public Emp(Integer id, String name, String sex, String pass, Date hiredate) { super(); this.id = id; this.name = name; this.sex = sex; this.pass = pass; this.hiredate = hiredate; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }
3.定义一个接口类
EmpDao.java
package cn.nm.dao; import java.util.List; import cn.nm.domain.Emp; public interface EmpDao { /*插入的操作*/ boolean insert(Emp entity); /*更新操作*/ boolean update(Emp entity); /*删除的操作*/ boolean delete(Emp entity); /*根据主键id查询*/ Emp findById(Integer id); /*查询所有*/ List<Emp> findAll(); }
4.创建一个EmpDaoImpl.java
package cn.nm.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import cn.nm.domain.Emp; public class EmpDaoImpl implements EmpDao { /* 封装一个JdbcTemplate的模板对象 */ private JdbcTemplate jdbcTemplate; /* 通过set方法注入进来即可 */ public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public boolean delete(Emp entity) { // 定义返回结果 boolean flag = false; /* 插入实现 */ int i = jdbcTemplate.update("delete from emp where id=? ", new Object[] { entity.getId() }); if (i > 0) { flag = true; } return flag; } @SuppressWarnings("unchecked") @Override public List<Emp> findAll() { // 定义返回结果 List<Emp> entities =new ArrayList<Emp>(); entities = (List<Emp>) jdbcTemplate.query( "select id,name,sex,pass,hiredate from emp", new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Emp emp = new Emp(); emp.setId(rs.getInt("id")); emp.setName(rs.getString("name")); emp.setSex(rs.getString("sex")); emp.setPass(rs.getString("pass")); emp.setHiredate(rs.getDate("hiredate")); return emp; } }); return entities; } @Override public Emp findById(Integer id) { // 定义返回结果 Emp entity = null; entity = (Emp) jdbcTemplate.queryForObject( "select id,name,sex,pass,hiredate from emp where id=?", new Object[] { id }, new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Emp emp = new Emp(); emp.setId(rs.getInt("id")); emp.setName(rs.getString("name")); emp.setSex(rs.getString("sex")); emp.setPass(rs.getString("pass")); emp.setHiredate(rs.getDate("hiredate")); return emp; } }); return entity; } @Override public boolean insert(Emp entity) { // 定义返回结果 boolean flag = false; /* 插入实现 */ int i = jdbcTemplate.update( "insert into emp(name,sex,pass,hiredate) values(?,?,?,?)", new Object[] { entity.getName(), entity.getSex(), entity.getPass(), entity.getHiredate() }); if (i > 0) { flag = true; } return flag; } @Override public boolean update(Emp entity) { // 定义返回结果 boolean flag = false; /* 更新实现 */ int i = jdbcTemplate .update( "update emp set name=?,sex=?,pass=?,hiredate=? where id=?", new Object[] { entity.getName(), entity.getSex(), entity.getPass(), entity.getHiredate(), entity.getId() }); if (i > 0) { flag = true; } return flag; } }
5.修改配置文件
<!-- 配置数据源c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据库驱动 --> <property name="driverClass" value="${DriverClass}"/> <!-- 数据库连接的URL --> <property name="jdbcUrl" value="${jdbcUrl}"/> <!-- 数据库连接的用户名 --> <property name="user" value="${user}"/> <!-- 数据库连接的用户密码 --> <property name="password" value="${pass}"/> </bean> <!-- 配置jdbc的模板类JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置操作的DaoImpl实现类 --> <bean id="empDaoImpl" class="cn.redarmys.dao.EmpDaoImpl"> <!-- 注入模板类对象 --> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <!-- 分散配置解析 --> <context:property-placeholder location="jdbc.properties"/>
6.创建一个测试类
package cn.nm.dao; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.nm.domain.Emp; public class App { @Test public void app(){ ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:app*.xml"); /*获取DaoImp的实体对象*/ EmpDao ed = (EmpDao) ac.getBean("empDaoImpl"); /*for (int i = 1; i <11; i++) { 插入验证 Emp emp = new Emp(null, "redarmy"+i, "男", "redarmy"+i+"@gmai.com", new Date()); boolean flag = ed.insert(emp); if(flag){ System.out.println("插入成功"); }else{ System.out.println("插入失败"); } }*/ /*根据主键查询ID*/ //Emp emp = ed.findById(11); /*System.out.println( "ID::"+emp.getId()+" Name::"+emp.getName()+" sex::"+emp.getSex()+" pass:::"+emp.getPass()+" hiredate::"+emp.getHiredate()); */ /*更新操作*/ /*emp.setName("孙彤彤"); emp.setPass("123"); emp.setHiredate(new Date()); ed.update(emp);*/ /*删除操作*/ //ed.delete(emp); List<Emp> emps = ed.findAll(); for(Emp emp:emps){ System.out.println( "ID::"+emp.getId()+" Name::"+emp.getName()+" sex::"+emp.getSex()+" pass:::"+emp.getPass()+" hiredate::"+emp.getHiredate()); } } }