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测试范例
原创CAIHONGSHIJIE6 博主文章分类:mybatis ©著作权
©著作权归作者所有:来自51CTO博客作者CAIHONGSHIJIE6的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
mybatis中crud操作范例
1 2 4 5 6 7 8 9 10 12 INSERT INTO 13 course 14 ( 15 classification_id, 16 course_name, 17 ...
mybatis.mysql SQL sed java 二级 -
MyBatis 集合操作语法范例:配合SQL的in关键字
Java语法: MyBatis语法
java语法 java