天天看点

Mybatis通用Mapper(springboot环境下使用)

​​mapper3通用接口大全​​​:​​https://gitee.com/free/Mapper.git​​

1、添加pom文件依赖

<!--mapper -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.1.4</version>
</dependency>      

2、实体类

package com.ahut.entity;

import java.io.Serializable;
import java.util.Date;

import com.ahut.core.enums.SexEnum;

/**
 * 
 * @ClassName: Demo
 * @Description:
 * @author cheng
 * @date
public class Demo implements Serializable

    /**
     * 
     */
    private static final long serialVersionUID = 4122974131420281791L;

    private Date birthDay;
    private String userName;
    private int age;
    private String id;
    private SexEnum sex;

    public Demo() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Demo [id=" + id + ", userName=" + userName + ", age=" + age + ", birthDay=" + birthDay + ", sex=" + sex
                + "]";
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    public SexEnum getSex() {
        return sex;
    }

    public void setSex(SexEnum sex) {
        this.sex = sex;
    }

}      

3、dao层

没有xml的mapper

package com.ahut.mapper;

import com.ahut.entity.Demo;

import tk.mybatis.mapper.common.Mapper;

/**
 * 
 * @ClassName: DemoWithNoXmlMapper
 * @Description: 
 * @author cheng
 * @date
public interface DemoWithNoXmlMapper extends Mapper<Demo> {      

4、service层

接口

package com.ahut.service;

import java.util.List;

import com.ahut.entity.Demo;

/**
 * 
 * @ClassName: DemoWithNoXmlService
 * @Description:
 * @author cheng
 * @date
public interface DemoWithNoXmlService

    /**
     * 
     * @Title: selectAll
     * @Description: 查询
     * @return      

实现

package com.ahut.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ahut.entity.Demo;
import com.ahut.mapper.DemoWithNoXmlMapper;
import com.ahut.service.DemoWithNoXmlService;

/**
 * 
 * @ClassName: DemoWithNoXmlServiceImpl
 * @Description:
 * @author cheng
 * @date
@Service
@Transactional
public class DemoWithNoXmlServiceImpl implements DemoWithNoXmlService

    @Autowired
    private DemoWithNoXmlMapper dao;

    /**
     * 
     */
    @Override
    public List<Demo> selectAll() {
        return      

5、测试

package com.ahut.serviceimpl;

import java.util.List;

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;

import com.ahut.entity.Demo;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoWithNoXmlServiceImplTest

    @Autowired
    private DemoWithNoXmlServiceImpl service;

    /**
     * 
     * @Title: testSelectAll
     * @Description:
     */
    @Test
    public void testSelectAll() {
        int resultNum = 0;
        List<Demo> demoList = service.selectAll();
        for (Demo demo : demoList) {
            System.out.println(""      

执行testSelectAll方法:

查询出来了所有数据

Mybatis通用Mapper(springboot环境下使用)
SELECT birth_day,user_name,age,id,sex FROM