mybatis使用通用mapper步骤

Posted fanqiexin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis使用通用mapper步骤相关的知识,希望对你有一定的参考价值。

技术图片

首先在pom.xml文件中通过maven下载通用mapper包

技术图片

 创建通用mapper接口且继承Mapper<T>类泛型

技术图片

 

 在mybatis配置文件中引入mapperclass

技术图片

 接下来便开始在通用mapper的世界中畅游了~~~~~~~~~

package com.aaa.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;

@Table(name = "person")
public class Person implements Serializable 
    //最好使用integer类型,int类型为空时显示为0,0太过危险
    //根据主键查询则在实体类属性中加入ID为主键的性质
    //映射只接受Integer类型的字段
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer age;
    private Dept dept;

    //不需要的属性则加上transient来进行隔离查询
    @Transient
    private String gender;


    public Person() 
    

    public Person(String name, Integer age) 
        this.name = name;
        this.age = age;
    

    public Person(Integer id, String name, Integer age) 
        this.id = id;
        this.name = name;
        this.age = age;
    

    public Person(Integer id, String name, Integer age, Dept dept) 
        this.id = id;
        this.name = name;
        this.age = age;
        this.dept = dept;
    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public Integer getAge() 
        return age;
    

    public void setAge(Integer age) 
        this.age = age;
    

    public Dept getDept() 
        return dept;
    

    public void setDept(Dept dept) 
        this.dept = dept;
    

    @Override
    public String toString() 
        return "Person" +
                "id=" + id +
                ", name=‘" + name + \\‘ +
                ", age=" + age +
                ", dept=" + dept +
                ;
    
package com.aaa.test;

import com.aaa.dao.ITestMapperDAO;
import com.aaa.entity.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class JavaTestTwo 
    public static void main(String[] args) throws IOException 
        InputStream in = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MapperHelper mapperHelper = new MapperHelper();
        mapperHelper.registerMapper(ITestMapperDAO.class);

        mapperHelper.processConfiguration(sqlSession.getConfiguration());
        ITestMapperDAO mapper = sqlSession.getMapper(ITestMapperDAO.class);

//        List<Person> personList = mapper.selectAll();
//        System.out.println(personList);

        Person person = new Person();
        person.setId(2);
        Person person1 = mapper.selectByPrimaryKey(person);
        System.out.println(person1);

//        Example example = new Example(Person.class);
//        //可在此基础上添加对象
//        Example.Criteria criteria = example.createCriteria();
//
//        criteria.andGreaterThan("age",15);
//
//
//        List<Person> personList = mapper.selectByExample(example);
//
//        System.out.println(personList);

//        Person person = new Person(1,"茉莉",15);
//        int insert = mapper.insert(person);
//        sqlSession.commit();
//        System.out.println(insert);

    

 

以上是关于mybatis使用通用mapper步骤的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis通用Mapper

Mybatis通用Mapper

Mybatis通用Mapper(转)

Mybatis插件和通用Mapper使用

Mybatis 通用 Mapper 3.3.9 发布

MyBatis 通用 Mapper 4.0.0 发布