第一步:添加依赖
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.2.8
com.github.pagehelper
pagehelper-spring-boot-starter
1.4.1
第二步:编写application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#Mapper.xml所在的位置
mapper-locations: classpath*:mapper/*.xml
#Entity扫描的model包
type-aliases-package: com.hc.domain
#显示日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
offset-as-page-num: true
row-bounds-with-count: true
support-methods-arguments: true
reasonable: true
params: count=countSql
第三步:生成代码
实体类:
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {
private static final long serialVersionUID = -1050535219404733415L;
/**
* 部门编号
*/
private Integer deptno;
/**
* 部门名称
*/
private String dname;
/**
* 部门地址
*/
private String loc;
}
Mapper接口
@Mapper
public interface DeptMapper {
@Select("select * from tb_dept where deptno = #{deptno} ")
Dept selectByPrimaryKey(Integer deptno);
@Select("select * from tb_dept")
List selectAll();
}
第四步:测试代码
@SpringBootTest
class DeptMapperTest {
@Resource
private DeptMapper deptMapper;
@Test
void selectByPrimaryKey() {
System.out.println(deptMapper);
final Dept dept = deptMapper.selectByPrimaryKey(10);
System.out.println(dept);
}
@Test
void page(){
PageHelper.startPage(1,2);
List list = deptMapper.selectAll();
list.forEach(System.out::println);
}
}