开始

1. 在接口中增加一个方法

public int addT1Item(t1Item item);

2. 在mapper配置文件中编写对应的标签

<insert id="addT1Item" parameterType="com.example.mybatis.t1Item">
        insert into t1 (id, name, password) value (#{id},#{name},#{password})
 </insert>

3.测试

@Test
	void conTextLoads() throws IOException {
		SqlSessionFactory sqlSessionFactory=SessionFactory.getSessionFactory();
		try (SqlSession sqlSession=sqlSessionFactory.openSession()){
			t1Mapper mapper = sqlSession.getMapper(t1Mapper.class);
			int i = mapper.addT1Item(new t1Item(100, "t100", "a100"));
			sqlSession.commit();
			System.out.println(i);
		}
	}

注意

  • 使用对象作为参数时,在xml配置文件中使用里面的属性与直接使用map中的键值对一样的。#{name}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

基本用法 上一篇
序对应用 下一篇