学习 Java + MyBatis 可以分为「基础 → 实战 → 进阶」三个阶段,下面给你一条比较清晰、可落地的学习路线。
在学 MyBatis 之前,建议先掌握:
知道 JDBC 的“麻烦”,才能理解 MyBatis 为什么好用。
建议第一个项目:
user 表User 实体类UserMapperSqlSessionFactorySqlSessionMapper 接口Mapper.xmlmybatis-demo
├── User.java
├── UserMapper.java
├── UserMapper.xml
└── mybatis-config.xml
✅ XML 方式(推荐新手)
<select id="selectById" resultType="User">
select * from user where id = #{id}
</select>
✅ 注解方式(简单 SQL)
@Select("select * from user where id = #{id}")
User selectById(Integer id);
#{} 和 ${} 区别(重点)parameterTyperesultType / resultMap<resultMap id="userMap" type="User">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
</resultMap>
非常常用:
<if><where><foreach><choose><select id="list" resultType="User">
select * from user
<where>
<if test="name != null">
and name = #{name}
</if>
</where>
</select>
现在企业基本都用 MyBatis + Spring Boot
mybatis-spring-boot-starter@Mapper / @MapperScanapplication.yml 配置mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Transactional✅ 不要只看教程
✅ 每学一个知识点,立刻写代码
✅ 先 XML,再注解
✅ 先单体,再整合 Spring Boot
如果你愿意,我可以:
你现在学到哪一步了?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。