zoukankan      html  css  js  c++  java
  • 测试文件

    package cn.lijun.mybaits.junit;

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

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;

    import cn.lijin.mybatis.pojo.User;

    public class MybatisFirstTest {
    @Test
    public void testMybatis() throws IOException{
    //加载核心配置文件
    String resource ="sqlMapConfin.xml";
    InputStream resourceAsStream = Resources.getResourceAsStream(resource);

    //创建SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //创建会话
    System.out.println("1");
    SqlSession openSession = sqlSessionFactory.openSession();
    //执行sql 语句
    User user = openSession.selectOne("jun.findUserById", 1);
    System.out.println(user);

    }
    // 根据用户模糊查询列表

    @Test
    public void testFindUserByUsername() throws IOException{
    // 加载核心配置文件
    String resource = "sqlMapConfin.xml";
    System.out.println(resource);
    InputStream resourceAsStream = Resources.getResourceAsStream(resource);
    //创建工厂
    SqlSessionFactory sqls = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //创建会话
    SqlSession openSession = sqls.openSession();
    //执行语句
    List<User> user = openSession.selectList("jun.findUserByUsername", "明");
    for(User l:user){
    System.out.println(l);
    }
    }
    //添加数据
    @Test
    public void testInsertUser() throws IOException{
    //再次加载核心配置文件
    String resource ="sqlMapConfin.xml";
    InputStream resourceAsStream = Resources.getResourceAsStream(resource);
    //创建 工厂
    SqlSessionFactory sqls = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //创建SqlSession
    SqlSession openSession = sqls.openSession();
    //执行语句
    User lijun = new User();
    lijun.setUsername("qy95");
    lijun.setBirthday(new Date());
    lijun.setAddress("郑州");
    lijun.setSex("男");
    openSession.insert("jun.inserUser",lijun);
    openSession.commit();
    System.out.println(lijun.getId());
    }

    //更新

    @Test
    public void testUpdateById() throws IOException{
    //再次加载核心配置文件
    String resource ="sqlMapConfin.xml";
    InputStream resourceAsStream = Resources.getResourceAsStream(resource);
    //创建 工厂
    SqlSessionFactory sqls = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //创建SqlSession
    SqlSession openSession = sqls.openSession();
    //执行语句
    User lijun = new User();
    lijun.setId(1);
    lijun.setUsername("qy95");
    lijun.setBirthday(new Date());
    lijun.setAddress("郑州");
    lijun.setSex("男");
    openSession.update("jun.lijun666",lijun);
    openSession.commit();

    }
    //删除
    @Test
    public void testUpdateById1() throws IOException{
    //再次加载核心配置文件
    String resource ="sqlMapConfin.xml";
    InputStream resourceAsStream = Resources.getResourceAsStream(resource);
    //创建 工厂
    SqlSessionFactory sqls = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //创建SqlSession
    SqlSession openSession = sqls.openSession();
    openSession.delete("jun.deleteUser",23);
    openSession.commit();

    }






    }

  • 相关阅读:
    C# 中的委托和事件
    POJ题目分类
    A*算法
    Poj 1077 eight(BFS+全序列Hash解八数码问题)
    Poj 2304 Combination Lock(模拟顺、逆时钟开组合锁)
    Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)
    Poj 2328 Guessing Game(猜数字游戏)
    Poj 2403 Hay Points(Map)
    Poj 1338 Ugly Numbers(数学推导)
    Poj 1504 Adding Reversed Numbers(用字符串反转数字)
  • 原文地址:https://www.cnblogs.com/lijun6/p/10586065.html
Copyright © 2011-2022 走看看