目录
回顾MyBatis如何操作数据库
我们先来回顾一下MyBatis操作数据库的一条线。
1.先在dao层写方法名
public interface UserDao {
List<User> getUserList();
}
2.在具体的xml文件中指定方法id和返回值类型等,写入sql语句
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.lt.dao.UserDao">
<!--select查询语句-->
<select id="getUserList" resultType="com.lt.pojo.User">
select * from mybatis.user
</select>
</mapper>
3.测试类中执行查询,有参则传参
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//方式一:getMapper
UserDao userDao = sqlSession.getMapper(UserDao.class);

本文详细介绍了MyBatis使用XML进行数据库操作的方法,包括namespace的设定、Mapper.xml属性解析、增删改操作的注意事项,以及如何利用万能Map进行灵活的参数传递。此外,还强调了在进行模糊查询时应避免直接在SQL中使用百分号以防止SQL注入风险。
最低0.47元/天 解锁文章
971

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



