zoukankan      html  css  js  c++  java
  • 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; }
    }
    

      

  • 相关阅读:
    Linux环境下安装MySQL(解压方式)
    Linux环境下安装MySQL(yum方式)
    Linux环境下安装ActiveMq
    Linux环境下安装Tomcat
    Linux环境下安装JDK
    Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)
    Linux就该这么学--命令集合5(用户与组管理命令)
    Linux就该这么学--命令集合4(文件目录管理命令)
    Linux就该这么学--命令集合3(文本文件编辑命令)
    解决shell脚本“syntax error near unexpected token `fi'”的问题。
  • 原文地址:https://www.cnblogs.com/Lyh1997/p/10195183.html
Copyright © 2011-2022 走看看