动态sql
环境搭建
- 随机生成id工具
public static String getId(){
return UUID.randomUUID().toString().replaceAll("-","");
}
- 接口类
public interface BlogMapper {
int addBlog(Blog blog);
}
- Mapper
<mapper namespace="com.skl.dao.BlogMapper">
<insert id="addBlog" parameterType="blog">
insert into mybatis.blog (id,title,author,create_time,views)
values (#{id},#{title},#{author},#{createTime},#{views})
</insert>
</mapper>
- test
@Test
public void addBlogTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setId(IDutils.getId());
blog.setTitle("Mybatis如此困难");
blog.setAuthor("小黑屁");
blog.setCreateTime(new Date());
blog.setViews(9998);
}