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

Posted System.print("宇航")

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建相关的知识,希望对你有一定的参考价值。

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

一、搭建SpringBoot的项目

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

  

 

二、项目结构

  

 

三、项目代码

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

  1、pom中的mave配置

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.3.2</version>
	</dependency>
		<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>    

 

 

 

   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<String,Object> 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<String, Object> 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<String, Object> 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; }
}

 

  

 

以上是关于SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建的主要内容,如果未能解决你的问题,请参考以下文章

springboot微服务之集成mybatis和mysql整合完整详细版

Java之Spring Boot入门到精通IDEA版SpringBoot整合其他框架Junit,Redis,MyBatis(一篇文章精通系列)中

SpringBoot之整合MyBatis

SpringBoot入门之基于注解的Mybatis

IDEA:springboot框架使用mybatis-generator插件报错:org.apache.ibatis.binding.BindingException: Invalid bound s

IDEA:springboot框架使用mybatis-generator插件报错:org.apache.ibatis.binding.BindingException: Invalid bound s