spring-application.xml
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比東山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式東山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋東山地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
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 實現(xiàn)了BeanDefinitionRegistryPostProcessor接口,因此該bean必須static修飾。否則啟動加載的順序會出現(xiàn)錯誤,如: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數(shù)據(jù)庫連接配置
@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注解管理事物,相當(dāng)于
測試
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() + "]");
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。