本系列博文包含 h5全栈 java全栈Python基础、前端开发、Web框架、缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!!

测试文件

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

}






}

posted on 2019-03-23 22:43  李俊互联网技术传播者  阅读(195)  评论(0编辑  收藏  举报