建站公司塔山双喜,seo关键词优化提高网站排名,沈阳人流,口碑好网站建设在哪里之前搭建了Select标签来做SringBootMybatis的集成。这次使用SelectProvider标签的方式搭建一次。 一、搭建SpringBoot的项目 https://start.spring.io/自己配置SpringBoot的项目#xff0c;点击“Generate Project”按钮就可以下载下来一个配置好的SpringBoot项目。 二、项目结…之前搭建了Select标签来做SringBootMybatis的集成。这次使用SelectProvider标签的方式搭建一次。 一、搭建SpringBoot的项目 https://start.spring.io/自己配置SpringBoot的项目点击“Generate Project”按钮就可以下载下来一个配置好的SpringBoot项目。 二、项目结构 三、项目代码 demo代码实现的是对表数据的一个简单查询。 1、pom中的mave配置 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.2/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/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 teanew Teacher();tea.setId(id);tea.setName(name);Teacher teacheruserService.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) {MapString,Object mapsnew 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 returnSqlid,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(MapString, 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(MapString, 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