本项目是在maven中构建的,所以要在pom.xml中加入dependency:
mysql
mysql-connector-java
5.0.5
org.springframework
spring-web
4.3.1.RELEASE
org.springframework
spring-webmvc
4.3.1.RELEASE
org.springframework
spring-jdbc
4.3.6.RELEASE
c3p0
c3p0
0.9.1.2
commons-dbcp
commons-dbcp
1.4
(1)数据库中有的字段名字,写进java文件中:
import java.io.Serializable;
/**
* 数据库中的数据字段
*/
public class Emp implements Serializable{
private int id;
private String name;
private String sex;
private String age;
private String tel;
public Emp(){
super();
}
public Emp(int id,String name,String sex,String age,String tel){
super();
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.tel=tel;
}
public int getId(){
return id;
}
public void setId(int 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 getAge(){
return age;
}
public void setAge(String age){
this.age=age;
}
public String getTel(){
return tel;
}
public void setTel(String tel){
this.tel=tel;
}
}
(2)定义接口(增删改查):
public interface EmpDao {
/*插入的操作*/
boolean insert(Emp entity);
/*查询所有*/
List findAll();
}
(3)实现上面接口的类(增删改查):
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 实现EmpDao接口的类
*/
public class EmpDaoImpl implements EmpDao {
private JdbcTemplate jdbcTemplate;
public EmpDaoImpl(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
@SuppressWarnings("unchecked")
public List findAll() {
// 定义返回结果
List entities =new ArrayList();
entities = (List) jdbcTemplate.query(
"select id,name,sex from students", new RowMapper() {
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"));
return emp;
}
});
return entities;
}
//插入数据
public boolean insert(Emp entity) {
// 定义返回结果
boolean flag = false;
/* 插入实现 */
int i = jdbcTemplate.update(
"insert into students(id,name,sex,age,tel) values(?,?,?,?,?)",
new Object[] { entity.getId(), entity.getName(),entity.getSex(),
entity.getAge(),entity.getTel()});
if (i > 0) {
flag = true;
}
return flag;
}
}
(4)配置类:要进行数据库连接,数据的注入,把jdbcTemplate注入到EmpDaoImpl 中:
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* Created by Administrator on 2017/3/8.
*/
@Configuration
public class EmDaoConfig {
@Bean
public BasicDataSource dataSource(){
BasicDataSource ds=new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");//加载驱动
ds.setUrl("jdbc:mysql://localhost:3306/mydata?");
ds.setUsername("root");
ds.setPassword("86914381");
ds.setInitialSize(5); //池启动时创建的连接数量
ds.setMaxActive(10); //同一时间可以从池中分配的最多连接数。如果设置为0,则无限制
return ds;
}
@Bean //加入数据
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean //jdbcTemplate注入依赖
public EmpDao empDaoImpl(JdbcTemplate jdbcTemplate){
return new EmpDaoImpl(jdbcTemplate);
}
}
(5)测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
/**
* 测试实例
*/
public class EmDaoTest {
public static void main(String[] args){
ApplicationContext ac=new AnnotationConfigApplicationContext(EmDaoConfig.class);
//获取实例
EmpDao ed = ac.getBean(EmpDao.class);
List emps = ed.findAll();
for(Emp emp:emps){
System.out.println(
"ID::"+emp.getId()+" Name::"+emp.getName()+" sex::"+emp.getSex());
}
//插入数据
Emp emp=new Emp(20,"llqllq","男","10","15603004804");
boolean flag=ed.insert(emp);
if(flag){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
}
}