本文讲解如何在springboot中集成mybatis (xml方式)

spring Boot 支持两种方式集成mybatis,一种是使用注解,另一种是使用xml,两种方式各有优劣,注解方式简洁方便,不需要xml,xml方式可以直观的看到sql,方便sql优化

集成mybatis:

  • 新建一个springboot项目
  • pom.xml中引入mybatis和mysql的依赖
  • 配置文件配置数据库链接属性
pom.xml引入mybatis,mysql依赖

pom.xml中的mybatis,mysql依赖可以在创建项目的时候勾选对应的依赖项

如果在构建项目是没勾选对应的依赖,可以在建完项目后,手动添加依赖,保证pom.xml有mybatis,mysql依赖

pom.xml对应的依赖

<dependencies>
    <!-- web -->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mybatis -->
    <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.2</version>
    </dependency>
    <!-- mysql -->
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- junit -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
     </dependency>
</dependencies>
application.yml 配置数据库属性,mybatis配置

application.yml 配置如下:

spring:
  #mysql配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wtx?useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root

mybatis:
  #mybatis xml文件文件地址
  mapper-locations: classpath:mapper/*.xml
  #批量扫描别名包地址
  type-aliases-package: com.my.example.domain
表结构
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(32) DEFAULT NULL,
  `pass_word` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

域对象 user

/**
 * @auther: chenmingyu
 * @date: 2018/11/4 13:39
 * @description:
 */
@Data
public class User {

    private Long id;
    private String userName;
    private String password;
}

xml方式集成mybatis

  • 配置文件中配置mybatis的sql的xml文件的路径
  • 入口类上使用注解@MapperScan指定扫描dao层接口

SpringBootApplication 入口类

@SpringBootApplication
@MapperScan("com.my.example.dao")
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }
}

dao层接口

/**
 * @auther: chenmingyu
 * @date: 2018/11/4 13:42
 * @description:
 */
public interface UserMapper {

    User findUserByName(@Param("id") Long id) throws Exception;
}

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.my.example.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.my.example.domain.User" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="pass_word" property="passWord" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_name, pass_word
  </sql>
  <select id="findUserByName" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=BIGINT}
  </select>
</mapper>
使用junit测试mapper
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testFindByName() throws Exception {
        User user = userMapper.findUserByName(5L);
        System.out.println("第一条记录:"+user.toString());
    }
}
测试结果