1:查询
报错信息1
1 org.apache.ibatis.exceptions.PersistenceException: 2 ### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.itheima.pojp.Employee.EmselectOne'. It's likely that neither a Result Type nor a Result Map was specified. 3 ### The error may exist in mapper/EmployeeMapper.xml 4 ### The error may involve com.itheima.pojp.Employee.EmselectOne 5 ### The error occurred while handling results 6 ### SQL: select * from employee where id = 1 7 ### Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statemen
原因:书写select标签时没有指定 resultType="com.itheima.pojp.Employee" 该属性
解决方法:在<select标签>里添加resultType属性即可,因为返回的是Employee类型的数据。
1 <select id="EmselectOne" parameterType="int">
2 select * from employee where id = ${id}
3 </select>
报错信息2
1 ### Error building SqlSession. 2 ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 27; columnNumber: 17; 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"。
原因在mybatis的核心配置文件里写了两个mapper标签 导致报错
1 <mappers> 2 <mapper resource="mapper/UserMapper.xml"/> 3 </mappers> 4 5 <mappers> 6 <mapper resource="mapper/EmployeeMapper.xml"></mapper> 7 </mappers>
解决方法:1:注释掉一个mappers 2:再重新写一个核心配置文件。只能存在一个mappers标签。
当执行update,insert, delete操作时需要插入sqlSession.commit(); 这行代码否则不会执行成功,执行完毕之后记得关闭连接
1 @Test //查询一条 2 public void testEmSelectOne() throws IOException { 3 String str = "mybatis-config.xml"; 4 Reader reader = Resources.getResourceAsReader(str); 5 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader); 6 SqlSession sqlSession = build.openSession(); 7 Employee em = sqlSession.selectOne("EmselectOne", 1); 8 System.out.println(em); 9 }
映射文件配置:
<select id="EmselectAll" resultType="com.itheima.pojp.Employee">
select * from employee
</select>
2:插入
1 @Test //插入一条 2 public void testInsertOne() throws IOException { 3 String str = "mybatis-config.xml"; 4 Reader reader = Resources.getResourceAsReader(str); 5 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader); 6 SqlSession sqlSession = build.openSession(); 7 Employee em = new Employee(4, "hahaha", 15, "冲裁"); 8 int eminsertOne = sqlSession.insert("EminsertOne", em); 9 sqlSession.commit(); //插入数据需要提交否则插入不成功 10 sqlSession.close(); 11 System.out.println(eminsertOne); 12 }
映射文件配置
<insert id="EminsertOne" parameterType="com.itheima.pojp.Employee">
insert into employee(id, name, age, position) values (#{id}, #{name}, #{age}, #{position})
</insert>
3:修改
1 public void testUpdateOne() throws IOException { 2 String str = "mybatis-config.xml"; 3 Reader reader = Resources.getResourceAsReader(str); 4 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader); 5 SqlSession sqlSession = build.openSession(); 6 Employee employee = new Employee(); 7 employee.setName("熊大"); 8 employee.setId(7); 9 int emupdateOne = sqlSession.update("EmupdateOne", employee); 10 sqlSession.commit(); 11 sqlSession.close(); 12 System.out.println(emupdateOne); 13 }
映射文件配置:
<update id="EmupdateOne" parameterType="com.itheima.pojp.Employee">
update employee set name = #{name} where id = #{id}
</update>
4:删除
1 @Test //删除一条 2 public void testDeleteOne() throws IOException { 3 String str = "mybatis-config.xml"; 4 Reader reader = Resources.getResourceAsReader(str); 5 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader); 6 SqlSession sqlSession = build.openSession(); 7 int emdeleteOne = sqlSession.delete("EmdeleteOne", 5); 8 System.out.println(emdeleteOne); 9 sqlSession.commit(); 10 sqlSession.close(); 11 }
映射文件配置:
<delete id="EmdeleteOne" parameterType="int">
delete from employee where id = #{id}
</delete>