spring中JDBC的应用

    xiaoxiao2026-08-27  1

      下面以一个具体的代码来说明spring中jdbc的应用

    1.Dept

    package cn.itcast.h_jdbc; public class Dept{ private int deptId; private String deptName; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } } 2.UserDao

    package cn.itcast.h_jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class UserDao{ // IOC容器注入 /*private DataSource dataSource; public void setDataSource(DataSource dataSource){ this.dataSource=dataSource; }*/ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate=jdbcTemplate; } public void save(){ String sql="insert into t_dept (deptName) values('test')"; jdbcTemplate.update(sql); } //根据id查询记录 public Dept findById(int id){ String sql="select * from t_dept where deptId=?"; List<Dept> list=jdbcTemplate.query(sql, new MyResult(),id); return (list!=null&&list.size()>0)?list.get(0):null; } //查询所有记录 public List<Dept> getAll(){ String sql="select * from t_dept"; List<Dept> list=jdbcTemplate.query(sql, new MyResult()); return list; } class MyResult implements RowMapper<Dept>{ // 如何封装一行记录 @Override public Dept mapRow(ResultSet rs, int index) throws SQLException { Dept dept=new Dept(); dept.setDeptId(rs.getInt("deptId")); dept.setDeptName("deptName"); return dept; } } } 3.bean.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" 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"> <!-- 1. 数据源对象: C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///hib-demo"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean> <!-- 2. 创建JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- dao 实例 --> <bean id="userDao" class="cn.itcast.h_jdbc.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans> 4.Junit测试

    package cn.itcast.h_jdbc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/h_jdbc/bean.xml"); // 目标对象有实现接口,spring会自动选择“JDK代理” @Test public void testApp(){ UserDao userDao=(UserDao) ac.getBean("userDao"); System.out.println(userDao.getClass());//$Proxy001 userDao.save(); System.out.println(userDao.findById(1)); System.out.println(userDao.getAll()); } }

    转载请注明原文地址: https://ju.6miu.com/read-1311613.html
    最新回复(0)