MyBatis测试范例

package com.njupt.test;


import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






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 com.njupt.pojo.User;


public class Test1 {


//查询的结果一实体的形式返回
@Test
public void test1() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = session.selectOne("com.njupt.pojo.User.selectUserById","0001");

System.out.println(u.getUsername());
}

@Test
public void test6() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

session.delete("com.njupt.pojo.User.deleteUserById","0004");

session.commit();
}


@Test
public void test2() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

List<User> users = session.selectList("com.njupt.pojo.User.selectAllUsers");

for( User u : users ){
System.out.println(u.getUsername());
}
}

@Test
public void test3() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = new User();
u.setId("003");
u.setUsername("liushishi2");
u.setAddress("beijing1");

int i = session.insert("com.njupt.pojo.User.insertUser",u);
   
//一定要有,否则事务还没有提交
session.commit();

//打印数据库中受影响的行数
System.out.println(i);
}


//查询结果以map的形式返回
@Test
public void test4() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

Map m  = session.selectOne("com.njupt.pojo.User.selectUserByIdForMap","003");

System.out.println(m);

}


@Test
public void test5() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sf.openSession();

Map map = new HashMap();

map.put("id", "0004");
map.put("username1", "liuyifei1");
map.put("address", "shanghai1");

session.insert("com.njupt.pojo.User.insertUserForMap",map);

session.commit();

}

@Test
public void test7() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = new User();

u.setId("003");
u.setUsername("hjd");
u.setAddress("maoming");

session.update("com.njupt.pojo.User.updateUserById",u);
session.commit();
}


@Test
public void test8() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

Map m = new HashMap();

m.put("id", "003");
m.put("username2", "hjd2");
m.put("address", "guangdong");

session.update("com.njupt.pojo.User.updateUserByIdForMap",m);
session.commit();


}

@Test
public void test9() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = new User();

u.setId("0001");
u.setAddress("guangdong");
List<User> users = session.selectList("com.njupt.pojo.User.selectUserByCondition",u);

for( User user : users){
System.out.println(user.getUsername());
}
}

@Test
public void test10() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = new User();

u.setId("0001");
// u.setAddress("guangdong");
List<User> users = session.selectList("com.njupt.pojo.User.selectUserByCondition1",u);

for( User user : users){
System.out.println(user.getUsername());
}
}


@Test
public void test11() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sf.openSession();

User u = new User();

u.setUsername("hjd2");


List<User> users = session.selectList("com.njupt.pojo.User.selectUserByCondition1",u);

for( User user : users){
System.out.println(user.getUsername());
}
}
}

你可能感兴趣的:(mybatis)