调用的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
查询、批量查询、更新(添加、删除、修改)的实现方式
package top.biglin.jdbctest;
import org.junit.Test;
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 top.biglin.spring.jdbc.Employee;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Chiawei
* @date 2019/1/14 13:12
*/
public class JDBCTest {
private ApplicationContext ctx = null;
private JdbcTemplate jdbcTemplate;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
}
/**
* 获取单个的值
*/
@Test
public void testQueryForObject2(){
String sql = "SELECT count(id) FROM employee";
long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
/**
* 从数据库中查询一组对象
*/
@Test
public void testQueryForList(){
// String sql = "SELECT id,last_name lastName,email, dept_id as \"department\" FROM employee ";
String sql = "SELECT id,last_name lastName,email FROM employee where id > ?";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
List<Employee> employees = jdbcTemplate.query(sql, rowMapper, 2);
System.out.println(employees);
}
/**
* 从数据库中获取一条记录,实际得到对应的一个对象
*/
@Test
public void testQueryForObject(){
String sql = "SELECT id,last_name lastName,email FROM employee WHERE id = ? ";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
System.out.println(employee);
}
@Test
public void testBatchUpdate(){
String sql = "INSERT INTO employee(id, last_name,email) values(?, ?, ?)";
List<Object[]> batchArgs = new ArrayList<Object[]>();
batchArgs.add(new Object[]{1,"AA", "1534369714@qq.com"});
batchArgs.add(new Object[]{2,"BB", "1534369714@qq.com"});
batchArgs.add(new Object[]{3,"CC", "1534369714@qq.com"});
batchArgs.add(new Object[]{4,"DD", "1534369714@qq.com"});
int[] result;
result = jdbcTemplate.batchUpdate(sql,batchArgs);
System.out.println(result[0]);
for (int i:
result) {
System.out.println(i);
}
}
@Test
public void testUpdate(){
String sql = "UPDATE administrator SET name = ? WHERE id = ?";
int result = jdbcTemplate.update(sql, "Jack", 2);
System.out.println("Update " + result + " rows");
}
@Test
public void testJDBC() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
System.out.println("JDBCTest...");
}
}
实际开发中如何写Dao
package top.biglin.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;
/**
* @author Chiawei
* @date 2019/1/14 16:39
*/
@Repository
public class EmployeeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public Employee get(Integer id) {
String sql = "SELECT id,last_name lastName,email FROM employee WHERE id = ? ";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
return employee;
}
}