之前搭建了@Select标签来做SringBoot+Mybatis的集成。这次使用@SelectProvider标签的方式搭建一次。

一、搭建SpringBoot的项目

https://start.spring.io/自己配置SpringBoot的项目,点击“Generate Project”按钮就可以下载下来一个配置好的SpringBoot项目。

二、项目结构

三、项目代码

demo代码实现的是对表数据的一个简单查询。

1、pom中的mave配置

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

2、Controller

package com.example.demo.Controller;

import com.example.demo.Service.TeacherService;

import com.example.demo.entity.Teacher;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TeacherController {

@Autowired(required = false)

TeacherService userService;

@RequestMapping("selectUser")

public Teacher getUserOne(String id){

Teacher tea = new Teacher();

tea.setId(id);

Teacher teacher1 = userService.findTeacherById(tea);

return teacher1;

}

@RequestMapping("selectUserByName")

public Teacher getUserOne(String id,String name){

Teacher tea=new Teacher();

tea.setId(id);

tea.setName(name);

Teacher teacher=userService.findTeacherByName(tea);

return teacher;

}

}

3、Service

一个interface接口,一个Impl实现

package com.example.demo.Service;

import com.example.demo.entity.Teacher;

public interface TeacherService {

Teacher findTeacherById(Teacher user);

Teacher findTeacherByName(Teacher user);

}

接口实现:

package com.example.demo.ServiceImpl;

import com.example.demo.Mapper.TeacherMapper;

import com.example.demo.Service.TeacherService;

import com.example.demo.entity.Teacher;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.HashMap;

import java.util.Map;

@Service

public class TeacherServiceImpl implements TeacherService {

@Autowired(required = false)

TeacherMapper userMapper;

@Override

public Teacher findTeacherById(Teacher teacher) {

return userMapper.findUserById(teacher);

}

@Override

public Teacher findTeacherByName(Teacher teacher) {

Map maps=new HashMap<>();

maps.put("id",teacher.getId());

maps.put("name",teacher.getName());

return userMapper.findUserByName(maps);

}

}

4、Mapper代码

package com.example.demo.Mapper;

import com.example.demo.entity.Teacher;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.SelectProvider;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**

* The interface Teacher mapper.

*/

@Mapper

public interface TeacherMapper {

/**

* The constant returnSql.

*/

String returnSql="id,name";

/**

* Find user by id teacher.

*

* @param user the user

* @return the teacher

*/

@SelectProvider(type = UserDaoProvider.class, method = "findTeacherById")

Teacher findUserById(Teacher user);

/**

* Find user by name teacher.

*

* @param map the map

* @return the teacher

*/

@SelectProvider(type = UserDaoProvider.class, method = "findTeacherByName")

Teacher findUserByName(Map map);

/**

* The type User dao provider.

*/

class UserDaoProvider {

/**

* Find teacher by id string.

*

* @param teacher the teacher

* @return the string

*/

public String findTeacherById(Teacher teacher) {

String sql = "SELECT "+returnSql+" FROM Teacher";

if(teacher.getId()!=null){

sql += " where id = #{id}";

}

return sql;

}

/**

* Find teacher by name string.

*

* @param map the map

* @return the string

*/

public String findTeacherByName(Map map) {

String name = (String) map.get("name");

return new SQL() {

{

SELECT(returnSql);

FROM("Teacher");

WHERE("name="+ name);

}

}.toString();

}

}

}

在程序启动时,会扫描Mapper文件,所以需要在Mapper文件里添加@Mapper注解。

还可以在main文件中添加@MapperScan()注解:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.example.demo.Mapper")

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

5、实体类

package com.example.demo.entity;

public class Teacher {

private String id;

private String name;

public String getId() { return id; }

public void setId(String id) { this.id = id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

}

mysql selectprovider_SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建相关推荐

  1. SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建

    之前搭建了@Select标签来做SringBoot+Mybatis的集成.这次使用@SelectProvider标签的方式搭建一次. 一.搭建SpringBoot的项目 https://start.s ...

  2. SpringBoot+Mybatis 框架之 @Select注解方式搭建

    最近两天在帮同学搭建SpringBoot框架,我以往使用的是xml映射文件的方式,这次我的同学要我使用@Select注解的方式搭建的一次.感觉挺有意思的,分享给大家. 1.创建SpringBoot项目 ...

  3. JAVA配置注解方式搭建简单的SpringMVC前后台交互系统

    前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...

  4. mybatis框架使用generator的快速搭建

    mybatis框架使用generator的快速搭建 首先建立一个maven项目,在idea中直接使用下图建立 然后按照以下步骤 使用generator工具快速生成,dao层,bean层,mapper层 ...

  5. mysql+xml+注释,springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)...

    一.注解方式 pom org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java org. ...

  6. MyBatis多参数传递之注解方式示例--转

    原文地址:http://legend2011.blog.51cto.com/3018495/1015003 若映射器中的方法只有一个参数,则在对应的SQL语句中,可以采用#{参数名}的方式来引用此参数 ...

  7. Mybatis(17)注解方式增删改查单表

    IUserDao.java /*在mybatis中,CRUD一共有四个注解 * @Select @Insert @Update @Delete*/ public interface IUserDao ...

  8. mybatis mysql ssh_SSH Mybatis 框架

    Hibernate框架是用来对数据库的代码进行封装,ORM映射,使用java反射机制,支持各种数据库. 原理: 1.Configuration().configure()读取并解析hibernate. ...

  9. mybatis 连带操作(注解方式)(两张表关联,一张表插入一条新数据,另外一张表也跟着插入一条新数据)

    以角色权限模块中增加功能为例子: a) 概念: 连带操作:首先先在Role表中插入一条数据,接着拿到rid,往Role-Acl表中插入一条数据 b)数据库设计: Role表: Role-Acl表: c ...

最新文章

  1. 关于继承方式和访问权限
  2. Educational Codeforces Round 101 (Rated for Div. 2) C. Building a Fence 思维取范围
  3. java与MySQL做购物系统_java Swing mysql实现简单的购物系统项目源码附带指导视频教程...
  4. php项目怎么分配,php项目目录的合理划分和Pipeline 组件的使用场景
  5. 11-windows下卸载Orcale
  6. shiro 与spring的集成
  7. 某化工学院安装锐捷elog
  8. jsfl读取xml,图片,并生成swf
  9. 服务器上怎么安虚拟主机呀,上线虚拟主机产品步骤
  10. 手把手教你千万级唯一ID如何生成
  11. 典型知识图谱项目:FreeBase、WikiData、Schema.org、DBPedia、YAGO、WordNet、ConceptNet、BabelNet、Palantir
  12. 常见报错:RuntimeError: expected scalar type Long but found Float
  13. H3C SecPath F100 系列防火墙基本配置
  14. 2004年9月全国计算机等级考试二级C语言笔试试题
  15. 【Codeforces 869 C The Intriguing Obsession】 组合数学 思维
  16. 日志分析篇---Web日志分析
  17. 计算机excel按F4是那个公式,excel中键盘F4到底怎么用?_excle 中的f4
  18. pycharm运行tensorflow报错
  19. 最新PHP软文发稿新闻文章发布自助推广平台源码
  20. 处理ftp登陆提示[右] 500 OOPS: cannot change directory:/home/jock11

热门文章

  1. PlateSpin forge V2P回推步骤。
  2. 某android平板项目开发笔记--自定义sharepreference UI
  3. 宝塔php7.1地址在哪里,宝塔Linux面板安全入口地址忘了(方法一)
  4. 【工具分享】deepin v20.5桌面快捷方式编辑器:desktop-entry-editor
  5. vue elementUI表单输入完成后回车触发事件@keyup.enter.native
  6. Jenkins CLI命令行工具,助你轻松管理 Jenkins
  7. linux du -hd1查看文件及目录所占磁盘空间
  8. linux read while 变量运算
  9. linux命令使用示例:查看某目录属于哪个分区
  10. Scala @BeanProperty注解生成getter/setter