SpringBoot集成mybatis实例

作者:xixicat 时间:2023-03-09 16:57:01 

一、使用mybatis-spring-boot-starter

1、添加依赖


<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.0.0</version>
</dependency>

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


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <typeAliases>
   <package name="sample.mybatis.domain"/>
 </typeAliases>
 <mappers>
   <mapper resource="sample/mybatis/mapper/CityMapper.xml"/>
 </mappers>
</configuration>

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方式

数据源


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<context:property-placeholder ignore-unresolvable="true" />

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
   <property name="poolName" value="springHikariCP" />
   <property name="connectionTestQuery" value="SELECT 1" />
   <property name="dataSourceClassName" value="${database.dataSourceClassName}" />
   <property name="maximumPoolSize" value="${pool.maxPoolSize}" />
   <property name="idleTimeout" value="${pool.idleTimeout}" />

<property name="dataSourceProperties">
     <props>
       <prop key="url">${database.url}</prop>
       <prop key="user">${database.username}</prop>
       <prop key="password">${database.password}</prop>
     </props>
   </property>
 </bean>

<!-- HikariCP configuration -->
 <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
   <constructor-arg ref="hikariConfig" />
 </bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource"/>
   <!-- 配置mybatis配置文件的位置 -->
   <property name="configLocation" value="classpath:mybatis-config.xml"/>
   <property name="typeAliasesPackage" value="com.xixicat.domain"/>
   <!-- 配置扫描Mapper XML的位置 -->
   <property name="mapperLocations" value="classpath:com/xixicat/modules/dao/*.xml"/>

</bean>

<!-- 配置扫描Mapper接口的包路径 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
   <property name="basePackage" value="com.xixicat.repository.mapper"/>
 </bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg ref="sqlSessionFactory"/>
 </bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource"/>
 <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true" />

<tx:advice id="txAdvice" transaction-manager="transactionManager" >
   <tx:attributes>
     <tx:method name="start*" propagation="REQUIRED"/>
     <tx:method name="submit*" propagation="REQUIRED"/>
     <tx:method name="clear*" propagation="REQUIRED"/>
     <tx:method name="create*" propagation="REQUIRED"/>
     <tx:method name="activate*" propagation="REQUIRED"/>
     <tx:method name="save*" propagation="REQUIRED"/>
     <tx:method name="insert*" propagation="REQUIRED"/>
     <tx:method name="add*" propagation="REQUIRED"/>
     <tx:method name="update*" propagation="REQUIRED"/>
     <tx:method name="delete*" propagation="REQUIRED"/>
     <tx:method name="remove*" propagation="REQUIRED"/>
     <tx:method name="execute*" propagation="REQUIRED"/>
     <tx:method name="del*" propagation="REQUIRED"/>
     <tx:method name="*" read-only="true"/>
   </tx:attributes>
 </tx:advice>
 <aop:config proxy-target-class="true" expose-proxy="true">
   <aop:pointcut id="pt" expression="execution(public * com.xixicat.service.*.*(..))" />
   <aop:advisor order="200" pointcut-ref="pt" advice-ref="txAdvice"/>
 </aop:config>
</beans>

aop依赖


   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
   </dependency>

mybatis-spring等依赖


<!-- boot dependency mybatis -->
   <dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis</artifactId>
     <version>3.3.0</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis-spring</artifactId>
     <version>1.2.2</version>
     <scope>compile</scope>
   </dependency>

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.6</version>
   </dependency>

<!--<dependency>-->
     <!--<groupId>org.hsqldb</groupId>-->
     <!--<artifactId>hsqldb</artifactId>-->
     <!--<scope>runtime</scope>-->
   <!--</dependency>-->

<dependency>
     <groupId>com.zaxxer</groupId>
     <artifactId>HikariCP-java6</artifactId>
     <version>2.3.8</version>
   </dependency>

指定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 Exception {
   SpringApplication.run(AppMain.class, args);
 }

}

点评

跟传统的方式集成最为直接,而且事务配置也比较容易上手

来源:https://segmentfault.com/a/1190000004275305

标签:spring,boot,mybatis
0
投稿

猜你喜欢

  • C#操作session的类实例

    2023-07-17 13:57:19
  • SpringBoot整合之SpringBoot整合MongoDB的详细步骤

    2023-11-25 09:55:37
  • Android滑动拼图验证码控件使用方法详解

    2021-10-03 02:56:06
  • 详解WPF中的隧道路由和冒泡路由事件

    2023-03-01 07:59:49
  • 详解java 三种调用机制(同步、回调、异步)

    2023-11-25 07:59:57
  • 基于C#设计一个双色球选号工具

    2021-10-07 20:39:57
  • Java代理模式实例详解【静态代理与动态代理】

    2023-04-27 08:18:51
  • C语言非递归后序遍历二叉树

    2023-12-13 18:05:45
  • springMVC如何将controller中Model数据传递到jsp页面

    2023-05-25 23:46:58
  • Java之单例模式实现方案详解

    2022-02-15 19:02:29
  • Android Support Palette使用详解

    2023-04-05 15:47:24
  • C# menuStrip控件实现鼠标滑过自动弹出功能

    2022-09-01 05:18:37
  • 浅谈Java工程读取resources中资源文件路径的问题

    2021-07-20 19:13:45
  • Spring MVC返回的json去除根节点名称的方法

    2023-07-15 17:46:55
  • Linux+Docker+SpringBoot+IDEA一键自动化部署的详细步骤

    2023-08-12 15:25:39
  • IDEA2020.2.3中创建JavaWeb工程的完整步骤记录

    2022-03-15 05:16:02
  • 解决idea删除模块后重新创建显示该模块已经被注册的问题

    2023-04-24 18:06:12
  • 详解MyBatis配置typeAliases的方法

    2023-11-29 06:21:52
  • 在C#中 webbrowser的使用心得

    2023-12-10 12:57:00
  • android阅读器长按选择文字功能实现代码

    2023-09-16 08:48:42
  • asp之家 软件编程 m.aspxhome.com