栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis实例(mybatis使用详解)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis实例(mybatis使用详解)

Executor架构

Caching Executor的作用就是实现二级缓存,使用的是装饰者模式。

装饰者模式:在不改变原有类继承结构的情况下,新建一个对象来扩展原有功能。

Executor具体实现

maven依赖


     org.mybatis 
     mybatis 
     3.5.7 


    mysql
    mysql-connector-java
    8.0.20

public interface UserMapper {

    @Select("select name from user where id = #{id}")
    String selectById(@Param("id") int id);

    @Update("update user set name=#{arg1} where id=#{arg0}")
    void updateById(int id,String name);
}
public class ExecutorTest {

    private Configuration configuration;

    private Connection connection;

    private JdbcTransaction jdbcTransaction;

    private  MappedStatement ms;

    @Before
    public void init() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        configuration = factory.getConfiguration();
        connection = DriverManager.getConnection(JDBC.URL,JDBC.USERNAME,JDBC.PASSWORD);
        jdbcTransaction = new JdbcTransaction(connection);
        ms = configuration.getMappedStatement("dao.UserMapper.selectById");
    }

    
    @Test
    public void simpleTest() throws Exception {
        SimpleExecutor executor = new SimpleExecutor(configuration,jdbcTransaction);

        List list = executor.doQuery(ms,1,                 
           RowBounds.DEFAULT,SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        executor.doQuery(ms,1, 
           RowBounds.DEFAULT,SimpleExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        System.out.println(list.get(0));
    }

    
    @Test
    public void reuseTest() throws Exception {
        ReuseExecutor executor = new ReuseExecutor(configuration,jdbcTransaction);
        MappedStatement ms = 
             configuration.getMappedStatement("dao.UserMapper.selectById");
        List list = executor.doQuery(ms,1, 
             RowBounds.DEFAULT,ReuseExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        executor.doQuery(ms,1, 
             RowBounds.DEFAULT,ReuseExecutor.NO_RESULT_HANDLER,ms.getBoundSql(1));
        System.out.println(list.get(0));
    }

    
    @Test
    public void batchTest() throws Exception {
        BatchExecutor executor = new BatchExecutor(configuration,jdbcTransaction);
        MappedStatement ms = 
          configuration.getMappedStatement("dao.UserMapper.updateById");
        Map param = new HashMap();
        param.put("arg0",1);
        param.put("arg1","rrrteeeo");
        Map param1= new HashMap();
        param1.put("arg0",1);
        param1.put("arg1","rrrcco");
        executor.doUpdate(ms,param);
        executor.doUpdate(ms,param1);
        executor.doFlushStatements(false);
    }

    
    @Test
    public void baseTest() throws Exception {
        Executor executor = new SimpleExecutor(configuration,jdbcTransaction);
        executor.query(ms,1, RowBounds.DEFAULT,Executor.NO_RESULT_HANDLER);
        executor.query(ms,1, RowBounds.DEFAULT,Executor.NO_RESULT_HANDLER);

    }
} 
 执行结果 
simpleTest:每次都会进行新的预处理 

SimpleExecutor为默认执行器

reuseTest:相同的sql只进行一次预处理

batchTest:相同的批量更新操作,会组合成一次sql处理

baseTest:调用baseExecutor的query方法(或者update方法),最终会执行子类中的doQuery方法(或者doUpdate方法)

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号