JdbcTemplate

JdbcTemplate:
导入jar包:
5个基本包+两个实现包+c3p0+dbcp:
实现包:
springJdbc操作:spring-framework-3.2.0.RELEASE\libs-->spring-jdbc-3.2.0.RELEASE.jar
spring事务:spring-framework-3.2.0.RELEASE\libs-->spring-tx-3.2.0.RELEASE.jar
c3p0:spring-framework-3.0.2dependencies\com.mchange.c3p0\com.springsource.com.mchange.v2.c3p0\0.9.1.2-->com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
dbcp:
spring-framework-3.0.2dependencies\org.apache.commons\com.springsource.org.apache.commons.dbcp\1.2.2.osgi-->com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
spring-framework-3.0.2dependencies\org.apache.commons\com.springsource.org.apache.commons.pool\1.5.3-->com.springsource.org.apache.commons.pool-1.5.3.jar

Use of api:
        creation of data source (connection pool) --dbcp
  BasicDataSource dataSource = new BasicDataSource();
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql://localhost :3306/dbname");
  dataSource.setUsername("root");
  dataSource.setPassword("1234");
Create template:
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
  jdbcTemplate.setDataSource(dataSource);
Use template:
  jdbcTemplate.update( "insert into t_user(username,password,age) values(?,?,?)", "jack","1234",18);
 }

Configure dbcp:
first implement dao:
 spring injection template instance
 private JdbcTemplate jdbcTemplate;
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 public User findById(Integer id){
  String sql = "select * from t_user where id = ? ";
  //Use ParameterizedBeanPropertyRowMapper to complete data encapsulation, the fields of the database table must be consistent with javabean properties
  RowMapper<User> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
  return jdbcTemplate.queryForObject(sql, rowMapper, id);
 }
spring configuration:
< !-- Configure data source dbcp -->
 <bean id="dataSourceId" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/dbname"></property>
  <property name="username" value="root"></property>
  <property name="password" value="1234"></property>
 </bean>
 <!-- 创建模板 -->
 <bean id="jdbcTemplateId" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSourceId"></property>
 </bean>
 <!-- dao -->
 <bean id="userDaoId" class="cn.itcast.b_dbcp.UserDao">
  <property name="jdbcTemplate" ref="jdbcTemplateId"></property>
 </bean>

Configure c3p0:
dao and template creation are basically unchanged
Note: When using c3p0, you can use attribute injection template (as above) or directly inject data source as attribute into
c3p0 When writing, the database driver name, URL, and user name are different from dbcp.
 <!-- Configure data source c3p0 -->
 <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql.jdbc.Driver" ></property>
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname"></property>
  <property name="user" value="root"></property>
  < property name="password" value="1234"></property>
 </bean>

JdbcDaoSupport:
dao:
inherits JdbcDaoSupport; uses the parent class method to manipulate data: this.getJdbcTemplate().update(...)
public class UserDao extends JdbcDaoSupport{
 
 public User findById(Integer id){
  String sql = "select * from t_user where id = ?";
  //Use ParameterizedBeanPropertyRowMapper to complete data encapsulation, the fields of the database table must be consistent with javabean properties
  RowMapper<User> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
  return getJdbcTemplate().queryForObject(sql, rowMapper, id);
 }
 public List<User> findAll(){
  String sql = "select * from t_user";
  RowMapper<User> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
  return this.getJdbcTemplate().query(sql, rowMapper);
 }

}
c3p0 configuration:
 <!-- Configure data source c3p0 -->
 <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql. jdbc.Driver"></property>
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname"></property>
  <property name="user" value="root">< /property>
  <property name="password" value="1234"></property>
 </bean>
 <!-- dao does not need a template
  * to inject data source into dao, UserDao has no setter, but the parent class provides
  * when After providing the data source to JdbcDaoSupport, the bottom layer will automatically create a template
 -->
 <bean id="userDaoId" class="cn.itcast.d_jdbcdaosupport.UserDao">
  <property name="dataSource" ref="dataSourceId"></property>
 </bean>

使用properties:
<!-- 加载properties配置
  * 之后通过 ${key}获得
 -->
 <context:property-placeholder location="classpath:cn/xxx/e_properties/jdbcInfo.properties"/>
 <!-- 配置数据源 c3p0 -->
 <bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="${jdbc.driverClass}"></property>
  <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  <property name="user" value="${jdbc.user}"></property>
  <property name="password" value="${jdbc.password}"></property>
 </bean>

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326561205&siteId=291194637