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();    }}