赞
踩
pom.xml
- !--
- 操作数据源
- 配置oracle数据库
- 然后再加载四要素
- -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <!-- spring-boot-starter-jdbc or spring-boot-starter-data-jpa -->
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>oracle</groupId>
- <artifactId>orcale</artifactId>
- <version>3.2.8</version>
- <scope>system</scope>
- <systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
- </dependency>
-
- <!-- 新的数据库连接池架包dbcp -->
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>

BeanContainer
- package cn.et.boot.lesson01.source;
-
- import javax.sql.DataSource;
-
- import org.apache.commons.dbcp.BasicDataSource;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
-
-
- /**
- * 相当于标示该类是一个bean容器
- * @author Administrator
- */
- @Configuration
- public class BeanContainer {
-
- /**
- * @Bean
- * 表示该方法是创建一个bean
- * 方法名是bean的id
- *
- * @ConfigurationProperties
- * 读取application.properties文件
- * 设置前缀,自动读取application.properties文件中的属性
- */
- @ConfigurationProperties(prefix="mysource")
- @Bean
- public DataSource dataSource(){
- BasicDataSource dataSource = new BasicDataSource();
-
- //dataSource.setDriverClassName(driverClassName)
- return dataSource;
- }
-
- }

#数据源的四要素
mysource.url=jdbc:oracle:thin:@localhost:1521:orcl
mysource.username=scott
mysource.password=tiger
mysource.driverClassName=oracle.jdbc.OracleDriver
IocController
- package cn.et.boot.lesson01.source;
-
- import javax.sql.DataSource;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @RestController
- * 配置消息转换器 Jackson
- * 同时在Action上添加了@ResponseBody
- */
- @RestController
- /**
- * 自动增加spring.xml文件,并且配置自动扫描
- * 自动增加web.xml 同时在web.xml过滤器、拦截器...
- *
- * @EnableAutoConfiguration要替换成@SpringBootApplication
- * 不然扫描不到dao层,装配不了EmpDaoImpl
- * 要装配的对象必须位于HelloController类共一个包或它的子包下才可以扫描的到
- */
- @SpringBootApplication
- public class IocController {
-
- @Autowired
- DataSource dataSource;
-
- /**
- * 这里是可以直接返回List,@RestController会自动帮我们转换到json
- * @return
- */
- @RequestMapping("/dataSource")
- public String hello(){
- return "bean";
- }
-
- public static void main(String[] args) {
- //发布程序的方法入口
- SpringApplication.run(IocController.class, args);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。