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());
- }
- }
- }