- 2023-10-22 14:10:07
- 6966 热度
- 0 评论
上一篇我们介绍了如何在Spring Boot中整合我们国人最常用的MyBatis来实现对关系型数据库的访问。但是上一篇中使用了注解方式来实现,而对于很多MyBatis老用户还是习惯于XML的开发方式,所以这篇,我们就来看看如何使用XML的方式来进行开发。
动手试试
本篇将不具体介绍整合MyBatis的基础内容,读者可以阅读上一篇:Spring Boot 2.x基础教程:使用MyBatis访问MySQL来了解该部分内容。
下面的实操部分将基于上一篇的例子之后进行,基础工程可通过文末仓库中的chapter3-5
目录获取。
第一步:在应用主类中增加mapper的扫描包配置:
@MapperScan("com.didispace.chapter36.mapper") |
第二步:在第一步中指定的Mapper包下创建User表的Mapper定义:
public interface UserMapper { |
第三步:在配置文件中通过mybatis.mapper-locations
参数指定xml配置的位置:
mybatis.mapper-locations=classpath:mapper/*.xml |
第四步:在第三步中指定的xml配置目录下创建User表的mapper配置:
<?xml version="1.0" encoding="UTF-8" ?> |
到这里从注解方式的MyBatis使用方式就改为了XML的配置方式了,为了验证是否运行正常,可以通过下面的单元测试来尝试对数据库的写和读操作:
@Slf4j |
如果您在尝试没有成功,建议通过文末仓库查看完成代码,对比是否有所遗漏与疏忽。
更多本系列免费教程连载「点击进入汇总目录」
代码示例
本文的相关例子可以查看下面仓库中的chapter3-6
目录:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
如果您觉得本文不错,欢迎Star
支持,您的关注是我坚持的动力!
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- discuz(5)