spring boot + mybatis整合

spring boot +mybatis整合
1.创建spring boot工程
file——>new——>project——>spring initializr
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建完成后目录结构如下
在这里插入图片描述
配置配置文件application.properties

#页面热加载
spring.thymeleaf.cache = false

#配置请求的默认上下文路径
server.servlet.context-path=/springboot0525
#端口号
server.port=8888
        
# 数据库访问配置
spring.datasource.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#配置实体类包扫描
mybatis.type-aliases-package=com.linewell.springboot0525.model
#配置mapper.xml文件扫描
mybatis.mapper-locations=classpath:mappers/*.xml
        
#视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

mybatis.mapper-locations=classpath:mappers/*.xml配置中的classpath路径是基于resource路径填写mapper.xml文件的路径
mybatis.type-aliases-package=com.linewell.springboot0525.model填写mapper.xml对应的实体类的完整包路径

也可以不使用application.properties,在application.yml文件中配置,如下方式配置(只是举个例子,yml的配置文件不全),当然也可以使用二者结合,将不同的内容分别配置在这两个文件中,不要重复配置同一内容。yml文件根据有层次,变量与值之间以冒号隔开,冒号后要有空格,.properties文件中变量与之之间是以等号隔开

server:
  servlet:
    context-path: /springboot0525
  port: 8888
  maxHttpHeaderSize : 3145728

mybatis:
  typeAliasesPackage: com.linewell.springboot0525.model
  mapper-locations: classpath:mappers/*.xml

配置开始编写代码
注意spring boot的主程序上要配上mapper.java文件的包扫描
在这里插入图片描述
代码编写完成后的目录结构如下
在这里插入图片描述
代码如下:
UserController文件

package com.linewell.springboot0525.controller;

import com.linewell.springboot0525.model.UserPO;
import com.linewell.springboot0525.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/getUser")
    public UserPO getUserById(int id){
        System.out.println("=======Controller=====id==="+id);
        UserPO userPO =  userService.getUserById(id);
        return userPO;
    }

}

UserMapper文件

package com.linewell.springboot0525.dao;
import com.linewell.springboot0525.model.UserPO;

public interface UserMapper {

    UserPO getUserById(int id);
}

UserPO文件

package com.linewell.springboot0525.model;

public class UserPO {

    private int id;
    private String username;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserPO{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}

UserServiceImpl文件

package com.linewell.springboot0525.service.impl;

import com.linewell.springboot0525.dao.UserMapper;
import com.linewell.springboot0525.model.UserPO;
import com.linewell.springboot0525.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserPO getUserById(int id) {
        System.out.println("======ServiceImpl==id==="+ id);
        UserPO userPO = userMapper.getUserById(id);
        return userPO;
    }
}

UserService

package com.linewell.springboot0525.service;

import com.linewell.springboot0525.model.UserPO;
import org.springframework.stereotype.Service;


public interface UserService {

    UserPO getUserById(int id);
}

Springboot0525Application文件

package com.linewell.springboot0525;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.linewell.springboot0525.dao")
public class Springboot0525Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot0525Application.class, args);
    }

}

如果在serviceImpl层中userMapper上添加 @Autowired后提示Could not autowire. No beans of ‘UserMapper’ type found
@Autowired
UserMapper userMapper;
可以pom文件中添加如下依赖


UserMapper.xml文件,mapper.xml文件常见完成后不要随便移动其位置,否则会报类似这样的错误
 Invalid bound statement (not found): com.linewell.springboot0525.dao.UserMapper.getUserById
<?xml version="1.0" encoding="UTF-8"?> select * from user where id = #{id} ``` org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 ``` 测试

在这里插入图片描述
在这里插入图片描述
数据库test01中的数据

在这里插入图片描述

在控制台上打印sql语句
参考:https://blog.csdn.net/qq_16855077/article/details/84975564
在application.yml中配置

logging:
  level:
      com.linewell.springboot0525.dao: debug

com.linewell.springboot0525.dao为useMapper.java文件所在的包
或者在application.properties文件中配置也可以,在application.properties中配置如下

logging.level.com.linewell.springboot0525.dao=debug

猜你喜欢

转载自blog.csdn.net/qq_43039260/article/details/90551361