如何使用 RESTFUL API 和 SpringBoot-MyBatis-MySQL 对我的 MySQL 数据库进行基本 SELECT FROM 调用?

How to Make a Basic SELECT FROM call to my MySQL database using RESTFUL API with SpringBoot-MyBatis-MySQL?

问题很简单。我想使用上面提到的工具对我的 MySQL 数据库进行基本的 SELECT * FROm 调用。 我将使用 XML 风格的映射器 MyBatis 配置

到目前为止,我在使用字符串语句时成功了

@RestController
@RequestMapping("/")
public class ApplicationRestController {

    Actor actor;
    //this works easily because no connections to SQL are being made.
    @GetMapping("/hello")
    public String hello() {
        return "HELO WORLD";        
    }
}

但是如果我想从我的数据库中获取数据怎么办?这该怎么做?我需要 SqlSession 吗?

有没有人有用link?

这是我更新的错误:

Error creating bean with name 'applicationRestController': Unsatisfied dependency expressed through field 'actorMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actorMapper' defined in file [/Users/helios/Documents/workspace-sts-3.9.8.RELEASE/mybatis-sakila/target/classes/com/helios/mybatissakila/mappers/ActorMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [mybatis-sakila/src/main/resources/mybatis-mapper/ActorMapper.xml]'; nested exception is

java.io.FileNotFoundException: class path resource [mybatis-sakila/src/main/resources/mybatis-mapper/ActorMapper.xml] cannot be opened because it does not exist

这些是我的相关文件:

Actor.java

public class Actor {

    private static final long serialVersionUID = 1L;

    private int actor_id;
    private String first_name;
    private String last_name;
    private Date last_update;

    public int getActor_id() {
        return actor_id;
    }
    public void setActor_id(int actor_id) {
        this.actor_id = actor_id;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public Date getLast_update() {
        return last_update;
    }
    public void setLast_update(Date last_update) {
        this.last_update = last_update;
    }
}

ActorMapper.java

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;  
import com.helios.mybatissakila.model.Actor;

@Mapper
public interface ActorMapper {

    List <Actor> selectAllActors();
}

ApplicationRestController.java

@RestController
@RequestMapping("/")
public class ApplicationRestController {

    @Autowired
    ActorMapper actorMapper;

    //working as not connected to mysql
    @GetMapping("/hello")
    public String hello() {
        return "MyBatis + SpringBoot + MySQL!";

    }
    //error 
    @GetMapping("/actors")
    public List<Actor> selectAllActors(){
            return actorMapper.selectAllActors();
    }
}

ActorMapper.xml

<resultMap id="ActorResultMap" type="Actor">
    <id column="actor_id" property="actor_id" jdbcType="INTEGER"/>
    <result column="first_name" property="first_name" />
    <result column="last_name" property="last_name" />
    <result column="last_update" property="last_update" />
</resultMap>

<select id="selectAllActors" resultMap="ActorResultMap">
        select * from actor
</select>

application.properties

server.port = 9090

spring.datasource.url= jdbc:mysql://localhost:3306/sakila
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.dbcp2.test-while-idle= true
spring.datasource.dbcp2.validation-query= select 1

#mybatis entity scan packages
mybatis.type-aliases-package=com.helios.mybatissakila.model
#Mapper.xml location
mybatis.mapper-locations=classpath*:mybatis-sakila/src/main/resources/ActorMapper.xml
mybatis.typeAliasesPackage=com.helios.mybatissakila.model.Actor

logging.level.root=WARN
logging.level.sample.mybatis.mapper=TRACE

我觉得这是错误的,你能帮我改一下吗? “classpath”只是一个应该用实际值替换的样板代码吗?

mybatis.mapper-locations=classpath*:mybatis-sakila/src/main/resources/ActorMapper.xml

注意:我已经设法使用@Annotation 样式执行REST API 调用,但我想使用XML 样式。

你不需要 SqlSession,MyBatis 使用它自己的实现,你唯一需要做的就是创建 MyBatis 配置并设置它的所有依赖项。

简而言之,您需要执行以下操作:

创建您的 POJO:

public class Dog{
   private Long id;
   private String name;

  //Getters and Setters
}

然后需要在java中创建MyBatis mapper接口:

public interface DogMapper{
   public void insert(Dog dog);
}

然后 DogMapper.xml,您可以在其中放置查询。

最后,您将查询称为:

@Service
public class DogService{
   @Autowired
   private DogMapper dogMapper;

   public void insert(Dog dog){
       dogMapper.insert(dog);
   }
}

这是对 MyBatis 工作方式的过度简化,但是我建议您看一些 MyBatis CRUD 示例。里面会有详细的介绍,还有很重要的配置。

好的,我是这样修复的:

正如我确实怀疑的那样,类路径 是使这项工作成功的主要因素。一旦我在 application.properties:

中提供了正确的路径
mybatis.mapper-locations=classpath:/mybatis-mapper/ActorMapper.xml

我能够从 MySQL 拨打 SELECT 电话。这不是很有趣吗? :)