當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring jdbc的搭建
生活随笔
收集整理的這篇文章主要介紹了
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的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 印度威普罗集团斥资5 亿美元收购云计算解
- 下一篇: 高并发測试工具webbench