第⑤项配置:数据库映射EmpMapper.xmlselect * from emp;insert into emp(empno,ename) values(#{empno},#{ename});delete from emp where empno = #{empno};update emp set ename = #{ename} where empno=#{empno};
文章插图
第⑥项配置:测试mybatis的CRUD方法public class MybatisTest {SqlSessionFactory sqlSessionFactory;SqlSession sqlSession;@Before //加载mybatis.xml配置文件 , 获取sqlSession会话public void before() throws IOException {//调用test方法前都会首先执行此方法String resource = "mybatis.xml";//读取项目中的mybatis.xml文件InputStream inputStream = Resources.getResourceAsStream(resource);//创建一个SqlSessionFactory的工厂类sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//创建SqlSession会话sqlSession= sqlSessionFactory.openSession(true); //默认为手动提交false,自动提交true 。}@Testpublic void testQueryEmpList(){//测试查询EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);List empList =empMapper.queryEmpList();System.out.println(empList); //输出观察:最好重写Emp实体中的toString方法}@Testpublic void testAddEmp(){//测试添加EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);Emp emp = new Emp();emp.setEmpno(9999);emp.setEname("cnlm");int result = empMapper.addEmp(emp);System.out.println(result);//如果添加成功 , 返回1 。 注意:mybatis默认是手动提交 , 我们需要修改sqlSessionFactory.openSession(true);}@Testpublic void testDelEmp(){//测试删除EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);int result = empMapper.delEmp(9999);System.out.println(result);//如果删除成功 , 返回1 。 注意:mybatis默认是手动提交 , 我们需要修改sqlSessionFactory.openSession(true);}@Testpublic void testUpdateEmp(){//测试修改EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);Emp emp = new Emp();emp.setEmpno(9999);emp.setEname("菜鸟联盟");int result = empMapper.updateEmp(emp);System.out.println(result);//如果修改成功 , 返回1 。 注意:mybatis默认是手动提交 , 我们需要修改sqlSessionFactory.openSession(true);}}注意:测试报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cnlm.mapper.EmpMapper.queryEmpList是因为:idea默认找不到对应的xml配置路径 , 在pom文件的build标签中 , 加入resource标签如下代码:
src/main/java**/*.xml如上是简单入门配置 , 后续会陆续更新系列文章 , 记得关注我 。 谁在最需要的时候轻轻拍着我肩膀 , 谁在最快乐的时候愿意和我分享 。 我是一个包夜敲代码 , 想靠技术苟且的程序员 。 如果觉得有点用的话 , 请毫不留情地关注、点赞、转发 。 这将是我写出更多优质文章的最强动力!
推荐阅读
-
明星八卦|20年的青春在TVB度过,却一直不温不火,来到内地后一炮而红
-
一个体育黑|2个月后乔丹与妻子结婚,乔丹分手信被拍出2.5万美元!写于31年前
-
-
萌娃出生18天被“一吻而亡”,新生儿要远离4类人接触
-
证监会网站|证监会:暂缓蚂蚁集团上市是对投资者和市场负责任的做法
-
听歌打娱@姚晨评价余欢水:集万千霉头于一身的苦命人,《我是余欢水》明晚开播
-
第一财经|陆挺:“双循环”战略有三大实现途径,帮中国有效应对疫情冲击
-
-
『界面新闻』电商MCN受追捧,“构美”获得近亿元A轮融资
-
人民网|《关于促进石油成品油流通高质量发展的意见》将于近期出台
-
追剧小能手|却因黑料退圈,16年后终翻身,张含韵15岁爆红被刘德华宠上天
-
亡国|明末清初三大思想家每人一句话,发出时代最强音
-
『武磊』武磊迎来噩耗!西甲联赛确定重开 西班牙人队铁定降级
-
湖人|又一巨星或考虑加盟湖人!4年1.96亿比詹皇还贵,佩总豪赌3巨头?
-
中国青年网▲白衬衣牛仔裤似少女,周慧敏海边上演侧颜杀
-
-
金蜜蜂CSR@准备屯粮食的注意了|既想屯点心里踏实又不想浪费的?
-
-
新疆喀什新增无症状感染者137例,广西疾控中心紧急提醒
-
紫包菜和紫甘蓝是一样的吗 紫包菜和紫甘蓝是一样的吗