1.在pom.xnl中引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
2.在maven的resources目录下的建立application.properties文件
spring.datasource.url=jdbc:mysql:///你本地的数据库名 或者是jdbc:mysql://localhost:3306/你本地的数据库名
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.然后使用的bean层就自定义表的bean 如
public class Person {
private Long id;
private String name;
private Integer age;
对应的setget方法....
}
4.定义mapper层 如 此时我没加@Mapper注解 那么我在启动类的时候要加上@MapperScan注解 定义对应的mapper包 扫取其对应的包
public interface PersonMapper {
@Insert("INSERT INTO person(name,age) VALUES(#{name},#{age})")
int insert(@Param("name")String name,@Param("age")Integer age);
@Select("SELECT * FROM person where id = #{id}")
Person selectPersonById(Long id);
@Select("SELECT * FROM person")
List<Person> selectPersons();
}
5.service类,controller类跟springmvc差不多,@Autowired @RequestMapping @Controller就差不多
6.启动类
注意 1.这个启动类必须在你的controller,service,mapper包的父类包下
2.MapperScan则是扫你对应的mapper下面的包
@SpringBootApplication
@MapperScan("xxx.mapper")
public class SpringBootMybatisApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApp.class, args);
}
}