03mybatis使用类型--基于传统方式statementid方式

mybatis使用类型03–基于传统方式statementid方式


具体实现

  1. 传统的statementid方式,使用时包含四个模块:pojo.java、mapper.xml、dao接口、daoImpl。
    1.1 pojo.java 如下:
@Getter
@Setter
@ToString
public class BusinessApply {
    private Integer id;
    private String name;
    private String businessNo;
    private String businessType;
    private Integer businessSum;
}
1.2 mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper  
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="businessApply">
    <select id="findByNo" parameterType="java.lang.String"
        resultType="com.cmhy.model.BusinessApply">
        select * from business_apply where business_no = #{value}
    </select>
    <select id="findByName" parameterType="java.lang.String"
        resultType="com.cmhy.model.BusinessApply">
        select * from business_apply where name like '%${value}%'
    </select>
    <select id="findIdByName" parameterType="java.lang.String"
        resultType="java.lang.Integer">
        select id from business_apply where name like #{value}
    </select>
    <update id="updateById" parameterType="int">
        update business_apply set business_sum = 12.00 where id = #{value}
    </update>
    <update id="deleteById" parameterType="int">
        delete from business_apply where id = #{value}
    </update>
</mapper>

其中namespace可以任意,但要唯一。select、update、insert和delete模块中的id要和mapper.java中的方法名一直,parameterType约定输入参数类型,resultType预定输出参数的类型。
2.3 dao接口

public interface BusinessApplyDao {
    public BusinessApply findByNo(String businessNo);
}

2.4 daoImpl

package com.cmhy.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.cmhy.model.BusinessApply;

import lombok.Setter;
@Setter
public class BusinessApplyDaoImpl implements BusinessApplyDao{
    private SqlSessionFactory sqlSessionFactory;
    @Override
    public BusinessApply findByNo(String businessNo) {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BusinessApply ba = sqlSession.selectOne("businessApply.findByNo",businessNo);
        sqlSession.close();
        return ba;
    }
    @Override
    public List<BusinessApply> findByName(String name) {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<BusinessApply>  list = sqlSession.selectList("businessApply.findByName", name);
        sqlSession.close();
        return list;
    }
    @Override
    public List<Integer> findIdByName(String name) {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Integer> list = sqlSession.selectList("businessApply.findIdByName", name);
        sqlSession.close();
        return list;
    }
    @Override
    public void updateById(Integer id) {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.update("businessApply.updateById", id);
        sqlSession.commit();
        sqlSession.close();
    }
    @Override
    public void deleteById(Integer id) {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.delete("businessApply.deleteById", id);
        sqlSession.commit();
        sqlSession.close();
    }

}

2.5 测试类

package org.cmhy.dao;

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

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cmhy.dao.BusinessApplyDaoImpl;
import com.cmhy.model.BusinessApply;

public class BusinessApplyDaoImplTest {
    private final Logger logger = LoggerFactory.getLogger(BusinessApplyDaoImplTest.class);
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void getSqlSessionFactory() throws IOException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public void findByNoTest() {
        BusinessApplyDaoImpl businessApplyDao = new BusinessApplyDaoImpl();
        businessApplyDao.setSqlSessionFactory(sqlSessionFactory);
        String returnStr = businessApplyDao.findByNo("1").toString();
        System.out.println(returnStr);

    }
    @Test
    public void findByNameTest() {
        BusinessApplyDaoImpl businessApplyDao = new BusinessApplyDaoImpl();
        businessApplyDao.setSqlSessionFactory(sqlSessionFactory);
        List<BusinessApply> list = businessApplyDao.findByName("个贷");
        list.forEach(ba->System.out.println(ba.toString()));
    }
    @Test
    public void findIdByNameTest() {
        BusinessApplyDaoImpl businessApplyDao = new BusinessApplyDaoImpl();
        businessApplyDao.setSqlSessionFactory(sqlSessionFactory);
        List<Integer> list = businessApplyDao.findIdByName("%个贷%");
        list.forEach(ba->System.out.println(ba.toString()));
    }
    @Test
    public void updateByIdTest() {
        logger.debug("updateByIdTest 开始了 ******* ");
        BusinessApplyDaoImpl businessApplyDao = new BusinessApplyDaoImpl();
        businessApplyDao.setSqlSessionFactory(sqlSessionFactory);
        businessApplyDao.updateById(1);
    }
    @Test
    public void deleteByIdTest() {
        logger.debug("updateByIdTest 开始了 ******* ");
        BusinessApplyDaoImpl businessApplyDao = new BusinessApplyDaoImpl();
        businessApplyDao.setSqlSessionFactory(sqlSessionFactory);
        businessApplyDao.deleteById(1);
    }

}

缺点

此种方式,现在基本不再使用,缺陷有以下三个:
2.1 存在大量的重复的代码。
2.2 需要实现dao和daoImpl代码
2.3 需要把statementid的id硬编码

List<Integer> list = sqlSession.selectList("businessApply.findIdByName", name);

2.4 由于sqlSession使用泛型,即使变量使用错误,编译阶段不能暴露出错误。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值