springboot整合mybatis

偶尔善良 提交于 2019-12-03 15:29:18

添加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);
    }
}

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!