spring+mybatis+通用mapper使用介绍

版本:spring-3.2.9.RELEASE、mybatis-3.4.4、tk.mybatis-3.4.3
使用tk.mybatis通用mapper,很方便实现表的基本CRUD操作,不需要为实体类编写mapper.xml文件。具体参考https://mapperhelper.github.io/docs/

1,maven配置



    org.springframework
    spring-aop
    3.2.9.RELEASE

    
    org.springframework
    spring-beans
    3.2.9.RELEASE


    org.springframework
    spring-context
    3.2.9.RELEASE


    org.springframework
    spring-core
    3.2.9.RELEASE


    org.springframework
    spring-expression
    3.2.9.RELEASE


    org.springframework
    spring-web
    3.2.9.RELEASE


    org.springframework
    spring-tx
    3.2.9.RELEASE


    org.springframework
    spring-jdbc
    3.2.9.RELEASE



                                 
    org.mybatis         
    mybatis       
    3.4.4               
                            
                             
    org.mybatis         
    mybatis-spring
    1.3.1               


                            
                             
    tk.mybatis          
    mapper        
    3.4.3               



                               
    commons-dbcp      
    commons-dbcp
    1.4               
                          
                               
    org.apache.commons    
    commons-pool2   
    2.3
                              


                                                                   
    mysql                     
    mysql-connector-java
    5.1.6                     




    commons-lang
    commons-lang
    2.5


                                                        
    log4j      
    log4j
    1.2.16     

2,spring配置,spring-app.xml




    
    
    

    
        
            ${jdbc.driverClassName}
        
        
            ${jdbc.url}
        
        
            ${jdbc.username}
        
        
            ${jdbc.password}
        
        
        
        
        
        
  
    

    
        
    

    
        
        
            
                classpath:mybatis/mapper/*.xml
            
        
        
    

    
    
        
        
    

3,编写实体类

package com.mybatis.mymapper;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.commons.lang.builder.ToStringBuilder;

@Table(name="T_CDR")
public class CdrDO {
    @Id
    @Column(name="CDR_ID")
    private Long cdrId;
    private String caller;
    private String callee;
    private Date beginTime;
    private Date endTime;
    @Column(name = "CALL_DURATION")
    private Integer duration;
    private Double fee;

    public Long getCdrId() {
        return cdrId;
    }

    public void setCdrId(Long cdrId) {
        this.cdrId = cdrId;
    }

    public String getCaller() {
        return caller;
    }

    public void setCaller(String caller) {
        this.caller = caller;
    }

    public String getCallee() {
        return callee;
    }

    public void setCallee(String callee) {
        this.callee = callee;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public Double getFee() {
        return fee;
    }

    public void setFee(Double fee) {
        this.fee = fee;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

4,编写mapper接口

package com.mybatis.mymapper;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository("cdrMapper")  
    public interface CdrMapper extends Mapper{
}

5,编写service类

package com.mybatis.mymapper;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

@Service("myMapper")
public class MyMapper {
    @Transactional
    public void crud_transaction(AbstractXmlApplicationContext context) {
        CdrMapper cdrMapper = (CdrMapper) context.getBean("cdrMapper");
        CdrDO cdr = new CdrDO();
        cdr.setCdrId(1L);
        cdr.setCdrId(4L);
        System.out.println("inserted: " + cdrMapper.insert(cdr));
        cdr.setCallee("callee");
        System.out.println("updated: " + cdrMapper.updateByPrimaryKey(cdr));
        cdr.setCallee(null);
        cdr.setCaller("caller");
        System.out.println("updated: "
            + cdrMapper.updateByPrimaryKeySelective(cdr));
        CdrDO cdr_1 = cdrMapper.selectByPrimaryKey(4L);
        System.out.println(cdr_1);
        Example example = new Example(CdrDO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("CDR_ID=", 4L);
        cdrMapper.deleteByExample(example);
    }

    public void crud_single(AbstractXmlApplicationContext context) {
        CdrMapper cdrMapper = (CdrMapper) context.getBean("cdrMapper");
        CdrDO cdr = new CdrDO();
        cdr.setCdrId(1L);
        cdr.setCdrId(4L);
        System.out.println("inserted: " + cdrMapper.insert(cdr));
        cdr.setCallee("callee");
        System.out.println("updated: " + cdrMapper.updateByPrimaryKey(cdr));
        cdr.setCallee(null);
        cdr.setCaller("caller");
        System.out.println("updated: "
            + cdrMapper.updateByPrimaryKeySelective(cdr));
        CdrDO cdr_1 = cdrMapper.selectByPrimaryKey(4L);
        System.out.println(cdr_1);
        Example example = new Example(CdrDO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("CDR_ID=", 4L);
        cdrMapper.deleteByExample(example);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "spring-app.xml" });
        MyMapper test = (MyMapper) context.getBean("myMapper");
        test.crud_single(context);
        test.crud_transaction(context);
        context.close();
    }
}

6,运行MyMapper,观察输出日志

  • 1,运行crud_single()时,可以观察到cdrMapper的方法执行时,都会创建新的SqlSession;
  • 2,运行crud_transaction()时,观察到只创建了一次SqlSession