• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解SpringBoot 快速整合MyBatis(去XML化)

springboot 搞代码 4年前 (2022-01-05) 25次浏览 已收录 0个评论

本篇文章主要介绍了详解SpringBoot 快速整合MyBatis(去XML化),非常具有实用价值,需要的朋友可以参考下

序言:

此前,我们主要通过XML来书写SQL和填补对象映射关系。在SpringBoot中我们可以通过注解来快速编写SQL并实现数据访问。(仅需配置:mybatis.configuration.map-underscore-to-camel-case=true)。为了方便大家,本案例提供较完整的层次逻辑SpringBoot+MyBatis+Annotation。

具体步骤

1. 引入依赖

在pom.xml 引入ORM框架(Mybaits-Starter)和数据库驱动(MySQL-Conn)的依赖。

   <!--继承信息 --> org.springframework.bootspring-boot-starter-parent2.0.0.M4<!--依赖管理 -->  <!--添加Web依赖 -->org.springframework.bootspring-boot-starter-web <!--添加Mybatis依赖 -->org.mybatis.spring.bootmybatis-spring-boot-starter1.3.1<!--添加MySQL驱动依赖 -->mysqlmysql-connector-javaruntime<!--添加Test依赖 -->org.springframework.bootspring-boot-starter-testtest

2. 添加数据源

在application.yml 添加数据源,以及开启Mybaits的驼峰映射功能。

 spring: datasource: url: jdbc:mysql://localhost:3306/socks?useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: configuration: map-underscore-to-camel-case: true #开启驼峰映射 

3. 编写数据层代码

 // POJO类如下: public class User { private String userId; private String username; private String password; // Getters & Setters .. } // 数据层代码如下: public interface UserMapper { @Select("select * from t_user where 1=1") List list(); @Select("select * from t_user where username like #{username}") List findByUsername(String username); @Select("select * from t_user where user_id like #{userId}") User getOne(String userId); @Delete("delete from t_user where user_id like #{userId}") int delete(String userId); } 

4. 添加数据库记录

在Navicat 连接本地数据库,随便打开查询窗口,复制下面这段脚本,点击执行即可。

 DROP DATABASE IF EXISTS `socks`; CREATE DATABASE `socks`; USE<mark style="color:transparent">来源gaodaimacom搞#^代%!码&网</mark> `SOCKS`; DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `USER_ID` varchar(50) , `USERNAME` varchar(50) , `PASSWORD` varchar(50) ) ; INSERT INTO `t_user` VALUES ('1', 'admin', 'admin'); INSERT INTO `t_user` VALUES ('2', 'yizhiwazi', '123456'); 

5. 启动项目

 @SpringBootApplication @MapperScan("com.hehe.mapper") //扫描Mapper接口 public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } } 

6. 单元测试

 import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest public class MybatisApplicationTest { @SuppressWarnings("all") @Autowired UserMapper userMapper; @Test public void test_db() { //开始进行测试 assertThat(userMapper.list().size()).isGreaterThan(1); assertThat(userMapper.getOne("1")).isNotEqualTo(null); assertThat(userMapper.getOne("xxx")).isEqualTo(null); } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是详解SpringBoot 快速整合MyBatis(去XML化)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解SpringBoot 快速整合MyBatis(去XML化)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址