package com.icss.mybaties.Pojo;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
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;
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("------------------------------");
System.out.println(user);
System.out.println("------------------------------");
//String t=user.getTeacher().getJob();
//System.out.println(t);
List<student> list=user.getStudentList();
for(student s:list){
System.out.println("**"+s+"**");
}
session.close();
}
///
public static void insertT(){
SqlSession session=ssf.openSession();
TestTable ci=new TestTable();
ci.setMyname("测试");
session.insert("com.icss.mybaties.Pojo.UserDao.insertT", ci);
session.commit();
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();
insertT();
}
}
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
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;
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("------------------------------");
System.out.println(user);
System.out.println("------------------------------");
//String t=user.getTeacher().getJob();
//System.out.println(t);
List<student> list=user.getStudentList();
for(student s:list){
System.out.println("**"+s+"**");
}
session.close();
}
///
public static void insertT(){
SqlSession session=ssf.openSession();
TestTable ci=new TestTable();
ci.setMyname("测试");
session.insert("com.icss.mybaties.Pojo.UserDao.insertT", ci);
session.commit();
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();
insertT();
}
}