1.主键回填
位置:接口映射文件
1)方法传递对象
1)开启主键回填useGeneratedKeys="true"
2)指定主键回填的那个属性keyProperty
2)方法传递map,把id放到了map里面
1.创建项目
2.建包建配置文件
com.qf.entity
com.qf.dao
com.qf.mapper
com.qf.utils
mybatis-config.xml
3.con.qf.entity包中创建实体类
package com.qf.entity;
public class User {
private Integer id;
private String name;
private String password;
private String isAdmin;
public User(String name, String password) {
this.name = name;
this.password = password;
}
public User() {
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(String isAdmin) {
this.isAdmin = isAdmin;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", isAdmin=" + isAdmin + "]";
}
}
4.con.qf.dao包中创建接口
package com.qf.dao;
import java.util.Map;
import com.qf.entity.User;
public interface IUserDao {
public void add(User user);
public void insert(Map<String, Object> map);
}
5.配置jdbc.properties配置文件
jdbc.url =jdbc:mysql://localhost:3306/1707_hibernate
jdbc.name=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
配置mybatis-config.xml配置文件
<?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>
<!-- 加载属性文件
resource:本地资源文件
-->
<properties resource="jdbc.properties"></properties>
<typeAliases>
<package name="com.qf.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.name}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
</configuration>
6.com.qf.mapper包创建接口的映射文件
<?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.qf.dao.IUserDao">
<!--
useGeneratedKeys:开启主键回填
keyProperty:回填到那个属性
-->
<insert id="add" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into t_user (name,password) values(#{name},#{password})
</insert>
<!-- 传递Map返回主键 -->
<insert id="insert" parameterType="map" useGeneratedKeys="true" keyProperty="id" >
insert into t_user (name,password) values(#{username},#{password})
</insert>
</mapper>
7.在mybatis-config.xml里面读取Mapper文件
<!-- Mapper文件 -->
<mappers>
<mapper resource="com/qf/mapper/IUserDao.xml" />
</mappers>
8.com.qf.util工具类
package com.qf.utils;
import java.io.IOException;
import java.io.InputStream;
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.xml.sax.InputSource;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getSqlSessionFactory() {
if (sqlSessionFactory == null) {
initSqlSessionFactory(); // 为空再去参加
}
return sqlSessionFactory;
}
private static void initSqlSessionFactory() {
InputStream ips = null;
try {
// 1.指定配置文件
ips = Resources.getResourceAsStream("mybatis-config.xml");
// 2.赋值操作
synchronized (MyBatisUtil.class) {
if (sqlSessionFactory == null) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ips != null) {
try {
ips.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
9.测试类---》测试 id的值有没有回填
package com.qf.dao;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.qf.entity.User;
import com.qf.utils.MyBatisUtil;
public class UserDaoTest {
@Test
public void testAdd() {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
User user = new User("王五", "xxxxx");
userDao.add(user);
System.out.println(user.getId());
sqlSession.commit();
sqlSession.close();
}
@Test
public void testInsert() {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
Map<String, Object> map = new HashMap<String,Object>();
map.put("username", "admin123");
map.put("password", "xxxx123");
userDao.insert(map);
System.out.println(map.get("id"));
sqlSession.commit();
sqlSession.close();
}
}
运行结果:id的值会填了。