- 第一步在接口上写该接口所需要的sql语句
@Select("select * from t_userInfo")
List<User> getUserList();
- 在Mybatis核心配置文件中,通过Mapper标签指定该接口的位置
<!--绑定接口注解-->
<mappers>
<mapper class="com.shige.dao.UserMapperInterface"/>
</mappers>
@Param("id")注解
User getUserInfoById(@Param("id") String id);
基本类型的数据 和 String类型需要加上@Param()注解,我们在SQL中引用的,就是@Param()注解中设置的属性名
- #{} 是预编译的。可以防止SQL注入
- ${} 不是预编译的,不能防止sql注入
- 尽量使用#{}
Mybatis的注解本质上使用了反射机制,底层使用了动态代理。
设置事务自动提交 sqlSessionFactory.openSession(true);
原文:https://www.cnblogs.com/szqengr/p/14727141.html