💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
1.beans.xml配置 ~~~ //db.properties文件 jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///goods jdbc.initPoolSize=5 jdbc.maxPoolSize=10 <!-- 导入资源文件 --> <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="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></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> ~~~ 2.jdbcTemplate的增删改查以及批量操作 ~~~ public class MainTest { private ApplicationContext ac = null; private JdbcTemplate jdbcTemplate; { ac = new ClassPathXmlApplicationContext("beans.xml"); jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate"); } /* * insert,update,delete的sql语句都需要调用jdbcTemplate的update方法 * */ @Test public void testUpdate(){ String sql="update user set u_name=? where u_id=?"; jdbcTemplate.update(sql, "张三",3); } /* * 批量操作 insert update delete */ @Test public void test2(){ // String sql="insert into user(u_name,u_jifen) values(?,?)"; // List<Object[]> list=new ArrayList(); // list.add(new Object[]{"王五1",20}); // list.add(new Object[]{"王五2",20}); // list.add(new Object[]{"王五3",20}); // jdbcTemplate.batchUpdate(sql, list); //批量删除 String sql="delete from user where u_id=?"; List<Object[]> list=new ArrayList(); list.add(new Object[]{4}); list.add(new Object[]{5}); list.add(new Object[]{6}); jdbcTemplate.batchUpdate(sql, list); } /* * 测试查询单行操作 * 要使用RowMapper 指定封装规则 */ @Test public void testSelectOne(){ String sql="select * from user where u_id=?"; RowMapper<User> mapper=new BeanPropertyRowMapper<>(User.class); User user=jdbcTemplate.queryForObject(sql, mapper, 1); System.out.println(user); } /* * 测试查询多条数据 */ @Test public void testQueryList(){ String sql="select * from user where u_id>?"; RowMapper<User> mapper=new BeanPropertyRowMapper<>(User.class); List<User> list=jdbcTemplate.query(sql, mapper, 1); System.out.println(list); } /* * 测试查询单一(一个)数据 */ @Test public void testSelectOneColumn(){ String sql="select u_jifen from user where u_id=?"; int jifen=jdbcTemplate.queryForObject(sql, Integer.class, 2); System.out.println(jifen); } @Test public void test1() { // spring整合JDBC之后,使用JdbcTemplate对象 ,去完成对数据库的各种操作 。 // 注意:JdbcTemplate对象中需要注入 DataSource对象 。。。 // JdbcTemplate 是由Spring框架提供,这个类对象JDBC操作,进行了封装 。 // 使用者 只需要编写SQL语句 ,处理数据封装就可以了 。。。 // JdbcTemplate 多表操作的时候比较麻烦 并不是ORM框架,而是jdbc //System.out.println(jdbcTemplate); DataSource ds = (DataSource) ac.getBean("dataSource"); //System.out.println(ds); } } ~~~ [代码位置](https://gitee.com/limiao11/springframework/tree/master/spring_jdbc) ~~~ 附: Spring使用dbcp数据源 导入两个jar包: commons-pool-1.5.6jar / commons-dbcp-1.4jar 配置文件修改如下: <!-- 配置dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/demo1"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> ~~~