SpringBoot整合Mybatis

准备工作

1.准备数据库等一些列操作
创建一个测试所用的数据库
创建service,dao层等
创建mapper.xml文件(dao层操作数据库的文件)
…操作和SSM类似

2.配置文件
application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8


mybatis:
  type-aliases-package: com.xjj.demo.domain   #别名包
  mapper-locations: classpath:mybatis/*.xml   #Mapper.XML文件
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #mybatis日志文件

3.Service和Dao层调用关系
IPersonService

public interface IPsersonService {

    List<Person> findAllPerson();
}

PersonServiceimpl

import com.xjj.demo.dao.IPersonDao;
import com.xjj.demo.domain.Person;
import com.xjj.demo.service.IPsersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonServiceimpl implements IPsersonService {
    @Autowired
    IPersonDao personDao;
    @Override
    public List<Person> findAllPerson() {
    List<Person>  list=personDao.findAll();
    return list;
    }

}

IPersonDao

import com.xjj.demo.domain.Person;


import java.util.List;

public interface IPersonDao  {

    List<Person> findAll();
}


IPersonDao.xml

<?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.xjj.demo.dao.IPersonDao">
    <select id="findAll" resultType="person">
            select * from person
        </select>
</mapper>

注意
在Springboot中相应的Mapper.xml文件可随意放置
在这里插入图片描述

在这里插入图片描述
在这里我放置在:/mybatis下
相的,在整合Mybatis 文件中,Mapper.xml文件信息如下:
在这里插入图片描述因为我不可能只读取一个Mapper.xml文件,所有用*来替代文件名


因为SpringBoot启动一个程序来运行
在这里插入图片描述


测试!

测试数据库中的数据

在这里插入图片描述
测试代码

 @Test
    public void test02(){
    List<Person> list=Service.findAllPerson();
     System.out.println(list);

测试结果
在这里插入图片描述
和测试数据库中的数据相匹配!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值