版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zw524362419/article/details/78939823
mybatis使用类型03–基于传统方式statementid方式
具体实现
- 传统的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使用泛型,即使变量使用错误,编译阶段不能暴露出错误。