​前言:​

​一、MyBatis简介:​

​二、MyBatis的HelloWord​

​1.导入相关的包资源​

​2.添加主配置文件,设置数据库连接信息:​

​3.添加实体类User.java:​

​4.需要添加domain文件映射信息.(添加一个UserMapper.xml)​

 

​扩展:监控SQL语句:​

​三、常见错误排查:​

​四、基本的增删查改操作:​

​文件配置​

​对象更新:​

​对象查询:get一条数据​

​查询所有:​

​对象删除:​



前言:

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_java

一、MyBatis简介:

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Ordinary Java Objects,普通的 Java 对象)映射成数据库中的记录。

MyBatis的前身是iBatis,MyBatis在iBatis的基础上面,对代码结构进行了大量的重构和简化;

二、MyBatis的HelloWord

1.导入相关的包资源

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_java_02

2.添加主配置文件,设置数据库连接信息:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis01/UserMapper.xml"/>
</mappers>
</configuration>

3.添加实体类User.java:

 

4.需要添加domain文件映射信息.(添加一个UserMapper.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis01.UserMapper">
<insert id="save" keyProperty="id" parameterType="User" useGeneratedKeys="true">
INSERT INTO user(username,password,age) values (#{username},#{password},#{age})
</insert>
<update id="update" parameterType="User">
update user set username=#{username},password=#{password},age=#{age} where id = #{id}
</update>
<select id="get" parameterType="java.lang.Long" resultType="User">
select id,username,password,age from user where id =#{id}
</select>
</mapper>

UserMapper映射文件:

Notes:

1.insert方法这里的value的值必须和User的set、get方法设置的参数一致(否则报错)

2.自动增长的ID要在这里配置:

 keyProperty="id"  useGeneratedKeys="true"

3.此处命名需要特别注意使用UserMapper,后面使用Mapper接口实现

 

扩展:监控SQL语句:

由于倒入log4j jar包便于显示

1.在资源目录下添加log4j.properties文件.添加如下配置.

namespace的包名或者父包名(需要替换好

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com._520it.mybatis.day01.hello=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

三、常见错误排查:

org.apache.ibatis.exceptions.PersistenceException: 

### Error updating database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis01.UserMapper.save

### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for 

1.数据库(版本不一也可能导致报错)优先查看!!!

2.检查文件配置是否均正确

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_xml_03

我在mybatis-config.xml漏了下面这句话导致报错

Notes:

<mappers>

        <mapper resource="com/mybatis01/UserMapper.xml"/>

 </mappers>

否则会报错:

org.apache.ibatis.exceptions.PersistenceException: 

### Error updating database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis01.UserMapper.save

### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for 

 

四、基本的增删查改操作:

文件配置:

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_xml_04

对象更新:

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_JSP-MyBatis入门_05

对象查询:get一条数据

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_JSP-MyBatis入门_06

查询所有:

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_java_07

对象删除:

JSP-MyBatis(一)入门以及org.apache.ibatis.exceptions.PersistenceException: 报错_后端_08