一、创建工程
1. 创建web工程
如何创建web工程,这里不细说,详情请参考第一课内容(https://blog.youkuaiyun.com/zj499063104/article/details/94493866);
2. 引入jar包
ioc+aop+jdbc+dbcp(前面四个jar包,下载地址)+mysql数据库驱动包(MySQL官网下载)
3. 如果没有MySQL数据库,需要下载安装
二、案例编写
1. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="cn.springjdbc"/>
<!-- 定义JdbcTemplate -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dbcp"></property>
</bean>
<!-- DataSource//数据源,连接池;dbcp,c3p0,proxool -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="asdf123456"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///test"></property>
</bean>
</beans>
2. 编写实体类Emp.java
package cn.springjdbc.entity;
import java.io.Serializable;
public class Emp implements Serializable{
private String name;
private Integer sex;
private Double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Emp(String name, Integer sex, Double salary) {
super();
this.name = name;
this.sex = sex;
this.salary = salary;
}
}
3.编写Dao方法
package cn.springjdbc.dao;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import cn.springjdbc.entity.Emp;
@Repository
public class EmpDao {
@Resource
private JdbcTemplate template;
public void save(Emp emp) {
String sql="insert into emp (name,sex,salary)values (?,?,?)";
Object[] params = {emp.getName(),emp.getSex(),emp.getSalary()};
template.update(sql, params);
}
public List<Emp> findAll(){
String sql="select * from emp";
EmpRowMapper rowMapper = new EmpRowMapper();
List<Emp> lst = template.query(sql, rowMapper);
return lst;
}
public List<Emp> findAllById(int id){
String sql="select * from emp where id=?";
EmpRowMapper rowMapper = new EmpRowMapper();
Object[] params = {id};
List<Emp> lst = template.query(sql, params, rowMapper);
return lst;
}
public void deleteById(int id) {
String sql ="delete from emp where id=?";
Object[] obj = {id};
template.update(sql, obj);
}
}
package cn.springjdbc.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import cn.springjdbc.entity.Emp;
public class EmpRowMapper implements RowMapper<Emp>{
@Override
public Emp mapRow(ResultSet rs, int arg1) throws SQLException {
//将当前rs指针指向的记录取出,封装成Emp返回
Emp emp = new Emp(rs.getString("name"),rs.getInt("sex"),rs.getDouble("salary"));
return emp;
}
}
4.编写测试类
package cn.springjdbc.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.springjdbc.dao.EmpDao;
import cn.springjdbc.entity.Emp;
public class TestEmpDao {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpDao empDao =ac.getBean("empDao", EmpDao.class);
// Emp emp = new Emp("小红",0,5000.0);
// empDao.save(emp);
// List<Emp> list = empDao.findAll();
// for(Emp emp : list) {
// System.out.println(emp.getName()+", "+emp.getSex()+", "+emp.getSalary());
// }
// List<Emp> list = empDao.findAllById(1);
// for(Emp emp : list) {
// System.out.println(emp.getName()+", "+emp.getSex()+", "+emp.getSalary());
// }
empDao.deleteById(2);
}
}