之前搭建了@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;@Overridepublic Teacher findTeacherById(Teacher teacher) {return userMapper.findUserById(teacher);}@Overridepublic 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; }
}
?
?