springboot整合mybatis(xml版)

1.创建一个springboot工程,导入相关依赖

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>

2.创建一个实体类(getter/setter就不展示了)

public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    }

3.创建mapper层,加上@Repository注解,表示加入容器中

@Repository
public interface UserMapper {
    User getUserById(int id);
}

4.创建service层

public interface UserService {
    User getUserById(int id);
}

5.创建service层的实现类

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }
}

6.在resources创建两个文件夹,一个存放mybatis核心配置文件,一个存放对应mapper的配置文件

springboot整合mybatis(xml版)_第1张图片

7.创建application.yml文件,对数据库和mapper.xml文件进行配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  config-location: classpath:/config/mybatis-config.xml
  mapper-locations: classpath:/mybatis/*.xml

springboot整合mybatis(xml版)_第2张图片

8.编写controller层

@RestController
public class UserController {


    @Autowired
    UserService userService;

    @RequestMapping("/user/{id}")
    public User getUserById(@PathVariable("id") int id){

        return userService.getUserById(id);

    }
}

9.在启动类上添加@MapperScan(“com.example.demo.mapper”),并把路径指向你的mapper层所在地址

@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
        System.out.println("启动成功!");
    }

}

10.测试

springboot整合mybatis(xml版)_第3张图片
数据被取出来了
springboot整合mybatis(xml版)_第4张图片
完成,很简陋,哈哈哈

你可能感兴趣的:(springboot整合mybatis(xml版))