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);

    }
}

 

posted @ 2019-07-31 21:10  织式  阅读(7258)  评论(0编辑  收藏  举报