• MyBatis crud操作


    Test2.java

    package com.mycom.mybatis_1.crud;
    
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.junit.Test;
    
    import com.mycom.mybatis_1.bean.User;
    import com.mycom.mybatis_1.util.MyBatisUtils;
    
    public class Test2 {
        
        @Test
        public void testAdd(){
            SqlSessionFactory factory = MyBatisUtils.getFactory();
            //默认是手动提交的
            SqlSession session = factory.openSession();
            
            String statement = "com.mycom.mybatis_1.bean.userMapper.addUser";
            int ins = session.insert(statement, new User(-1,"KK",23, "男"));
            //提交
            session.commit();
            session.close();
            
            System.out.println(ins);
        }
        
        @Test
        public void testUpd(){
            SqlSessionFactory factory = MyBatisUtils.getFactory();
            //默认是手动提交的
    //        SqlSession session = factory.openSession();
    //        
    //        String statement = "com.mycom.mybatis_1.bean.userMapper.updUser";
    //        int ins = session.update(statement, new User(4,"KK444",27));
    //        //提交
    //        session.commit();
            
            //默认是自动提交的
            SqlSession session = factory.openSession(true);
            
            String statement = "com.mycom.mybatis_1.bean.userMapper.updUser";
            int upd = session.update(statement, new User(4,"KK444",28, "男"));
            session.close();
            System.out.println(upd);
        }
        
        @Test
        public void testDel(){
            SqlSessionFactory factory = MyBatisUtils.getFactory();
            //默认是自动提交的
            SqlSession session = factory.openSession(true);
            
            String statement = "com.mycom.mybatis_1.bean.userMapper.delUser";
            int del = session.delete(statement, 4);
            session.close();
            System.out.println(del);
        }
        
        @Test
        public void testgetUser(){
            SqlSessionFactory factory = MyBatisUtils.getFactory();
            //默认是自动提交的
            SqlSession session = factory.openSession(true);
            
            String statement = "com.mycom.mybatis_1.bean.userMapper.getUser";
            User user = session.selectOne(statement, 1);
            session.close();
            System.out.println(user);
        }
        
        @Test
        public void testgetAll(){
            SqlSessionFactory factory = MyBatisUtils.getFactory();
            //默认是自动提交的
            SqlSession session = factory.openSession(true);
            
            String statement = "com.mycom.mybatis_1.bean.userMapper.getAllUsers";
            List<User> list = session.selectList(statement);
            session.close();
            System.out.println(list);
        }
    
    }
    MyBatisUtils.java
    package com.mycom.mybatis_1.util;
    
    import java.io.InputStream;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import com.mycom.mybatis_1.select.Test1;
    
    public class MyBatisUtils {
        
        public static SqlSessionFactory getFactory(){
            String resource = "conf.xml";
            InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            return factory;
        }
    
    }
  • 相关阅读:
    Jenkins自动化多项目编译和Tomcat部署懒人终极大招
    python 装饰器总结
    selenium3之-测试环境搭建
    centos7.4 安装ftp服务器并配置匿名用户权限
    selenium3之-运行原理
    flutter 打包apk
    Fluwx:微信SDK在Flutter上的实现
    flutter 购物车功能
    flutter sharesdk实现跨平台分享
    Web API接口设计经验总结
  • 原文地址:https://www.cnblogs.com/mingforyou/p/5341284.html
走看看 - 开发者的网上家园