springboot学习笔记四
来源:互联网 发布:windows系统和mac系统 编辑:程序博客网 时间:2024/06/05 06:25
jdbc与事务:
1 pom文件加入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>
2 application.properties中加入数据源配置
#配置数据源spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.username=rootspring.datasource.password=root
3 启动文件加入@EnableTransactionManagement // 启动事务
4 需要开启事务的类加上注解@Transactional
测试代码:
package da.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication@EnableTransactionManagement // 启动事务public class Springboot03jdbcApplication { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(Springboot03jdbcApplication.class, args); // DataSource source = context.getBean(DataSource.class); // Connection connection = source.getConnection(); // System.out.println(connection.getCatalog()); // connection.close(); // context.getBean(StudentDao.class).addStudent("asdasd", 44); context.getBean(StudentDao.class).addStudentBatch("aaa1", "aaa2", "aaa3"); }}
package da.test;import java.io.IOException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;@Repositorypublic class StudentDao { @Autowired private JdbcTemplate jdbcTemplate; public void addStudent(String name, int age) { String sql = "insert into student values(?,?,?)"; jdbcTemplate.update(sql, null, name, age); } // 事务默认只对运行时异常生效 // rollbackFor = 指明对哪类异常回滚 // noRollbackFor = 指明对哪类异常不回滚 @Transactional(rollbackFor = Exception.class, noRollbackFor = NullPointerException.class) public void addStudentBatch(String... names) throws Exception { for (String name : names) { String sql = "insert into student values(?,?,?)"; jdbcTemplate.update(sql, null, name, 19); if ("".equals("")) { throw new IOException(); } } }}
AOP:
1 加入配置文件
<!-- aop的依赖 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
2 测试类
package da.testaop;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LogAspect { @Before("execution(* da.testaop..*.*(..))") public void log() { System.out.println("before method log done"); } @After("execution(* da.testaop..*.*(..))") public void logAfter(JoinPoint point) { System.out.println("after method log done." + point.getTarget().getClass() + ",args:" + Arrays.asList(point.getArgs()) + ",method:" + point.getSignature().getName()); }}
package da.testaop;import org.springframework.stereotype.Component;@Componentpublic class Dao { public void add(String username, int age) { System.out.println("add [" + username + "," + age + "]"); }}
package da.testaop;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.EnableAspectJAutoProxy;@SpringBootApplication@EnableAspectJAutoProxy // 启用aoppublic class Springboot03jdbcApplication2 { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(Springboot03jdbcApplication2.class, args); System.out.println(context.getBean(Dao.class).getClass()); context.getBean(Dao.class).add("aaa", 19); context.close(); }}
阅读全文
1 0
- springboot学习笔记四
- Springboot学习笔记(四)
- springboot学习笔记(四) JdbcTemplate
- SpringBoot学习笔记(四) 日志系统
- Springboot学习笔记(四)Repository接口
- SpringBoot学习笔记(四):SpringApplication的旅途
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- SpringBoot学习笔记(四):整合thymeleaf遇到的问题
- springboot学习笔记(四)使用querydsl完成条件查询
- SpringBoot学习(四)SpringBoot和WebSocket
- SpringBoot自学笔记(四)
- SpringBoot 学习笔记
- SpringBoot 学习笔记 2
- springboot学习笔记(一)
- SpringBoot学习笔记(一)
- springboot学习笔记
- springboot学习笔记
- springboot学习笔记
- js日常:用js构造一个轮播器的思考
- 简单透彻理解JSONP原理及使用
- Ngrok的外网映射
- JSP的生命周期
- java程序基本框架
- springboot学习笔记四
- D3-小试散点图
- 人工智能包含哪些技术
- Leetcode c语言-4Sum
- 中途取消安装 Microsoft Office profession plus 2010卸载不了怎么办?
- C++ STL-Map映射和Set集合
- GEOMETRIC APPLICATIONS OF BSTS
- 内存分区
- [算法分析与设计] leetcode 每周一题: 667. Beautiful Arrangement II