第一步:导入依赖
关键依赖就mybatis-spring-boot-starter这一个,其他的作为辅助测试用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
第二步:配置yml
server:
port: 8088
spring:
application:
name: your-service-name
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB:your database}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: ${MYSQL-USER:root}
password: ${MYSQL-PWD:root}
mybatis:
mapper-locations: classpath:mapper/*.xml #对应MenuConfigMapper.xml
type-aliases-package: com.nico.mybatis.entity #对应resultType="CastMenuConfig"
注意:mybatis的配置少了任何一个都 无法启动,因为你得告诉他这个MenuConfigMapper.xml这种文件去哪里找,resultType="CastMenuConfig"对应去哪里找
第三步:配置启动类
要加这个包扫描路径@MapperScan(basePackages = "com.nico.mybatis.mapper")
@MapperScan(basePackages = "com.nico.mybatis.mapper")
@SpringBootApplication
public class MyMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MyMybatisApplication.class,args);
}
}
如果不想在这里加也可以,换个地方在接口类CastMenuConfigMapper 上加@Mapper也一样的效果,注意包路径org.apache.ibatis.annotations.Mapper和继承的Mapper路径tk.mybatis.mapper.common.Mapper他们是不一样的
@org.apache.ibatis.annotations.Mapper
public interface CastMenuConfigMapper extends Mapper<CastMenuConfig> {
CastMenuConfig findById(String id);
}
第四步:准备测试
等等!看看你的实体类好了没有,数据库连起来没有,好的先看实体类,这里避免太长只贴部分字段 get set方法省略了,最好重写个toString方法,方便控制台打印查看
public class CastMenuConfig implements Serializable {
private Long id; //主键
private String menuName; //菜单名称
private int menuType; //菜单类型
private int menuSort; //菜单顺序
//get set此处省略。。。
@Override
public String toString() {
return "CastMenuConfig{" +
"id=" + id +
", menuName='" + menuName + '\'' +
", menuType=" + menuType +
", menuSort=" + menuSort +
'}';
}
}
看一下数据库

好的,再把CastMenuConfigMapper.xml 文件配置一下,namespace的包路径为上面CastMenuConfigMapper 接口的包路径,resultType="CastMenuConfig"是通过yml中这个配置的包路径 type-aliases-package: com.nico.mybatis.entity找到对应实体类CastMenuConfig的,还有接口方法名findById和 id="findById"是对应一致的,好了不再疑惑,继续看
<?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.nico.mybatis.mapper.CastMenuConfigMapper">
<select id="findById" parameterType="String" resultType="CastMenuConfig">
select * from cast_menu_config where id=#{id}
</select>
</mapper>
再看看项目文件结构,test里面的内容是创建测试类生成的,当然你可以创建controller和业务层service代码,通过postman或浏览器发起访问完成整个调用逻辑,但是这里通过测试来快速访问。

第五步:进入测试
先创建一个测试类


点击ok后就生成了测试类,由上面的文件结构可以看到在test目录下的文件CastMenuConfigMapperTest
@SpringBootTest
class CastMenuConfigMapperTest {
@Autowired
private CastMenuConfigMapper castMenuConfigMapper;
@Test
void findById() {
CastMenuConfig CastMenuConfig = castMenuConfigMapper.findById("1322078827776266241");
System.out.println("查询结果:"+CastMenuConfig);
}
}
写好参数,好了,点击测试

发现测试通过且返回结果,但是返回的值怎么都是null呢

由于数据库字段是这样的manu_name,实体类是manuName的驼峰命名规则,所以无法映射匹配上,所以需要在yml的mybatis下配置一个规则 map-underscore-to-camel-case: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.nico.mybatis.entity
configuration:
map-underscore-to-camel-case: true
配置好后再次测试就有值了

第六步:如何配置mybatis打印sql日志
首先要配置yml文件mybatis配置项 log-impl: org.apache.ibatis.logging.stdout.StdOutImp
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.nico.mybatis.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql日志到控制台
map-underscore-to-camel-case: true
再次测试查看就有sql日志输出了

但是这样配置你是看不出日志是哪个服务输出的,无法定位,所以还可以使用另一种方式,同样配置yml中logging 用com.nico.mybatis.mapper包名作为key,输出debug级别的sql日志
logging:
level:
com:
nico:
mybatis:
mapper: debug
这样再次测试,就能看到是哪个接口mapper,哪个方法输出的sql

配置时发现有这样一个配置 base-packages: com.nico.mybatis.mapper 跟启动类上的配置一样,试试能否替代启动类上的注解,所以都注释掉@Mapper注解和@MapperScan(basePackages = "com.nico.mybatis.mapper") 后测试
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.nico.mybatis.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
base-packages: com.nico.mybatis.mapper #启动会报错,并没有生效
报错,找不到这个CastMenuConfigMapper文件

所以这个配置不能这么用
第七步:使用注解
@Mapper
public interface CastMenuConfigMapper {
CastMenuConfig findById(String id);
@Select("select * from cast_menu_config")
List<CastMenuConfig> findAll();
}
同上,生成测试方法进行测试
@SpringBootTest
class CastMenuConfigMapperTest {
@Autowired
private CastMenuConfigMapper castMenuConfigMapper;
@Test
void findById() {
CastMenuConfig castMenuConfig = castMenuConfigMapper.findById("1322078827776266241");
System.out.println("查询结果:"+castMenuConfig);
}
@Test
void findAll() {
List<CastMenuConfig> castMenuConfigList = castMenuConfigMapper.findAll();
castMenuConfigList.forEach(item->System.out.println("查询结果:"+item));
}
}
遍历返回查询结果

第八步:使用pagehelper分页助手
导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
编写测试方法
@Test
void findPage() {
PageHelper.startPage(1,2);//获取第1页 每页2条记录
List<CastMenuConfig> castMenuConfigList = castMenuConfigMapper.findAll();
PageInfo<CastMenuConfig> castMenuConfigPageInfo = new PageInfo<>(castMenuConfigList);
castMenuConfigPageInfo.getList().forEach(item->System.out.println("查询结果:"+item));
}
查看查询结果,打印输出了分页sql语句

总结:注解方式就不需要扫描xml文件了,还有@update、@delete、@insert等注解支持,其他配置可参考官方文档 https://mybatis.org/mybatis-3/zh/getting-started.html
网友评论