




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第初學(xué)者,Spring快速入門publicvoidtest(){
ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");
UserServiceuserService=(UserService)context.getBean("userService");
userService.add();
}
3.3.3.使用注解實(shí)現(xiàn)AOP
//注解實(shí)現(xiàn)的增強(qiáng)類
packagecom.wyl.config;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.After;
importorg.aspectj.lang.annotation.Around;
importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Before;
@Aspect
publicclassAnnotationPointcut{
@Before("execution(*com.wyl.service.UserServiceImpl.*(..))")
publicvoidbefore(){
System.out.println("---------方法執(zhí)行前---------");
@After("execution(*com.wyl.service.UserServiceImpl.*(..))")
publicvoidafter(){
System.out.println("---------方法執(zhí)行后---------");
@Around("execution(*com.wyl.service.UserServiceImpl.*(..))")
publicvoidaround(ProceedingJoinPointjp)throwsThrowable{
System.out.println("環(huán)繞前");
System.out.println("簽名:"+jp.getSignature());
//執(zhí)行目標(biāo)方法proceed
Objectproceed=ceed();
System.out.println("環(huán)繞后");
System.out.println(proceed);
}
4.事務(wù)和JdbcTemplate
4.1.JdbcTemplate使用
入門案例:
//Spring自帶的數(shù)據(jù)源
DriverManagerDataSourcedataSource=newDriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springuseSSL=falseallowPublicKeyRetrieval=trueserverTimezone=Asia/Shanghai");
dataSource.setUsername("root");
dataSource.setPassword("root");
JdbcTemplatetemplate=newJdbcTemplate(dataSource);
ListAccountaccountList=template.query("select*fromaccount",newBeanPropertyRowMapper(Account.class));
for(Accountaccount:accountList){
System.out.println(account);
}
具體增刪改查用法:
@Autowired
privateJdbcTemplatejdbcTemplate;
//添加
@Test
publicvoidinsert(){
Stringsql="insertintoaccount(name,money)VALUES(,
Accountaccount1=newAccount();
account1.setName("迪迦");
account1.setMoney(10000F);
jdbcTemplate.update(sql,account1.getName(),account1.getMoney());
find();
//刪除
@Test
publicvoiddelete(){
Stringsql="deletefromaccountwhereid=
jdbcTemplate.update(sql,6);
find();
//更新
@Test
publicvoidupdate(){
ListAccountaccounts=jdbcTemplate.query("select*fromaccountwhereid=",newBeanPropertyRowMapper(Account.class),1);
Accountaccount=accounts.get(0);
account.setName("泰羅");
Stringsql="updateaccountsetname=whereid=
jdbcTemplate.update(sql,account.getName(),account.getId());
find();
//查詢所有
@Test
publicvoidfind(){
ListAccountaccountList=jdbcTemplate.query("select*fromaccount",newBeanPropertyRowMapper(Account.class));
for(Accountaccount:accountList){
System.out.println(account);
//查詢一個(gè)bean
@Override
publicAccountfindByName(Stringname){
Stringsql="select*fromaccountwherename=
returnjdbcTemplate.queryForObject(sql,newBeanPropertyRowMapper(Account.class),name);
//查詢一個(gè)Object
@Test
publicvoidfindOne(){
Stringsql="selectcount(id)fromaccount";
Integerinteger=jdbcTemplate.queryForObject(sql,Integer.class);
System.out.println(integer);
}
4.2.Spring配置事務(wù)
在Spring中有兩種方法管理事務(wù):聲明式事務(wù)管理和編程式事務(wù)管理;
聲明式事務(wù)管理:
1、xml配置文件式:
1、配置事務(wù)管理器
beanid="transactionManager"
propertyname="dataSource"ref="dataSource"http://bean
2、配置通知
!--配置事務(wù)的通知/增強(qiáng)--
tx:adviceid="interceptor"
!--配置事務(wù)的屬性--
!--isolation:事務(wù)隔離級別,默認(rèn)使用數(shù)據(jù)庫的隔離級別
no-rollback-for:指定一個(gè)異常,除了該異常都回滾。
propagation:事務(wù)傳播行為,默認(rèn)是required一定有事務(wù),增刪改設(shè)置required,查詢設(shè)置supports
read-only:是否只讀。只有查詢才能設(shè)置true。默認(rèn)是false支持讀寫。
rollback-for:指定一個(gè)異常,出現(xiàn)該異常就回滾,其他異常不回滾。
timeout:事務(wù)超時(shí)時(shí)間,默認(rèn)-1,永不超時(shí)。指定了以秒為單位。--
tx:attributes
!--指定在哪種規(guī)則的方法上添加事務(wù)--
tx:methodname="transfer*"/
/tx:attributes
/tx:advice
3、事務(wù)管理器和切入點(diǎn)表達(dá)式關(guān)聯(lián)起來
aop:config
!--service包下所有類的所有方法都添加事務(wù)--
aop:pointcutid="commonPointcut"expression="execution(*com.sample.service.*.*(..))"/
!--將事務(wù)管理器和切入點(diǎn)表達(dá)式關(guān)聯(lián)起來--
aop:advisoradvice-ref="interceptor"pointcut-ref="commonPointcut"/
/aop:config
2、注解式:
1、配置事務(wù)管理器
beanid="transactionManager"
propertyname="dataSource"ref="dataSource"http://bean
2、開啟對事務(wù)注解的支持
!--開啟對事務(wù)注解的支持--
tx:annotation-driven/
3、在要添加事物的類上添加注解:@Transactional
3、純注解式
@Configuration
@ComponentScan("com.sample")
//相當(dāng)于tx:annotation-driven/
@EnableTransactionManagement
@PropertySource("classpath:perties")
publicclassAppConfig{
@Value("${db.driver}")
privateStringdriver;
@Value("${db.url}")
privateStringurl;
@Value("${db.username}")
privateStringusername;
@Value("${db.password}")
privateStringpassword;
@Bean
publicDruidDataSourcedataSource(){
DruidDataSourcedataSource=newDruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
returndataS
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 苗木栽植施工合同協(xié)議書
- 承運(yùn)車輛解除合同協(xié)議書
- 防火涂料合同協(xié)議書范本
- 鄉(xiāng)鎮(zhèn)醫(yī)院進(jìn)修合同協(xié)議書
- 甲方服務(wù)合同協(xié)議書模板
- 怎么擬定合同協(xié)議書范本
- 紙品公司合同協(xié)議書
- 車輛買賣合同協(xié)議書下載
- 賣房簽合同的協(xié)議書
- 磚廠廢品買賣合同協(xié)議書
- 計(jì)算機(jī)基礎(chǔ)考試知識試題及答案集
- 山東省棗莊市市中學(xué)區(qū)五校聯(lián)考2025屆七年級數(shù)學(xué)第二學(xué)期期末考試模擬試題含解析
- DB31T 1400-2023 藥品生產(chǎn)全過程數(shù)字化追溯體系建設(shè)和運(yùn)行規(guī)范
- 浙江省溫州市2025屆高三下學(xué)期三模政治試題 含解析
- 福建廈門雙十中學(xué)2025屆物理八下期末質(zhì)量跟蹤監(jiān)視試題含解析
- 成人患者營養(yǎng)不良診斷與應(yīng)用指南(2025版)解讀課件
- 十五五時(shí)期經(jīng)濟(jì)社會(huì)發(fā)展座談會(huì)十五五如何謀篇布局
- 遵義市購房合同協(xié)議
- 2024年四川省天全縣事業(yè)單位公開招聘醫(yī)療衛(wèi)生崗筆試題帶答案
- 育兒嫂簽合同協(xié)議
- 【7語期中】合肥市包河區(qū)2024-2025學(xué)年七年級下學(xué)期4月期中語文試題
評論
0/150
提交評論