多平台统一管理软件接口,如何实现多平台统一管理软件接口
277
2023-05-22
SpringBoot集成mybatis实例
一、使用mybatis-spring-boot-starter
1、添加依赖
2、启动时导入指定的sql(application.properties)
spring.datasource.schema=import.sql
3、annotation形式
@SpringBootApplication
@MapperScan("sample.mybatis.mapper")
public class SampleMybatisApplication implements CommandLineRunner {
@Autowired
private CityMapper cityMapper;
public static void main(String[] args) {
SpringApplication.run(SampleMybatisApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(this.cityMapper.findByState("CA"));
}
}
4、xml方式
mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
application.properties
spring.datasource.schema=import.sql
mybatis.config=mybatis-config.xml
mapper
@Component
public class CityMapper {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public City selectCityById(long id) {
return this.sqlSessionTemplate.selectOne("selectCityById", id);
}
}
二、手工集成
1、annotation方式
@Configuration
@MapperScan("com.xixicat.modules.dao")
@PropertySources({ @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true), @PropertySource(value = "file:./application.properties", ignoreResourceNotFound = true) })
public class MybatisConfig {
@Value("${name:}")
private String name;
@Value("${database.driverClassName}")
private String driverClass;
@Value("${database.url}")
private String jdbcUrl;
@Value("${database.username}")
private String dbUser;
@Value("${database.password}")
private String dbPwd;
@Value("${pool.minPoolSize}")
private int minPoolSize;
@Value("${pool.maxPoolSize}")
private int maxPoolSize;
@Bean
public Filter characterEncodingFilter() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
@Bean(destroyMethod = "close")
public DataSource dataSource(){
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(driverClass);
hikariConfig.setJdbcUrl(jdbcUrl);
hikariConfig.setUsername(dbUser);
hikariConfig.setPassword(dbPwd);
hikariConfig.setPoolName("springHikariCP");
hikariConfig.setAutoCommit(false);
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
hikariConfig.setMinimumIdle(minPoolSize);
hikariConfig.setMaximumPoolSize(maxPoolSize);
hikariConfig.setConnectionInitSql("SELECT 1");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setFailFast(true);
sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
return sessionFactory.getObject();
}
}
点评
这种方式有点别扭,而且配置不了拦截式事务拦截,只能采用注解声明,有些冗余
2、xml方式
数据源
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/beans http://wwwhttp://.springframework.org/schema/beans/spring-beans-4.0.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd"> class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans
http://wwwhttp://.springframework.org/schema/beans/spring-beans-4.0.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-4.0.xsd">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
aop依赖
mybatis-spring等依赖
指定xml配置文件
@Configuration
@ComponentScan( basePackages = {"com.xixicat"} )
@ImportResource("classpath:applicationContext-mybatis.xml")
@EnableAutoConfiguration
public class AppMain {
// 用于处理编码问题
@Bean
public Filter characterEncodingFilter() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
//文件下载
@Bean
public HttpMessageConverters restFileDownloadSupport() {
ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
return new HttpMessageConverters(arrayHttpMessageConverter);
}
public static void main(String[] args) throws Excehttp://ption {
SpringApplication.run(AppMain.class, args);
}
}
点评
跟传统的方式集成最为直接,而且事务配置也比较容易上手
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~