引入的jar包与基于注解的方式引入的jar包相同
实体类
Employee.java 对应数据库中的employee表
public class Employee { private Integer id; private String last_name; private String email; private Department department; //...省略get、set方法 }
Department.java 对应数据库中的department表
public class Department { private Integer id; private String name; //.....省略get、set方法 }
EmployeeDao.java
/** * 在实际开发中这么使用 * @author umgsai */ @Repository public class EmployeeDao { @Autowired private JdbcTemplate jdbcTemplate; public Employee get(Integer id) { String sql = "select id, last_name, email from employee where id = ?"; RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class); Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, id); return employee; } }
DepartmentDao.java
/** * 不推荐使用JdbcDaoSupport,而推荐使用JdbcTemplate作为Dao类的成员变量 * @author umgsai */ @Repository public class DepartmentDao extends JdbcDaoSupport{ //注入dataSource @Autowired public void setDataSource2(DataSource dataSource) { setDataSource(dataSource); } public Department get(Integer id) { Department department = null; String sql = "select id, name from department where id = ?"; RowMapper<Department> rowMapper = new BeanPropertyRowMapper<>(Department.class); department = (Department) getJdbcTemplate().queryForObject(sql, rowMapper, id); return department; } }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.spring.jdbc"></context:component-scan> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置Spring的JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
db.properties
jdbc.user=root jdbc.password=123456 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///test jdbc.initialPoolSize=5 jdbc.maxPoolSize=10
JDBCTest.java
public class JDBCTest { private ApplicationContext applicationContext = null; private JdbcTemplate jdbcTemplate; private EmployeeDao employeeDao; private DepartmentDao departmentDao; { applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); employeeDao = applicationContext.getBean(EmployeeDao.class); departmentDao = applicationContext.getBean(DepartmentDao.class); } @Test public void testDepartmentDao() { System.out.println(departmentDao.get(2)); } @Test public void testEmployeeDao() { System.out.println(employeeDao.get(9)); } /** * 获取单个列的值或做统计查询 */ @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, email, department_id as \"department.id\" from employee where id >0"; RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class); List<Employee>employees = jdbcTemplate.query(sql, rowMapper); System.out.println(employees); } /** * 从数据库获取一条记录,实际得到对应的一个对象。 * 不是ORM框架,不支持级联属性。 */ @Test public void testQueryForObject() { String sql = "select id, last_name, email, department_id as \"department.id\" from employee where id = ?"; RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class); Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 9); System.out.println(employee); } /** * 执行批量操作(update、insert、delete) */ @Test public void testBatch() { String sql = "insert into employee (last_name, email,department_id) values (?, ?, ?)"; List<Object[]>batchArgs = new ArrayList<Object[]>(); batchArgs.add(new Object[]{"dd", "umgsai@126.com", 2}); batchArgs.add(new Object[]{"ff", "umgsai@163.com", 2}); batchArgs.add(new Object[]{"rr", "umgsai@qq.com", 3}); batchArgs.add(new Object[]{"tt", "umgsai@sina.com", 4}); batchArgs.add(new Object[]{"yy", "umgsai@ss.com", 1}); batchArgs.add(new Object[]{"uu", "umgsai@baidu.com", 1}); jdbcTemplate.batchUpdate(sql, batchArgs); } @Test public void testUpdate() { String sql = "update employee set email = ? where id = ?"; jdbcTemplate.update(sql, "umgsai@126.com", 1); } @Test public void testDataSource() { DataSource dataSource = applicationContext.getBean(DataSource.class); try { System.out.println(dataSource.getConnection()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1557816