https://github.com/mybatis
starter(场景启动器)
SpringBoot官方的Starter:spring-boot-starter-*
第三方的: *-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
@EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置项绑定类。
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}
@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
注意:mybatis全局配置文件中的所有配置都可以在springboot配置文件中进行设置。当要在springboot配置文件中进行设置时需要关闭关于mybatis全局配置文件的位置指定,并且要注释掉在mybatis全局配置文件中进行的设置。
如下所示:想要开启mybatis对驼峰命名的支持有两种方式:
方式一:
在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息
# 配置mybatis规则
# config-location:指定全局配置文件位置
# mapper-locations:指定mapper配置文件位置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
方式二:
关闭mybatis全局配置文件中的配置
在springboot配置文件application.yaml中配置关于驼峰命名的配置:
import com.study.admin.bean.Account;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AccountMapper {
public Account getAcct(long id);
}
<mapper namespace="com.study.admin.mapper.AccountMapper">
<select id="getAcct" resultType="com.study.admin.bean.Account">
select * from account_tb1 where id=#{id}
select>
mapper>
导入依赖:
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
@Data
public class City {
private long id;
private String name;
private String state;
private String country;
}
import com.study.admin.bean.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CityMapper {
@Select("select * from city where id=#{id}")
public City getCity(long id);
}
import com.study.admin.bean.City;
import com.study.admin.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CityService {
@Autowired(required = false)
CityMapper cityMapper;
public City getCity(long id){
return cityMapper.getCity(id);
}
}
import com.study.admin.bean.City;
import com.study.admin.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CityController {
@Autowired
CityService cityService;
@ResponseBody
@GetMapping("/getCity")
public City getCity(@RequestParam("id") long id){
return cityService.getCity(id);
}
}
引入依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>