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; }
}

 

  

 

转载于:https://www.cnblogs.com/Lyh1997/p/10195183.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值