org.sPRingframework.jdbc.core.JdbcTemplate 是spring中对数据库进行操作的核心类看API 文档:
再看该类的父类org.springframework.jdbc.support.Jdbcaccessor 所以在IOC容器中创建JdbcTemplate对象的时候有构造方法和属性注入两种方式 用c3p0数据源 com.mchange.v2.c3p0.ComboPooledDataSource
dao层:
import java.io.Serializable;import org.springframework.jdbc.core.JdbcTemplate;import com.pactera.pojo.User;import com.pactera.util.UserRowMapper;public class UserDao{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate = jdbcTemplate; } // 保存 public void save(User user){ String sql = "insert into user(name,age)values(?,?)"; jdbcTemplate.update(sql, user.getName(),user.getAge()); } //查询 public User queryById(Serializable id){ String sql = "select * from user where id = ?"; User user = jdbcTemplate.queryForObject(sql,new UserRowMapper(), id); return user; }}pojo类
package com.pactera.pojo;public class User { public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String name; private int age; @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; }}数据库字段和实体类的映射关系
package com.pactera.util;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import com.pactera.pojo.User;public class UserRowMapper implements RowMapper<User>{ //对于查询的一行数据数据库字段与实体类的属性如何对应 @Override public User mapRow(ResultSet rs, int index) throws SQLException { User user = new User(); user.setAge(rs.getInt("age")); user.setName(rs.getString("name")); return user; }}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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:MySQL://localhost:3306/test?useUnicode=true&characterEncoding=utf8"> </property> <property name="user" value="root"></property> <property name="passWord" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="50"></property> <property name="acquireIncrement" value="5"></property> </bean> <!-- 创建jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 这两种方式都可以 --> <constructor-arg name="dataSource" ref="dataSource"></constructor-arg> <!-- <property name="dataSource" ref="dataSource"></property> --> </bean> <bean id="userDao" class="com.pactera.daoImpl.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean></beans>测试插入
@Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = ac.getBean("userDao",UserDao.class); User user = new User(); user.setAge(12); user.setName("张三"); userDao.save(user); }数据库结果:
查询
@Test public void test2() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = ac.getBean("userDao",UserDao.class); int id = 1; User user = userDao.queryById(id); System.out.println(user); }结果
新闻热点
疑难解答