springboot整合mybatis

添加mybatis依赖

 

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

 

配置文件中配置数据源信息

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    generate-ddl: true
View Code

编写pojo mapper接口 以及mapper映射文件

pojo

public class MUser implements Serializable {
    private int id;
    private String username;
    private String password;
    private String name;
View Code

 mapper接口

public interface UserMapper {
    List<MUser> getUserList();
}

mapper映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.offcn.mapper.UserMapper">
    <select id="getUserList" resultType="com.offcn.pojo.MUser">
        select * from user
    </select>
</mapper>

手动配置mybatis包扫描

  主启动类中添加@MapperScan

@SpringBootApplication
@MapperScan(basePackages = "com.offcn.mapper")
public class HelloApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloApplication.class,args);
    }
}

 

 

 

 

posted @ 2019-11-05 22:53  绝不秃的猿  阅读(182)  评论(0编辑  收藏  举报