spring 注解驱动-事务管理器

需要导入的依赖

 <dependencies>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
    </dependencies>

使用jdbcTemplate 操作数据库
UserDao 接口


public interface UserDao {

  public void insert();
}

UserDaoImpl实现类


@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insert() {
        String username = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update("insert into BONUS values (?,?,?,?)",username,"b",20,5);
    }
}

UserService

public interface UserService {

    public void insert();
}

UserServiceImpl实现类

@Service
public class UserserviceImpl  implements UserService{
    @Autowired
    private  UserDao userDao;

    @Transactional
    public void insert(){
        userDao.insert();
        System.out.println("插入成功!");
    }

}

jdbcConfig配置类,配置类中有数据源,jdbcTemplate,和事务管理器

@EnableTransactionManagement
/**开启事务管理*/
@PropertySource(value = {"classpath:/jdbc.properties"})
@Configuration
public class JdbcConfig {
    @Value("${jdbc.username}")
    private  String user;
    @Value("${jdbc.password}")
    private  String password;
    @Value("${jdbc.url}")
    private  String jdbcUrl;
    @Value("${jdbc.driver}")
    private  String driverClass;

    @Bean
    public DataSource dataSource()  {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(jdbcUrl);
        try {
            dataSource.setDriverClass(driverClass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return  jdbcTemplate;
    }
    /**配置事务管理器*/
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }

}

jdbc.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=xxx

在这个配置类中引入jdbcConfig的配置


@Configuration
@ComponentScan(value = {"com.aaa.tx"})
@Import(JdbcConfig.class)
public class TxDataSource {
}

测试

public class TxTest {
    public static void main(String[] args) {
        ApplicationContext context=new AnnotationConfigApplicationContext(TxDataSource.class);
        UserService bean = context.getBean(UserService.class);
        bean.insert();
    }
}

注意:@EnableTransactionManagement
/*开启事务管理/
开启事务管理,需要配置事务管理器来管理数据源,
在方法上使用@Transactional
这样才能控制住操作数据库时的一些增删改查操作

你可能感兴趣的:(spring)