• 测试文件


    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();

    }






    }

  • 相关阅读:
    perl的文件操作(2)
    使用委派调用对象的方法
    C#GDI+ TextureBrush画刷
    C#GDI+的PathGradientBrush类的使用
    C#异常对象和多catch块
    tr使用
    vim sed删除前N个字符 删除空行等 [转]
    C#检索线程状态
    C#理解线程
    C语言字符串函数大全
  • 原文地址:https://www.cnblogs.com/lijun6/p/10586065.html
Copyright © 2020-2023  润新知