您的位置:首页 > 编程语言 > Java开发

SpringBoot整合MyBatis

2017-07-18 15:58 417 查看
一、创建数据库spring_boot,并增加表:



 

二、使用Intellij Idea新建一个SpringBoot工程,起始依赖选择mysql、mybatis,其它按需选择

三、在配置文件 application.yml 中增加数据库信息:
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_boot
username: root
password: 320121


四、使用Mybatis Generator生成和数据表对应的实体类和mapper文件

五、将文件复制到相应位置,结构图:



六、在配置文件 application.yml 中增加实体类的包名和mapper.xml文件位置:
mybatis:
type-aliases-package: com.zhqing.entity
mapper-locations: classpath:mapper/*.xml


七、在工程启动类Application中增加注解  @MapperScan("com.zhqing.mapper"), 否则在每个mapper类中都需要增加
@SpringBootApplication
@MapperScan("com.zhqing.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}


八、通过 自动注入的方式调用接口方法
@Autowired
private UserMapper userMapper;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis springboot