栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

【springboot2】整合mybatis-基本环境搭建

【springboot2】整合mybatis-基本环境搭建

    导入mybatis官方starter编写mapper接口。标准@Mapper注解com.liang.webadmin.mapper.AccountMapper编写实现接口类。com.liang.webadmin.service.AccountService编写sql映射文件并绑定mapper接口mybatis/mapper/AccountMapper.xml在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration)resourcesapplication.yaml

1.导入mybatis官方starter

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        
    编写mapper接口。标准@Mapper注解[^2]com.liang.webadmin.mapper.AccountMapper
import com.liang.webadmin.bean.Account;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AccountMapper {
    public Account getAcct(Long id);
}
    编写实现接口类。[^3]com.liang.webadmin.service.AccountService
import com.liang.webadmin.bean.Account;
import com.liang.webadmin.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {

    @Autowired
    AccountMapper accountMapper;

    public Account getAcctByid(Long id){
        return accountMapper.getAcct(id);
    };
}
    编写sql映射文件并绑定mapper接口[^4]mybatis/mapper/AccountMapper.xml





    
        select * from  user where  id=#{id}
    




    在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration)[^5]resourcesapplication.yaml
# 配置mybatis规则

mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
#可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号