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

【springboot2】整合mybatis-基本环境搭建,第1张

【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配置项中即可

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5712339.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存