调用方法2

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值