启动入口类控制台输出
class com.mchange.v2.c3p0.ComboPooledDataSource
com.mchange.v2.c3p0.impl.NewProxyConnection@64d43929
1.在pom.xml 中引入c3p0 的依赖
2.在application.properties 中配置信息,也可以写一个配置文件单独封装这些信息

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=1234
3.配置类
package com.jas.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
@SpringBootConfiguration
public class DatasourceConfiguration {
/**
* environment 用于读取application.properties 配置文件中的配置信息
*/
@Autowired
private Environment environment;
@Bean
public DataSource createDataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(environment.getProperty("spring.datasource.driver-class-name"));
dataSource.setJdbcUrl(environment.getProperty("spring.datasource.url"));
dataSource.setUser(environment.getProperty("spring.datasource.username"));
dataSource.setPassword(environment.getProperty("spring.datasource.password"));
return dataSource;
}
}
在Spring Boot 中使用事务,需要在入口类中使用@EnableTransactionManagement 注解启用事务。
对需要进行事务管理的方法加上@Transactional 注解。
PS:需要注意的是 @Transactional 注解默认只会对运行时异常(RuntimeException)起作用,对于I/O 等其他异常不起作用。对于其他方式的异常,Spring Boot 在@Transactional 注解中提供了rollbackFor 与noRollbackFor 选项,可以让我们自定义对异常的回滚。
/**
* rollbackFor 设置回滚的异常,Exception 可以回滚所有的异常,
* noRollbackFor 设置不回滚事务的异常
*/
@Transactional(rollbackFor = Exception.class, noRollbackFor = NullPointerException.class )
另一个地方需要注意的是:在同一个类中一个没有设置注解的方法调用有注解的方法时,事务不生效。spring4 事务
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-78821-2.html
我没看懂MV前面写他粉丝怎么怎么欢迎
看啦喜欢bb的声音太温暖啦