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能 ...
随机推荐
- iframe标签用法详解
功能:iframe标签用于定义内联框架. 语法:<iframe></iframe> 内联框架是在一个页面中嵌入另一个页面. 有很多网页看上去是一个网页,但实际上它其中可能镶 ...
- 【spark】连接Hbase
0.我们有这样一个表,表名为Student 1.在Hbase中创建一个表 表明为student,列族为info 2.插入数据 我们这里采用put来插入数据 格式如下 put ‘表命’,‘行键’, ...
- Android以root起一个process[shell脚本的方法]
有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚本,利用am来实现.下面是具体步骤: 1.创建一个包含Main()方法Java p ...
- AAC解码算法原理详解
”
- Scikit-Learn:开源的机器学习Python模块(转载)
摘要: scikit-learn是一个用于机器学习的Python模块,其具有操作简单.效率高.无访问限制.BSD开源协议等等特征,在机器学习这一块是比较受欢迎的. scikit-learn是一个用于机 ...
- Ant入门之引用外部jar文件
笔者在java项目开发中经常遇到引用外部Jar包的情况,使用ant打包过程中需要对其引用.现在此简单记忆以飨来者. 此处引用Log4j,具体程序HelloLog4j.java: package oat ...
- 深入学习Heritrix---解析Frontier(链接工厂)(转)
深入学习Heritrix---解析Frontier(链接工厂) Frontier是Heritrix最核心的组成部分之一,也是最复杂的组成部分.它主要功能是为处理链接的线程提供URL,并负责链接处理完成 ...
- C# 程序部署、调试时间长的解决办法
最近在做数控折弯机项目时,VS2008环境下采用C#..NET Compact Framework开发WinCE.Windows Mobile程序时,编译项目非常慢,看着进度条慢慢刷,有时候需要几分钟 ...
- [ArgumentException: 可能证书“CN=JRNet01-PC”没有能够进行密钥交换的私钥,或者进程可能没有访问私钥的权限。有关详细信息,请参见内部异常。]
堆栈跟踪: [CryptographicException: 密钥集不存在. ] System.Security.Cryptography.Utils.CreateProvHandle(CspPara ...
- 【策略】一致性Hash算法(Hash环)的java代码实现
[一]一致性hash算法,基本实现分布平衡. package org.ehking.quartz.curator; import java.util.SortedMap; import java.ut ...