JdbcTemplet是Spring下一个很好用的jdbc小工具,而且使用起来也是很简单
需求:实现基本的数据库操作
贴上代码:
dataSource.properties
jdbc.user = root
jdbc.password =123456
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql:///spring1
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
Student.java
package cn.limbo.spring.jdbc;
/**
* Created by Limbo on 16/7/14.
*/
public class Student {
private int id;
private String studentName;
private String email;
private int deptId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", studentName='" + studentName + '\'' +
", email='" + email + '\'' +
", deptId=" + deptId +
'}';
}
}
package cn.limbo.spring.jdbc;
/**
* Created by Limbo on 16/7/14.
*/
public class Department {
private int id;
private String name;
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;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
JDBCTest.java
package cn.limbo.spring.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Limbo on 16/7/14.
*/
public class JDBCTest {
private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
private static JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
private static NamedParameterJdbcTemplate namedParameterJdbcTemplate = ctx.getBean(NamedParameterJdbcTemplate.class);
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
/**
* 执行UPDATE,INSERT,DELETE
*/
public static void testUpdate()
{
String sql = "UPDATE student SET student_name = ? WHERE id = ?";
jdbcTemplate.update(sql,"Kylin",2);
}
/**
* 批量INSERT,UPDATE,DELETE
* 最后一个参数是Object[]的list类型,因为修改一条记录需要一个object数组,那么多条就需要多个
*/
public static void BatchUpdate()
{
String sql = "INSERT INTO student(id,student_name,email,Dept_No)VALUES(?,?,?,?)";
List
StudentDao.java
package cn.limbo.spring.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
/**
* Created by Limbo on 16/7/15.
*/
@Repository
public class StudentDao{
@Autowired
private JdbcTemplate jdbcTemplate;
public Student get(int id)
{
String sql = "SELECT * FROM student WHERE id = ?";
RowMapper rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql,rowMapper,1);
return student;
}
}