大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
spring-application.xml
创新互联-专业网站定制、快速模板网站建设、高性价比东山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式东山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖东山地区。费用合理售后完善,十年实体公司更值得信赖。
spring-annotation-componentScan.xml
spring-data.xml
spring-mybatis.xml
spring-transaction.xml
测试
ClassPathXmlApplicationContext context11 = new ClassPathXmlApplicationContext("classpath:spring-application.xml");
UserMapper userMapper = context11.getBean(UserMapper.class);
User user = userMapper.getById(6498);
System.out.println("id = [" + user.getId() + "]");
System.out.println("name = [" + user.getName() + "]");
ApplicationConfig启动入口
@Configuration
@Import({DaoConfig.class, ComponentScanConfig.class})
public class ApplicationConfig {
}
ComponentScanConfig扫描注解bean
@Configuration
@ComponentScan(basePackages = {"com.demo.enity","com.demo.dao","com.demo.mapper","com.demo.service"})
public class ComponentScanConfig {
}
DaoConfig持久层配置
@Configuration
@Import({MybatisConfig.class, TransactionConfig.class})
public class DaoConfig {
}
MybatisConfig ORM配置
@Configuration
@Import(DruidPoolConfig.class)
public class MybatisConfig {
@Autowired
private DataSource dataSource;
@Bean("sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setFailFast(true);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);
return sqlSessionFactoryBean;
}
@Bean
public static MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.demo.mapper");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
注意:
MapperScannerConfigurer 实现了BeanDefinitionRegistryPostProcessor接口,因此该bean必须static修饰。否则启动加载的顺序会出现错误,如:DataSource 为null;
PathMatchingResourcePatternResolver此类可以通配符加载资源;
DruidPoolConfig连接池配置
@Configuration
@Import(DruidJdbcConfig.class)
public class DruidPoolConfig {
private final static Logger LOGGER = LoggerFactory.getLogger(DruidPoolConfig.class);
@Autowired
private DruidJdbcConfig druidJdbcConfig;
@Bean("dataSource")
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(druidJdbcConfig.getUrl());
datasource.setUsername(druidJdbcConfig.getUsername());
datasource.setPassword(druidJdbcConfig.getPassword());
datasource.setDriverClassName(druidJdbcConfig.getDriverClassName());
datasource.setInitialSize(druidJdbcConfig.getInitialSize());
datasource.setMinIdle(druidJdbcConfig.getMinIdle());
datasource.setMaxActive(druidJdbcConfig.getMaxActive());
datasource.setMaxWait(druidJdbcConfig.getMaxWait());
datasource.setTimeBetweenEvictionRunsMillis(druidJdbcConfig.getTimeBetweenEvictionRunsMillis());
datasource.setMinEvictableIdleTimeMillis(druidJdbcConfig.getMinEvictableIdleTimeMillis());
datasource.setValidationQuery(druidJdbcConfig.getValidationQuery());
datasource.setTestWhileIdle(druidJdbcConfig.isTestWhileIdle());
datasource.setTestOnBorrow(druidJdbcConfig.isTestOnBorrow());
datasource.setTestOnReturn(druidJdbcConfig.isTestOnReturn());
datasource.setRemoveAbandoned(druidJdbcConfig.isRemoveAbandoned());
datasource.setRemoveAbandonedTimeout(druidJdbcConfig.getRemoveAbandonedTimeout());
datasource.setLogAbandoned(druidJdbcConfig.isLogAbandoned());
try {
datasource.setFilters(druidJdbcConfig.getFilters());
} catch (SQLException e) {
LOGGER.error("datasource.setFilters occur error.", e);
}
return datasource;
}
}
DruidJdbcConfig数据库连接配置
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DruidJdbcConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${druid.initialSize}")
private int initialSize;
@Value("${druid.minIdle}")
private int minIdle;
@Value("${druid.maxActive}")
private int maxActive;
@Value("${druid.maxWait}")
private int maxWait;
@Value("${druid.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${druid.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${druid.validationQuery}")
private String validationQuery;
@Value("${druid.testWhileIdle}")
private boolean testWhileIdle;
@Value("${druid.testOnBorrow}")
private boolean testOnBorrow;
@Value("${druid.testOnReturn}")
private boolean testOnReturn;
@Value("${druid.removeAbandoned}")
private boolean removeAbandoned;
@Value("${druid.removeAbandonedTimeout}")
private int removeAbandonedTimeout;
@Value("${druid.logAbandoned}")
private boolean logAbandoned;
@Value("${druid.filters}")
private String filters;
@Value("${druid.logSlowSql}")
private boolean logSlowSql;
@Value("${druid.loginUsername}")
private String loginUsername;
@Value("${druid.loginPassword}")
private String loginPassword;
TransactionConfig事物配置
@Configuration
@Import(DruidPoolConfig.class)
@EnableTransactionManagement
public class TransactionConfig {
@Autowired
private DataSource dataSource;
@Bean("transactionManager")
public DataSourceTransactionManager transactionManager(){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
注意:@EnableTransactionManagement注解管理事物,相当于
测试
AnnotationConfigApplicationContext context12 = new AnnotationConfigApplicationContext();
context12.register(ApplicationConfig.class);
context12.refresh();
UserService userService = context12.getBean(UserService.class);
User user = userService.save(6498);
System.out.println("id = [" + user.getId() + "]");
System.out.println("name = [" + user.getName() + "]");
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。