springboot中集成mybatis使用mapper.xml文件
- 主程序中要配置@MapperScan(“扫描的包名”)
@MapperScan("com.guigu.springboot.mapper")
@SpringBootApplication
public class Springboot06DataMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot06DataMybatisApplication.class, args);
}
}
- controller层用@Autowired注解进行自动注入
@Autowired
TeacherMapper mapper;
- springboot全局配置文件,配置了mapper.xml文件和mybatis配置文件的路径才会将这些属性和properties绑定
mybatis:
config-location: classpath:mybatis-config.xml
check-config-location: true
mapper-locations: classpath:/mappers/*.xml
前面这几部分都必须要有.
如果想要设置驼峰式命名,在mybatis的全局配置文件中这样设置.
它其他的设置参数可以参考github上发布的文档
settings配置参数文档
<?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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>