导入mybatis依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-Java</artifactId> <version>5.1.3</version> </dependency>
mybatis-spring-boot-starter引入了
mybatis
mybatis-spring
mybatis-spring-boot-autoconfiguration
spring-boot-starter-jdbc
步骤:
1
)、配置数据源相关属性
2
)、给数据库建表
3
)、创建
JavaBean
注解版
//指定这是一个操作数据库的mapper
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")//使
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
MyBatis @Options使用方法
配合查询语句使用,主要是开关一些查询的选项。比如useCache = true表示将会缓存本次查询结果,以提高下次查询速度;flushCache = Options.