开始
1. 构造出两张表
create table if not exists proper(
id int(8) primary key ,
name char(16) not null ,
meg char(32) not null
);
create table if not exists home (
id int(8) primary key ,
name char(16) not null ,
master int(8) not null ,
foreign key (master) references proper(id)
);2.在接口中增加一个方法
public int addHome(Home home);3.在mapper配置文件中编写对应的标签
<insert id="addHome" parameterType="com.example.mybatis.Home">
insert into home (id, name, master) value (#{id},#{name},#{proper.id})
</insert>4. 测试
@Test
void conTextLoadsHome() throws IOException {
SqlSessionFactory sqlSessionFactory=SessionFactory.getSessionFactory();
try (SqlSession sqlSession=sqlSessionFactory.openSession()){
t1Mapper mapper = sqlSession.getMapper(t1Mapper.class);
int i = mapper.addHome(new Home(10, "home10", new Proper(1, "a1", "10")));
System.out.println(i);
sqlSession.commit();
}
}注意
- 在xml中使用对象中的对象的属性的时候,用对象.属性如:#{proper.id})
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!