spring-boot-mybatis-demo

简要说明

spring-boot建议orm采用JPA,因为jpa符合spring-boot简化配置理念。但是最近在使用jpa的过程中各种不爽,特别是多表联合查询的时候,也可能是我对jpa还没深入。由于个人经历,mybatis使用较多,现整合spring-boot和mybatis。
按照约定,其实spring-boot整合mybatis非常简单,只需要加一个mybatis.mapper-locations配置项标明mybatis的xml映射文件在哪,mybatis接口加上@Mapper注解告诉spring的bean工厂,这是mybatis的mapper接口即可。

引入依赖包

pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql-connector.version}</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-spring-boot.version}</version>
</dependency

添加配置

application.properties

mybatis.mapper-locations=classpath:mapper/*.xml

该配置指明mybatis的xml映射文件位置。

添加mapper注解

@Mapper

@Mapper
public interface CityBeanMapper {
    long countByExample(CityBeanExample example);

    int deleteByExample(CityBeanExample example);

    int deleteByPrimaryKey(Short cityId);

    int insert(CityBean record);

    int insertSelective(CityBean record);

    List<CityBean> selectByExample(CityBeanExample example);

    CityBean selectByPrimaryKey(Short cityId);

    int updateByExampleSelective(@Param("record") CityBean record, @Param("example") CityBeanExample example);

    int updateByExample(@Param("record") CityBean record, @Param("example") CityBeanExample example);

    int updateByPrimaryKeySelective(CityBean record);

    int updateByPrimaryKey(CityBean record);
}

mybatis接口层添加@Mapper注解,告诉spring的上下文管理器,自动把该接口实例化,并纳入spring上下文管理器中,以后使用的时候便可自动注入到需要的类中。

Server层使用

@Service(value = "cityService")
public class CityServiceImpl implements CityService {

  @Autowired
  private CityBeanMapper cityBeanMapper;

  @Transactional
  public Boolean save(CityBean cityBean) {
    if (null == cityBean.getCityId()) {
      return cityBeanMapper.insert(cityBean)>0?true:false;
    }else{
      return cityBeanMapper.updateByPrimaryKey(cityBean)>0?true:false;
    }
  }

  public CityBean findById(Short id) {
    return cityBeanMapper.selectByPrimaryKey(i);
  }
}

自动生成

使用mybatis的时候很多时候都是通过mybatis-generator自动生成mapper.xml、接口、model。示例工程中已经加入了相关的配置,可参考。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <properties resource="application.properties"/>

  <classPathEntry location="D:\\Java\\maven\\repository\\mysql\\mysql-connector-java\\5.1.39\\mysql-connector-java-5.1.39.jar" />

  <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">

    <commentGenerator>
      <!--  关闭自动生成的注释  -->
      <property name="suppressAllComments" value="true" />
    </commentGenerator>

    <jdbcConnection driverClass="${spring.datasource.driverClassName}"
      connectionURL="${spring.datasource.url}"
      userId="${spring.datasource.username}"
      password="${spring.datasource.password}">
    </jdbcConnection>

    <javaModelGenerator targetPackage="com.itclj.model" targetProject="src/main/java"/>

    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

    <javaClientGenerator targetPackage="com.itclj.dao" targetProject="src/main/java" type="XMLMAPPER"/>

    <table schema="sakila" tableName="city" domainObjectName="CityBean"></table>

  </context>
</generatorConfiguration>

工程地址:https://github.com/clj198606061111/spring-boot-mybatis-demo
原文地址:http://www.itclj.com/blog/58c62d9f47508f786718d4f5

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值