mysql selectprovider_SpringBoot+Mybatis 框架之 @SelectProvider注解方式搭建
之前搭建了@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注解方式搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 幂等校验是什么意思_阿里面试官:接口的幂
- 下一篇: python中def fun(a、b=2