|
|
package com.example.web;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
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.jupiter.api.Test;
|
|
|
|
|
|
public class UserDaoTest {
|
|
|
|
|
|
@Test
|
|
|
public void insert() throws IOException {
|
|
|
//用PrintWriter架设一个管道,到mybatis-config.xml文件
|
|
|
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
|
|
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
|
|
SqlSession sqlSession = sessionFactory.openSession();
|
|
|
|
|
|
sqlSession.insert("insert",new User1(1,"杨星","123"));
|
|
|
sqlSession.commit();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void updateTest() throws IOException {
|
|
|
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
|
|
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
|
|
SqlSession sqlSession = sessionFactory.openSession();
|
|
|
|
|
|
sqlSession.update("updateUser",new User1(3,"小俊俊","123"));
|
|
|
sqlSession.commit();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void deleteTest() throws IOException {
|
|
|
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
|
|
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
|
|
SqlSession sqlSession = sessionFactory.openSession();
|
|
|
|
|
|
sqlSession.delete("deleteUser",new User1(2));
|
|
|
sqlSession.commit();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void seletTest() throws IOException {
|
|
|
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
|
|
|
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
|
|
|
SqlSession sqlSession = sessionFactory.openSession();
|
|
|
|
|
|
List<Object> userList = sqlSession.selectList("selectAll");
|
|
|
for (Object o : userList) {
|
|
|
System.out.println(o);
|
|
|
}
|
|
|
|
|
|
User1 user = sqlSession.selectOne("selectUserById", 1);
|
|
|
System.out.println(user);
|
|
|
sqlSession.commit();
|
|
|
}
|
|
|
}
|