栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 整合mybatis

springboot 整合mybatis 1.先说遇到的问题

controller层启动一直包空指针的错误,看了这篇文章才明白! 链接:http://t.csdn.cn/q3vbH
解决方法:在类上添加注解!

@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解。
@RunWith(SpringRunner.class)在正常情况下测试类是需要@RunWith的,作用是告诉java你这个类通过用什么运行环境运行,例如启动和创建spring的应用上下文。否则你需要为此在启动时写一堆的环境配置代码。你在IDEA里去掉@RunWith仍然能跑是因为在IDEA里识别为一个JUNIT的运行环境,相当于就是一个自识别的RUNWITH环境配置。但在其他IDE里并没有。
原文链接:https://blog.csdn.net/qq_21108099/article/details/111496005
9

2.先添加依赖
 
        
            mysql
            mysql-connector-java
        
        
            log4j
            log4j
            1.2.17
        
        
            com.alibaba
            druid
            1.0.5
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
3.application.yml的配置文件
#配置mybatis的xml位置!
#我的在resource/mapper
@Data
@AllArgsConstructor//带全参的构造器
@NoArgsConstructor//无参
@ToString
public class User {
    private Integer id;
    private String username;

}

②mapper层

package com.me.modules.userinfo.mapper;

import com.me.modules.userinfo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;


@Mapper
@Repository
public interface UserMapper {

      
      User getUserInfo(int id);
}

mapper.xml








    
        select * from usera where id = #{id}
    





id}-->












③Service层

impl实现类

package com.me.modules.userinfo.service.impl;

import com.me.modules.userinfo.domain.User;
import com.me.modules.userinfo.mapper.UserMapper;
import com.me.modules.userinfo.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
    private UserMapper userMapper;


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

④controller控制层—调用service空指针问题:http://t.csdn.cn/q3vbH
package com.me.modules.userinfo.rest;

import com.me.modules.userinfo.domain.User;
import com.me.modules.userinfo.service.UserService;
import com.me.modules.userinfo.service.impl.UserServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserInfoController {

    @Autowired
    private  UserService userService;


    @Test
    public void getuserinfo(){
        User userInfoservice = userService.getUserInfoservice(1);
        System.out.println(userInfoservice);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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