1.如何使用注解版Mybatis?

(1)引入mybatis ,druid,Mysql 的依赖,环境搭建可以参考第13篇的内容

        <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

(2)使用@Mapper注解。如果不想使用注解在每个接口上去标记,那么可以在主类上使用@MapperScan(value = "com.springboot.dao") 批量扫描接口

package com.springboot.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import com.springboot.entity.StockCode; @Mapper
public interface Stock { @Select("select * from stock_code where id = #{id}")
public StockCode getStockByID(Integer id); }

(3)这下Mybatis 的整合就完成,直接在controller中测试就行了

package com.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.springboot.dao.Stock;
import com.springboot.dao.StockCodeMapper;
import com.springboot.entity.StockCode; @RestController
public class StockController {
@Autowired
private Stock stockmapper; @GetMapping("/getStock/{id}")
public StockCode getStockByID(@PathVariable("id")Integer id) {
StockCode stock = stockmapper.getStockByID(id);
return stock;
}
}

这时我们会发现下面的情况,实体的属性的值为空,这时因为我们的数据字段单词中间是有下划线的,而实体是没有的,所以导致无法映射。

解决办法是:开启mybatis 的驼峰写法,在全局配置文件中添加一下配置即可

2.配置文件版集成MyBatis

(1)编写Mybatis 全局配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--开启驼峰命名法 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>

(2)编写Mybatis sql映射文件

(3)在全局配置文件声明mybatis 配置文件的位置和sql映射文件的位置

mybatis:
# 指定全局配置文件位置
config-location: classpath:mybatis-config.xml
# 指定sql映射文件位置
mapper-locations: classpath:mapper/*.xml

(4)这样mybatis 已经集成完了,我们可以测试了

package com.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.springboot.dao.Stock;
import com.springboot.dao.StockCodeMapper;
import com.springboot.entity.StockCode; @RestController
public class StockController { @Autowired
StockCodeMapper stockCodeMapper; @GetMapping("/getStock/{id}")
public StockCode getStockByID(@PathVariable("id")Integer id) {
StockCode stock = stockCodeMapper.selectByPrimaryKey(id);
return stock;
}
}

最新文章

  1. SQL Server自动化运维系列——关于数据收集(多服务器数据收集和性能监控)
  2. Go并发控制之sync.WaitGroup
  3. 浅谈P NP NPC
  4. Sublime Text 添加eclipse快捷键
  5. spring 启动流程
  6. 项目中的五级地址联动效果(js)
  7. java数据结构和算法------插入排序
  8. 分别应用include指令和include动作标识在一个jsp页面中包含一个文件。
  9. Linux相关命令
  10. Ionic3学习笔记(一)安装、项目结构与常用命令
  11. sort命令与cat区别25.1 由于sort默认是把结果输出到标准输出,所以需要用重定向才能将结果写入文件,形如sort filename &gt; newfile [root@shiyan a]# cat a.txt aaaaaa [root@shiyan a]# sort a.txt &gt;c.txt ------------- 在重定向前会自动创建c.txt这个文件。 [root@shiyan
  12. SQL Server 利用触发器对多表视图进行更新
  13. postman Installation has failed: There was an error while installing the application. Check the setup log for more information and contact the author
  14. 微信小程序统计分析
  15. 【原创】大数据基础之Spark(3)Spark Thrift实现原理及代码实现
  16. 基础总结(04)-- display:none;&amp;&amp;visibility:hidden;区别
  17. canvas-8clip.html
  18. margin的两个有趣现象:margin合并和margin塌陷
  19. Palindrome Number &amp; Reverse Integer
  20. spark submit参数及调优

热门文章

  1. G. Gangsters in Central City
  2. 学习笔记:python3,代码。小例子习作
  3. [LOJ2538][PKUWC2018]Slay the Spire:DP
  4. Python中的不可变对象类型与可变对象类型
  5. sqli-labs(39)
  6. 整合spring之后,struts2里面的自定义拦截器的invocation.invoke()总是返回input
  7. 浏览器端-W3School-HTML:HTML DOM Object 对象
  8. JavaEE-实验四 HTML与JSP基础编程
  9. ControlTemplate in WPF —— TreeView
  10. 子系统 安装vsftpd