欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象

這篇文章主要介紹“如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象”,在日常操作中,相信很多人在如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)是一家專業(yè)提供羅江企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為羅江眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

用spring 創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象
3.1添加ioc相關(guān)jar包

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.18.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.3.18.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.18.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.3.18.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>4.3.18.RELEASE</version>
</dependency>

3.2創(chuàng)建db.properties文件

driverClass=com.MySQL.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
user=root
password=123

3.3在applicationContext.xml文件中創(chuàng)建對(duì)象

<context:property-placeholder location="db.properties"></context:property-placeholder>

<!-- 創(chuàng)建一個(gè)連接池對(duì)象 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <property name="driverClass" value="${driverClass}" />
    <property name="jdbcUrl" value="${url}"/>
    <property name="user" value="${user}" />
    <property name="password" value="${password}" />
</bean>
<!-- 創(chuàng)建一個(gè)jdbcTemplate對(duì)象 并且通過(guò)構(gòu)造函數(shù) 將連接池對(duì)象 注入到j(luò)dbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <constructor-arg name="dataSource" ref="comboPooledDataSource" />
</bean>

3.4 修改dao 層

@Component
public class UsersDao implements IUsersDao {

    //注入jdbcTemplate對(duì)象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //update方法 (增 刪 改)
    @Override
    public void add(Users user) {

        String sql = "insert into users values(null,?,?)";

        jdbcTemplate.update(sql, user.getUname(), user.getPassword());

    }

3.5測(cè)試

@Test
public void test()
{
    ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

    IUsersDao usersDao=  applicationContext.getBean("usersDao",IUsersDao.class);

    Users user=new Users("xiaowanglaoshi","123");

    usersDao.add(user);
}

到此,關(guān)于“如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

網(wǎng)頁(yè)名稱:如何用spring創(chuàng)建ComboPooledDataSource和JdbcTemplate對(duì)象
文章源于:http://www.chinadenli.net/article16/goosdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名網(wǎng)站維護(hù)網(wǎng)站營(yíng)銷網(wǎng)站策劃用戶體驗(yàn)靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)