博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring+mybatis+通用mapper(tk.mybatis)使用介绍
阅读量:5919 次
发布时间:2019-06-19

本文共 7624 字,大约阅读时间需要 25 分钟。

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

转载于:https://blog.51cto.com/13166529/2051210

你可能感兴趣的文章
引入间接隔离变化(三)
查看>>
统一沟通-技巧-4-让国内域名提供商“提供”SRV记录
查看>>
cocos2d-x 3.0事件机制及用户输入
查看>>
比亚迪速锐F3专用夏季座套 夏天坐垫 四季坐套
查看>>
C++ 数字转换为string类型
查看>>
取证学习资料DVD
查看>>
Sublime Text 格式化代码快捷键
查看>>
疯狂的 Web 应用开源项目
查看>>
程序员全国不同地区,微信(面试 招聘)群。
查看>>
【干货】界面控件DevExtreme视频教程大汇总!
查看>>
用户管理脚本之删除用户——Delete_user.sh
查看>>
前端重构实践(二) —— 模块化开发
查看>>
ssh 与 locale
查看>>
将Flash 嵌入WPF 程序
查看>>
《自己动手做交互系统》——2.3 制作过程
查看>>
闭包 !if(){}.call()
查看>>
关闭CentOS6启动进度条,显示详细自检信息
查看>>
垂直居中的几种实现方法
查看>>
CSS定位
查看>>
我的友情链接
查看>>