优化mybatis的Mapper依赖,service层优化

7 篇文章 0 订阅
3 篇文章 0 订阅

先完成这个

1.项目结构
在这里插入图片描述

2.添加IService.class、BaseService.class

package com.xpf.service;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用接口
 */
@Service
public interface IService<T> {

    T selectByKey(Object key);

    int save(T entity);

    int delete(Object key);

    int updateAll(T entity);

    int updateNotNull(T entity);

    List<T> selectByExample(Object example);

    //TODO 其他...

}

package com.xpf.service.impl;

import com.xpf.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public abstract class BaseService<T> implements IService<T> {
    @Autowired
    protected Mapper<T> mapper;

    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public T selectByKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    public int save(T entity) {
        return mapper.insert(entity);
    }

    public int delete(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    public int updateAll(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    public int updateNotNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    //TODO 其他...
}

2.添加service

package com.xpf.service;

import com.xpf.domain.UserInfo;

public interface UserService extends IService<UserInfo> {
}

package com.xpf.service;

import com.xpf.domain.UserInfo;

public interface UserService extends IService<UserInfo> {
}

3.测试

package com.xpf;

import com.xpf.domain.SysRoles;
import com.xpf.domain.UserInfo;
import com.xpf.mapper.SysRolesMapper;
import com.xpf.mapper.UserInfoMapper;
import com.xpf.service.UserService;
import org.apache.catalina.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
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 SpringbootMapperApplicationTests {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    SysRolesMapper sysRolesMapper;


    @Autowired
    UserService userService;
    @Test
    public void contextLoads() {
    }

    @Test
    public void tsetfindUser(){
        System.out.println(userInfoMapper.selectByPrimaryKey(1));

        System.out.println(sysRolesMapper.selectByPrimaryKey(1));

        System.out.println(userService.selectByKey(1));

/*
        UserInfo userInfo=new UserInfo();
        userInfo.setUid(1);

        System.out.println(userInfoMapper.selectOne(userInfo));*/
    }
}

4.结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值