package com.icss.mybaties.Pojo;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMap2 {
public static SqlSessionFactory ssf;
static {
String resourse = "com/icss/mybaties/conf.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resourse);
ssf = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void findid() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
User user = session.selectOne("com.icss.mybaties.Pojo.UserDao.findid", "we");
System.out.println(user);
session.close();
}
// 接受映射
public static void findid2() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
UserDao userdao = session.getMapper(UserDao.class);
System.out.println(userdao.findid("we"));
session.close();
}
// ********************************************
/// 接口注解测试
public static void addonetest() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
User ci = new User();
ci.setAge(36);
ci.setPassword("123456");
ci.setSex("55");
ci.setUser("wmrt4");
UserDao userdao = session.getMapper(UserDao.class);
userdao.addone(ci);
//session.insert("com.icss.mybaties.Pojo.UserDao.addone", ci);
User dbci=session.selectOne("com.icss.mybaties.Pojo.UserDao.findid", "wmrtm4");
System.out.println(dbci);
session.commit();
session.close();
}
//****************
public static void findme(){
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
UserDao userdao=session.getMapper(UserDao.class);
userdao.findme(2, "wmrt");
}
//******************************************
//foreach的查询
public static void testforeach(){
SqlSession session=ssf.openSession();
UserDao userdao=session.getMapper(UserDao.class);
ArrayList<String> list=new ArrayList<String>();
list.add("we");
list.add("wmrt");
list.add("wmrt2");
userdao.forfind(list);
}
// ******************************************
public static void testAMDQ() {
System.out.println("---------------testAMDQ----------------");
SqlSession session = ssf.openSession();
try {
// 创建要插入班级表的对象
User ci = new User();
ci.setAge(36);
ci.setPassword("123456");
ci.setSex("55");
ci.setUser("xm");
// 插入班级记录
session.insert("mybaties_space.add", ci);
// 验证插入是否成功
User dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
// 修改数据库
ci.setSex("ro");
ci.setPassword("blue");
session.update("mybaties_space.mod", ci);
// 验证插入是否成功
dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
session.delete("mybaties_space.del", "xm");
dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
// 正确的业务执行完之后,提交数据
session.commit();
} catch (Exception e) {
// 出现异常,必须回滚
session.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
// ******************************************
public static void main(String[] args) {
//findid();
//addonetest();
//findme();
testforeach();
}
}
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMap2 {
public static SqlSessionFactory ssf;
static {
String resourse = "com/icss/mybaties/conf.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resourse);
ssf = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void findid() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
User user = session.selectOne("com.icss.mybaties.Pojo.UserDao.findid", "we");
System.out.println(user);
session.close();
}
// 接受映射
public static void findid2() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
UserDao userdao = session.getMapper(UserDao.class);
System.out.println(userdao.findid("we"));
session.close();
}
// ********************************************
/// 接口注解测试
public static void addonetest() {
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
User ci = new User();
ci.setAge(36);
ci.setPassword("123456");
ci.setSex("55");
ci.setUser("wmrt4");
UserDao userdao = session.getMapper(UserDao.class);
userdao.addone(ci);
//session.insert("com.icss.mybaties.Pojo.UserDao.addone", ci);
User dbci=session.selectOne("com.icss.mybaties.Pojo.UserDao.findid", "wmrtm4");
System.out.println(dbci);
session.commit();
session.close();
}
//****************
public static void findme(){
System.out.println("----------------------findid测试------------------");
SqlSession session = ssf.openSession();
UserDao userdao=session.getMapper(UserDao.class);
userdao.findme(2, "wmrt");
}
//******************************************
//foreach的查询
public static void testforeach(){
SqlSession session=ssf.openSession();
UserDao userdao=session.getMapper(UserDao.class);
ArrayList<String> list=new ArrayList<String>();
list.add("we");
list.add("wmrt");
list.add("wmrt2");
userdao.forfind(list);
}
// ******************************************
public static void testAMDQ() {
System.out.println("---------------testAMDQ----------------");
SqlSession session = ssf.openSession();
try {
// 创建要插入班级表的对象
User ci = new User();
ci.setAge(36);
ci.setPassword("123456");
ci.setSex("55");
ci.setUser("xm");
// 插入班级记录
session.insert("mybaties_space.add", ci);
// 验证插入是否成功
User dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
// 修改数据库
ci.setSex("ro");
ci.setPassword("blue");
session.update("mybaties_space.mod", ci);
// 验证插入是否成功
dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
session.delete("mybaties_space.del", "xm");
dbci = session.selectOne("mybaties_space.findid", "xm");
System.out.println(dbci);
// 正确的业务执行完之后,提交数据
session.commit();
} catch (Exception e) {
// 出现异常,必须回滚
session.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
// ******************************************
public static void main(String[] args) {
//findid();
//addonetest();
//findme();
testforeach();
}
}