Springboot整合mybatis
1.新建SpringBoot
在IntelliJ IDEA(14.1 版本) 新建Spring Initializr项目=>填写项目信息=>选择web
2.整合mybatis
(1) 添加mybatis依赖
<!--mybatis整合springboot jar包依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
(2) 添加mysql数据库驱动
<!-- mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
(3) 在application.properties配置文件中设置
#springboot整合Mybatis框架
#加载Mybatis配置文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#数据源
#方法一:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username= root
spring.datasource.password=root
(4)在resources文件夹下新建mapper,并在该文件夹下,添加mapper映射文件
例如UserMapper.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.hand.hand.mapper.UserMapper">
<select id="findAll" resultType="com.hand.hand.domain.User">
select * from user
</select>
</mapper>
3 .domain,mapper,service,controller可以新建
记得相应的类或者接口,放在入口类的同一级目录,或者同一级目录的子目录下