springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式
项目结构

依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
WebConfig
package com.springlearn.learn.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableScheduling
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
.allowedHeaders("*");
}
}
DemoApplication
package com.springlearn.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
方式一——@Select
User
package com.springlearn.learn.mapper;
import java.util.Map;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface User {
@Select("select * from test where id=#{id}")
Map GetUserbyId(@Param("id") Integer id);
}
DemoApplication
@RestController
public class TestController {
@Autowired
User user;
@ResponseBody
@RequestMapping(value = "/test1", method = RequestMethod.GET)
public Map Test1(HttpServletRequest request){
Map result = user.GetUserbyId(1);
return result;
}
}
方式二——@Select和SqlSession结合
User
package com.springlearn.learn.mapper;
import java.util.Map;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface User {
@Select("select * from test where id=#{id}")
Map GetUserbyId(@Param("id") Integer id);
}
DemoApplication
@RestController
public class TestController {
@Autowired
User user;
@Autowired
SqlSession sqlSession;
@ResponseBody
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public Map Test2(HttpServletRequest request){
// 用法一
Map result = sqlSession.selectOne("com.springlearn.learn.mapper.User.GetUserbyId", 1);
// 用法二
User mapper = sqlSession.getMapper(User.class);
Map result = mapper.GetUserbyId(1);
return result;
}
}
方式三——xml和SqlSession结合
applicationproperties
mybatis.mapper-locations=classpath:mapperxml/*.xml
UserTest
package com.springlearn.learn.mapper;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserTest {
Map<String, Object> GetUserbyId(@Param("id") Integer id);
}
Userxml
<?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.springlearn.learn.mapper.UserTest">
<select id="GetUserbyId" parameterType="int" resultType="java.util.Map">
select * from test where id=#{id}
</select>
</mapper>
TestController
@RestController
public class TestController {
@Autowired
User user;
@Autowired
SqlSession sqlSession;
@ResponseBody
@RequestMapping(value = "/test3", method = RequestMethod.GET)
public Map<String, Object> Test3(HttpServletRequest request, @RequestParam("id") int id){
// 用法一
Map<String, Object> result = sqlSession.selectOne("com.springlearn.learn.mapper.UserTest.GetUserbyId", id);
// 用法二
UserTest mapper = sqlSession.getMapper(UserTest.class);
System.out.println(mapper.GetUserbyId(id));
Map<String, Object> result = mapper.GetUserbyId(id);
return result;
}
}
springboot成神之——mybatis在spring-boot中使用的几种方式的更多相关文章
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
- Spring Boot 整合 Shiro ,两种方式全总结!
在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...
- 【spring boot】【mybatis】spring boot中mybatis打印sql语句
spring boot中mybatis打印sql语句,怎么打印出来?[参考:https://www.cnblogs.com/sxdcgaq8080/p/9100178.html] 在applicati ...
- 【websocket】spring boot 集成 websocket 的四种方式
集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...
- Spring Boot应用启动的三种方式
Spring Boot应用HelloWorld的三种启动方式: 项目的创建可以在http://start.spring.io/网站中进行项目的创建. 首先项目结构: 1. 通过main方法的形式启动 ...
- springboot成神之——mybatis和mybatis-generator
项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...
- Spring Boot 快速搭建的三种方式
方式一:http://start.spring.io/ 打开浏览器,在地址栏中输入http://start.spring.io/ 如下图: 点击generate project 然后就会有一个zip ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- Spring Boot — 运行应用程序5种方式
1. 从IDE中的Run 按钮运行 你可以从IDE中运行Spring Boot应用, 就像一个简单的Java应用, 但是, 你首先需要导入项目. 导入步骤跟你的IDE和构建系统有关. 大多数IDEs能 ...
随机推荐
- python中的mysql操作
一. 数据库在自动化测试中的应用 存测试数据 有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来.这点是非常重要的! 存测试结果 二. python中的数 ...
- 7zip 自解压安装程序
包含自解压安装器的包https://jaist.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920_extra.7z详细说明见7-zip帮助文档的 ...
- Chrome浏览器导入数字证书
1.打开 chrome ,点击 右上角的选项图标,在下拉列表中找到 设置 . 2.在设置页面中,滚动到页面的最底部,找到,并点击显示高级设置,找到 HTTPS/SSL 这一项,点击 管理证书 按键.
- saltstack学习篇
参考链接:http://sofar.blog.51cto.com/353572/1596960/ http://sofar.blog.51cto.com/353572/1596960/ 自动化运维工具 ...
- GAN作用——在我做安全的看来,就是做数据拟合、数据增强
from:https://www.zhihu.com/question/56171002/answer/155777359 GAN的作用,也就是为什么GAN会火了(有部分原因可能是因为Lecun的赞赏 ...
- f5 ddos cc——Mitigating DDoS Attacks with F5 Technology
摘自:https://f5.com/resources/white-papers/mitigating-ddos-attacks-with-f5-technology Mitigating Appli ...
- 【51nod-1596】搬货物
现在有n个货物,第i个货物的重量是 2wi .每次搬的时候要求货物重量的总和是一个2的幂.问最少要搬几次能把所有的货物搬完. 样例解释: 1,1,2作为一组. 3,3作为一组. Input 单组测试数 ...
- VS2017连接到中国区的Azure
1. 安装Azure Environment Select扩展 2. 选择中国区的Azure 3. 之后就可以使用中国区的账号登录了 参考链接:https://docs.azure.cn/zh-cn/ ...
- android 进制转换方法
import android.util.Log; public class CommandHelper { public static String intToHexString(int value) ...
- 20165222 实验一java开发环境的熟悉
实验内容及步骤 实验一 Java开发环境的熟悉-1 1 建立“自己学号exp1”的目录 2 在“自己学号exp1”目录下建立src,bin等目录 3 javac,java的执行在“自己学号exp1”目 ...