日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring jdbc的搭建

發布時間:2025/3/20 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring jdbc的搭建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

?

首先得在pom.xml中新增兩個依賴,不然你會發現要用DriverManagerDataSource這個類都沒有

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.43</version></dependency>

然后在controller里寫了段并加斷點測試一下,與數據庫聯通了,沒毛病

DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setUrl("jdbc:mysql://localhost:3306/gotoxoyo");dataSource.setUsername("root");dataSource.setPassword("go111112");dataSource.setDriverClassName("com.mysql.jdbc.Driver");JdbcCustomerDao customerDao = new JdbcCustomerDao();customerDao.setDataSource(dataSource);Customer customer = new Customer(1, "mkyong",28);customerDao.insert(customer);Customer customer1 = customerDao.findByCustomerId(1);System.out.println(customer1);

現在該把這個數據配置移到xml中了,在src根目錄下新建applicationContext.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/gotoxoyo" /><property name="username" value="root" /><property name="password" value="go111112" /></bean><bean id="customerDao" class="com.gotoxoyo.demo.dao.JdbcCustomerDao"><property name="dataSource" ref="dataSource" /></bean><!-- <context:property-placeholder location="jdbc.properties"/>--> </beans>

現在還有個問題,controller層如果要用dao類和pojo類,還需要初始化,如下代碼:

JdbcCustomerDao customerDao = new JdbcCustomerDao();customerDao.setDataSource(dataSource);Customer customer = new Customer(1, "mkyong",28);customerDao.insert(customer);Customer customer1 = customerDao.findByCustomerId(1);System.out.println(customer1);

能不能直接讓spring初始化呢,可以的,在applicationContext.xml中配置

<bean id="customerDao" class="com.gotoxoyo.demo.dao.JdbcCustomerDao"><property name="dataSource" ref="dataSource" /></bean><bean class="com.gotoxoyo.demo.pojo.Customer" id="customer"><property name="custId" value="2" /><property name="name" value="Yang" /><property name="age" value="32" /></bean>

然后controller層的類就只需要兩句代碼了

@Resourceprivate CustomerDao customerDao;public CustomerDao getCustomerDao() {return customerDao;}public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}@Resourceprivate Customer customer;public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}在方法里只需一句代碼customerDao.insert(customer);

?

轉載于:https://my.oschina.net/xoyo/blog/1503282

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Spring jdbc的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。