1.使用XML或Java代码方式构建SqlSessionFactory
2.从SqlSessionFactory中获取SqlSession
XXXMapper mapper = session.getMapper(XXXMapper.class);
mapper.method()来操作数据库
3.映射SQL语句:
<mapper namespace="你的类的限定名:包名.类名">
<语句 id="引用的名称" 其他属性>
这里写上sql语句,结尾没有分号
</语句>
</mapper>
4.当然,操作数据库之前你的数据库必须要有相关的表。
5.为mybatis插入一个对象:
package org.yang.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.yang.beans.Article;
public interface ArticleMapper
{
@Insert("INSERT INTO articles (typeClass, content, author, title) VALUES (#{typeClass}, #{content}, #{author}, #{title})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int addNewArticle(Article article);
}
切记:在关闭session之前要session.commit();
956

被折叠的 条评论
为什么被折叠?



