In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to integrate Mybatis and Druid in Springboot. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Implement the integration of Mybatis and Druid in Springboot. Spring-boot-starter-parent is used as follows:
Org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE
1. Introduce needed dependencies
Com.alibaba druid 1.1.8 org.mybatis mybatis-spring 1.3.2 org.mybatis mybatis 3.4.6 org.springframework spring-jdbc com.github.pagehelper pagehelper 5.1.4
2Perfect data source information in application.yml
Spring: datasource: master: driverClassName: oracle.jdbc.OracleDriver url: jdbc:oracle:thin:@192.168.30.150:1521:orcl username: ioss_sqm password: ioss_sqm
3. Create an object DruidPropConfig that encapsulates the configuration information of the data source
Public class DruidPropConfig {private String url; private String driverClassName; private String username;}
4. Create a Condition object to determine whether a data source needs to be created
Public class MasterDataSourceCondition implements Condition {@ Override public boolean matches (ConditionContext context, AnnotatedTypeMetadata arg1) {if (! context.getEnvironment (). ContainsProperty ("spring.datasource.master.url")) {return false;} if (! context.getEnvironment (). ContainsProperty ("spring.datasource.master.driverClassName")) {return false } if (! context.getEnvironment () .containsProperty ("spring.datasource.master.username")) {return false;} if (! context.getEnvironment () .containsProperty ("spring.datasource.master.password")) {return false;} String url = context.getEnvironment () .getProperty ("spring.datasource.master.url") String driverClassName = context.getEnvironment () .getProperty ("spring.datasource.master.driverClassName"); String username = context.getEnvironment () .getProperty ("spring.datasource.master.username"); String password = context.getEnvironment () .getProperty ("spring.datasource.master.password") If (StringUtils.isNotBlank (url) & & StringUtils.isNotBlank (driverClassName) & & StringUtils.isNotBlank (username) & & StringUtils.isNotBlank (password)) {return true;} return false;}}
5. Configure the data source
@ Configuration @ EnableTransactionManagement / / enables annotated transaction management, which is equivalent to @ Conditional (MasterDataSourceCondition.class) @ MapperScan (basePackages = "com.ultrapower.ioss.cmnet.linksync.mapper", sqlSessionTemplateRef= "sqlSessionTemplate") public class MasterDataSourceCfg {private Logger log = LoggerFactory.getLogger (MasterDataSourceCfg.class); public PageInterceptor pageHelper () {PageInterceptor pageHelper = new PageInterceptor (); Properties properties = new Properties (); properties.setProperty ("offsetAsPageNum", "true") Properties.setProperty ("rowBoundsWithCount", "true"); properties.setProperty ("offsetAsPageNum", "true"); properties.setProperty ("pageSizeZero", "true"); properties.setProperty ("reasonable", "true"); properties.setProperty ("autoRuntimeDialect", "true"); / / properties.setProperty ("params", "pageNum=pageNum;pageSize=pageSize"); pageHelper.setProperties (properties); return pageHelper } @ Primary @ Bean (name = "datasourceConfig") @ ConfigurationProperties (prefix= "spring.datasource.master") public DruidPropConfig datasourceConfig () {return new DruidPropConfig ();} / * * main database data source * * / @ Primary @ Bean (name = "dataSource") public DataSource dataSource () throws Exception {DruidPropConfig config = datasourceConfig () If (config = = null | | config.getDriverClassName () = = null) {throw new Exception ("slave datasource data source needs to be configured." + config.toString ());} / * DruidDataSource dataSource = new DruidDataSource (); dataSource.setDriverClassName (config.getDriverClassName ()); dataSource.setUrl (config.getUrl ()); dataSource.setUsername (config.getUsername ()); dataSource.setPassword (config.getPassword ()) DataSource.setConnectionErrorRetryAttempts (5); return dataSource; * / Map properties = new HashMap (); properties.put (DruidDataSourceFactory.PROP_DRIVERCLASSNAME, config.getDriverClassName ()); properties.put (DruidDataSourceFactory.PROP_URL, config.getUrl ()); properties.put (DruidDataSourceFactory.PROP_USERNAME, config.getUsername ()); properties.put (DruidDataSourceFactory.PROP_PASSWORD, config.getPassword ()) / / add statistics, SQL injection, log filter properties.put (DruidDataSourceFactory.PROP_FILTERS, "stat,wall,log4j2"); / / sql merge, slow query is defined as 5s properties.put (DruidDataSourceFactory.PROP_CONNECTIONPROPERTIES, "druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000"); return DruidDataSourceFactory.createDataSource (properties) } @ Primary @ Bean (name = "sqlSessionFactory") @ ConditionalOnBean (name = "dataSource") / / an Bean public SqlSessionFactory sqlSessionFactoryBean (@ Qualifier ("dataSource") DataSource dataSource) {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); / / bean.setObjectWrapperFactory (new MapWrapperFactory ()); bean.setDataSource (dataSource) is instantiated only if an object exists in the current context. / / bean.setPlugins (new Interceptor [] {/ / pageHelper (), sqlPrintInterceptor ()}); bean.setPlugins (new Interceptor [] {pageHelper ()}); / / add XML directory ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); try {List resources = new ArrayList () Resources.addAll (Arrays.asList (resolver.getResources ("classpath*:com/ultrapower/ioss/**/mapper/**/*.xml"); resources.addAll (Arrays.asList (resolver.getResources ("classpath:mapper/**/*.xml"); bean.setMapperLocations (resources.toArray (new Resource [resources.size ()])) / / set mybatis configuration scan path / / bean.setConfigLocation (resolver.getResource ("classpath:mybatis-config.xml")); return bean.getObject ();} catch (Exception e) {e.printStackTrace (); throw new RuntimeException (e) } @ Primary @ Bean ("sqlSessionTemplate") @ ConditionalOnBean (name = "sqlSessionFactory") public SqlSessionTemplate sqlSessionTemplate (@ Qualifier ("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate (sqlSessionFactory);} @ Primary @ ConditionalOnBean (name = "dataSource") @ Bean (name = "transactionManager") public PlatformTransactionManager transactionManager (@ Qualifier ("dataSource") DataSource dataSource) {DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager (dataSource); return dataSourceTransactionManager;}}
6. Configure Durid monitoring
@ Configurationpublic class DruidConfiguration {@ Bean public ServletRegistrationBean druidServlet () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (); servletRegistrationBean.setServlet (new StatViewServlet ()); servletRegistrationBean.addUrlMappings ("/ druid/*"); / / IP whitelist / / servletRegistrationBean.addInitParameter ("allow", "192.168.2.25127.0.0.1") / / IP blacklist (deny takes precedence over allow when co-existing) / / servletRegistrationBean.addInitParameter ("deny", "192.168.1.100"); / / console management user servletRegistrationBean.addInitParameter ("loginUsername", "admin"); servletRegistrationBean.addInitParameter ("loginPassword", "admin") / / whether the data can be reset to disable the "Reset All" function on the HTML page servletRegistrationBean.addInitParameter ("resetEnable", "false"); return servletRegistrationBean;} @ Bean public FilterRegistrationBean filterRegistrationBean () {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean (); filterRegistrationBean.setFilter (new WebStatFilter ()); filterRegistrationBean.addUrlPatterns ("/ *") FilterRegistrationBean.addInitParameter ("exclusions", "* .js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean }} this is the end of the article on "how to integrate Mybatis and Druid in Springboot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.