javascript
Spring框架第二天知识总结
一:DBCP與C3P0連接池的區(qū)別
DBCP通過BasicDataSource創(chuàng)建連接池對象,而C3P0通過ComboPooledDataSource創(chuàng)建連接池對象
步驟都是一樣的:
//創(chuàng)建一個連接池對象!!
BasicDataSource dataSource = new BasicDataSource(); 或者:
ComboPooledDataSource dataSource = new ComboPooledDataSource
//設(shè)置四大配置信息傳入dataSource 中
String driverClass="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/數(shù)據(jù)庫名?characterEncoding=utf8";
String username = "數(shù)據(jù)庫用戶名";
String password = "數(shù)據(jù)庫密碼";
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
// 創(chuàng)建DBUtils 核心類 QueryRunner對象把dataSource 傳進去
QueryRunner queryRunner = new QueryRunner(dataSource);
//執(zhí)行sql語句 處理數(shù)據(jù)
String sql = "select * from account";
List<Account> list = queryRunner .query(sql, new BeanListHandler<>(Account.class));
if(list!=null && list.size()>0){
for (Account account : list) {
System.out.println(account);
}
}
二:注解
1.注意:想要用注解必須先在xml中掃描注解(添加context約束)
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<!--開啟spring掃描約束-->
<context:component-scan base-package="要掃描的包"/>
<context:property-placeholder location="classpath:properties配置文件"/>
property-override: 讀取properties文件
location=下有兩種獲取properties配置文件的方式:
classpath: :在當(dāng)前路徑下查找properties文件
classpath* :除了當(dāng)前文件下找 還去引入的jar包下找 maven高級應(yīng)用
2.@Component:創(chuàng)建實現(xiàn)類對象 英文組件的意思
例:@Component:("accountservice") = <bean id="accountService" class="com.itheima.service.AccountServiceImpl"></bean>
3.Spring為了更好的區(qū)分三層結(jié)構(gòu) @Component注解在三層中還可以換一種注解方式
@Controller : 控制web層類中的注解
@Service : 控制在service層中類的注解
@Repository : 控制在dao層中類的注解
4.Scope注解屬性 :單例,多例 默認數(shù)單例模式 多例需要@Scope("prototype")
5.init-method構(gòu)造執(zhí)行以后執(zhí)行方法 destory-method對象銷毀之前執(zhí)行: 必須用在方法上
@PostConStruct 構(gòu)造執(zhí)行以后執(zhí)行
@PreDestroy 對象銷毀之前執(zhí)行
三:依賴注入的注解(分為String依賴注入和jdk依賴注入,建議使用String依賴注入)
1.@Autowired 英文 自動穿戴的意思 String提供
@Qualifier("關(guān)聯(lián)的id") 不寫關(guān)聯(lián)的id默認是類名的小寫 建議寫上關(guān)聯(lián)的id
@Autowired與 @Qualifier("關(guān)聯(lián)的id")連用實現(xiàn)注解注入 不需要寫setXXX方法
2.@Resource(name =" ") jdk提供
四:junit測試:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml");
AccountService accountService = context.getBean("accountService", AccountService.class);
每次測試都要寫以上代碼 解決這一問題的步驟 1.需要導(dǎo)包2.加入注解,3.讀取配置文件
導(dǎo)包: 需要注意的是要和sping導(dǎo)包的版本是一個版本
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
提取service,加入注解:@RunWith(SpringJUnit4ClassRunner.class)
讀取配置文件:@ContextConfiguration(locations = "classpath:xml配置文件")
@ContextConfiguration(locations = "classpath:xml配置文件") 等同于ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml");
轉(zhuǎn)載于:https://www.cnblogs.com/lgpliuguoping/p/11449040.html
總結(jié)
以上是生活随笔為你收集整理的Spring框架第二天知识总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring框架第一天知识总结
- 下一篇: P1032 字串变换