package cn.tedu; import cn.tedu.dao.DeptMapper; import cn.tedu.pojo.Dept; import cn.tedu.pojo.User; 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.jupiter.api.Test; import java.io.InputStream; import java.util.Iterator; import java.util.List; public class Test1 { @Test //完成对象关系映射 public void get() throws Exception{ /*获取全局配置文件*/ InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); /*创建 new SqlSessionFactory工厂*/ SqlSessionFactory session = new SqlSessionFactoryBuilder().build(in); /*执行sql语言,开启会话工厂,线程不安全*/ SqlSession session1 = session.openSession(); //定位sql(namespace="" 与id 的值),并执行 //selectone执行查询语句的sql,并返回一个结果 List<User> list =session1.selectList("userMapper.getByName","xiongda"); for (User user : list) { System.out.println(user); } /* Object o = session1.selectOne("deptMapper.getByName"); System.out.println(o);*/ /*接口定位sql的方法*/ /*获取指定接口*/ DeptMapper mapper = session1.getMapper(DeptMapper.class); /*调用接口里的方法*/ Dept D = mapper.getById(); System.out.println(D); //传入参数映射文件中是#{字段名} List<Dept> L = mapper.getByName("java教研部"); /*List集合的遍历*/ Iterator<Dept> it = L.iterator(); /*it.hasNext():是否有下一个数据,有则返回true*/ while (it.hasNext()){ /*it.next():将获取到的数据给dept*/ Dept dept = it.next(); System.out.println(dept); } /*for (Dept dept : L) { System.out.println(L); }*/ Dept dept = new Dept(); dept.setId(null).setDname("php").setLoc("北京"); mapper.set(dept); session1.commit(); int i = mapper.delete(new int[]{4,5}); session1.commit(); System.out.println(i); } }
ssm中mybatis 的动态SQL写法
最新推荐文章于 2023-06-30 15:41:02 发布