springboot整合mybatis

来源:互联网 发布:mac重装系统进度条不动 编辑:程序博客网 时间:2024/06/15 22:16

1. pom.xml添加依赖

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

2. 在application.properties添加配置,需要扫面的实体包,xml文件所谓位置,mybatis的位置

#------------------------------------mybatis--------------------------------mybatis.type-aliases-package=com.caiwufei.entitymybatis.mapper-locations=classpath:/mybatis/mappers/**/*.xmlmybatis.config-location=classpath:/config/mybatis-config.xml

3 在配置类或者启动类添加map对应接口需要扫描的包

@MapperScan(basePackages= {"com.caiwufei.module.*.dao"})

4 接口

package com.caiwufei.module.employees.dao;import java.util.List;import com.caiwufei.entity.base.BaseMapper;import com.caiwufei.entity.employees.Department;public interface DepartmentDao extends BaseMapper<Department>{List<Department> selectAll();}

5 mapper的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.caiwufei.module.employees.dao.DepartmentDao"><select id="selectAll" resultType="Department"> select * from departments;</select></mapper>