<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                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> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看