spring4注解配置datasource⽅式package fig;
import t.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource boneCPDataSource = (DataSource) Bean("boneCPDataSource");
//UserService userService = Bean(UserService.class);
//List<User> users = userService.queryUserList();
//System.out.println(users);
context.close();
}
}
package fig;
import com.jolbox.bonecp.BoneCPDataSource;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import t.annotation.PropertySource;
//配置注解
@Configuration
/
/扫描
@ComponentScan(basePackages = "fig")
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound=true)
public class SpringConfig {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
//@Bean  //相当于xml配置⽂件的bean注解初始化userDao
//public UserDAO getUserDao(){
//    return new UserDAO();//返回对象
//}
//bean默认id为⽅法名
@Bean(destroyMethod="close")
public BoneCPDataSource boneCPDataSource(){
//配置数据库连接池对象
BoneCPDataSource boneCPDataSource=new BoneCPDataSource();
boneCPDataSource.setDriverClass(driverClassName);
boneCPDataSource.setUsername(username);
boneCPDataSource.setPassword(password);
boneCPDataSource.setJdbcUrl(url);
return boneCPDataSource;
}
}
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
springframework事务
jdbc.password=123456
获得数据库连接池对象

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。