### 修改UserMapper
```java
package com.gosuncn.dao;
import com.gosuncn.entity.User;
public interface UserMapper {
void saveUser(User user);
}
```
### 修改UserMapper.xml
```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.gosuncn.dao.UserMapper">
<insert id="saveUser" parameterType="com.gosuncn.entity.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select last_insert_id()
</selectKey>
insert into user (name, age, address) values (#{name}, #{age}, #{address})
</insert>
</mapper>
```
### 修改MainTest
```java
package com.gosuncn;
import com.gosuncn.dao.UserMapper;
import com.gosuncn.entity.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 java.io.InputStream;
public class MainTest {
public static void main(String[] args) throws Exception {
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setName("ABC");
user.setAge(26);
user.setAddress("BeiJin");
System.out.println(user);
userMapper.saveUser(user);
System.out.println(user);
sqlSession.commit();
sqlSession.close();
inputStream.close();
}
}
```
> user对象在保存前和保存后的id字段,一个是null,一个有值。
- springboot使用mybatis
- MyBatis入门
- MyBatis注解方式
- 从零开始MyBatis保存操作
- MyBatis更新操作
- MyBatis删除操作
- MyBatis根据Id查询用户
- MyBatis根据名称模糊查询用户1
- MyBatis根据名称模糊查询用户2
- MyBatis查询总记录条数
- MyBatis获取插入时自增的Id
- MyBatis传参类型
- MyBatis返回值字段不匹配
- MyBatis之properties标签
- MyBatis类型别名typeAliases
- mybatis逆向工程maven之pom配置
- configurationFile
- verbose
- overwrite
- includeCompileDependencies
- includeAllDependencies
- maven之pom配置总结版
- 逆向工程generatorConfig配置