ToB企服应用市场:ToB评测及商务社交产业平台

标题: MyBatis入门 [打印本页]

作者: 徐锦洪    时间: 2022-10-13 13:03
标题: MyBatis入门
Mybatis

环境
SSM框架:配置文件的最好方式:看官网文档
1 简介

1.1 什么是Mybatis?

如何获得Mybatis?
1.2 持久化

数据持久化
为什么需要持久化?
1.3 持久层

复习学习过的层:Dao,Service,Controller
持久层的工作
1.4 为什么需要Mybatis?

2 第一个MyBatis程序

思路:搭建环境-->导入MyBatis-->编写代码-->测试
2.1 搭建环境

2.2 创建模块

2.3 编写代码

2.4 测试

2.5 总结

  1. public class UserDaoTest {
  2.     @Test
  3.     public void test(){
  4.         //获取SqlSession对象
  5.         SqlSession sqlSession = MybatisUtils.getSqlSession();
  6.         try{
  7. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  8. @Select("select * from mybatis.user where id=#{id}")
  9. User selectUserById(@Param("id") int id);
  10. @Select("select * from mybatis.user")
  11. List<User> selectAll();
  12. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  13. boolean insertUser(User u);
  14. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  15. boolean updateUser(User u);
  16. @Delete("delete from mybatis.user where id=#{id}")
  17. boolean deleteUser(@Param("id") int id);   //获取mapper
  18. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  19. @Select("select * from mybatis.user where id=#{id}")
  20. User selectUserById(@Param("id") int id);
  21. @Select("select * from mybatis.user")
  22. List<User> selectAll();
  23. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  24. boolean insertUser(User u);
  25. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  26. boolean updateUser(User u);
  27. @Delete("delete from mybatis.user where id=#{id}")
  28. boolean deleteUser(@Param("id") int id);   UserDao mapper = sqlSession.getMapper(UserDao.class);
  29. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  30. @Select("select * from mybatis.user where id=#{id}")
  31. User selectUserById(@Param("id") int id);
  32. @Select("select * from mybatis.user")
  33. List<User> selectAll();
  34. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  35. boolean insertUser(User u);
  36. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  37. boolean updateUser(User u);
  38. @Delete("delete from mybatis.user where id=#{id}")
  39. boolean deleteUser(@Param("id") int id);   List<User> list = mapper.getUserList();
  40. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  41. @Select("select * from mybatis.user where id=#{id}")
  42. User selectUserById(@Param("id") int id);
  43. @Select("select * from mybatis.user")
  44. List<User> selectAll();
  45. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  46. boolean insertUser(User u);
  47. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  48. boolean updateUser(User u);
  49. @Delete("delete from mybatis.user where id=#{id}")
  50. boolean deleteUser(@Param("id") int id);   for (User u:list){
  51. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  52. @Select("select * from mybatis.user where id=#{id}")
  53. User selectUserById(@Param("id") int id);
  54. @Select("select * from mybatis.user")
  55. List<User> selectAll();
  56. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  57. boolean insertUser(User u);
  58. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  59. boolean updateUser(User u);
  60. @Delete("delete from mybatis.user where id=#{id}")
  61. boolean deleteUser(@Param("id") int id);       System.out.println(u);
  62. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  63. @Select("select * from mybatis.user where id=#{id}")
  64. User selectUserById(@Param("id") int id);
  65. @Select("select * from mybatis.user")
  66. List<User> selectAll();
  67. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  68. boolean insertUser(User u);
  69. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  70. boolean updateUser(User u);
  71. @Delete("delete from mybatis.user where id=#{id}")
  72. boolean deleteUser(@Param("id") int id);   }
  73. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  74. @Select("select * from mybatis.user where id=#{id}")
  75. User selectUserById(@Param("id") int id);
  76. @Select("select * from mybatis.user")
  77. List<User> selectAll();
  78. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  79. boolean insertUser(User u);
  80. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  81. boolean updateUser(User u);
  82. @Delete("delete from mybatis.user where id=#{id}")
  83. boolean deleteUser(@Param("id") int id);   //不推荐使用
  84. //        List<User> list = sqlSession.selectList("com.qian.dao.UserDao.getUserList");
  85. //        for (User user : list) {
  86. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  87. @Select("select * from mybatis.user where id=#{id}")
  88. User selectUserById(@Param("id") int id);
  89. @Select("select * from mybatis.user")
  90. List<User> selectAll();
  91. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  92. boolean insertUser(User u);
  93. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  94. boolean updateUser(User u);
  95. @Delete("delete from mybatis.user where id=#{id}")
  96. boolean deleteUser(@Param("id") int id);   System.out.println(user);
  97. //        }
  98.         }finally {
  99. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  100. @Select("select * from mybatis.user where id=#{id}")
  101. User selectUserById(@Param("id") int id);
  102. @Select("select * from mybatis.user")
  103. List<User> selectAll();
  104. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  105. boolean insertUser(User u);
  106. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  107. boolean updateUser(User u);
  108. @Delete("delete from mybatis.user where id=#{id}")
  109. boolean deleteUser(@Param("id") int id);   //关闭SqlSession
  110. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  111. @Select("select * from mybatis.user where id=#{id}")
  112. User selectUserById(@Param("id") int id);
  113. @Select("select * from mybatis.user")
  114. List<User> selectAll();
  115. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  116. boolean insertUser(User u);
  117. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  118. boolean updateUser(User u);
  119. @Delete("delete from mybatis.user where id=#{id}")
  120. boolean deleteUser(@Param("id") int id);   sqlSession.close();
  121.         }
  122.     }
  123. }
复制代码
3 增删改查实现

3.1 Mapper接口
  1. public interface UserMapper {
  2.     //查询全部用户
  3.     List<User> getUserList();
  4.     //根据id查询用户
  5.     <select id="queryBlogIf" parameterType="map" resultType="Blog">
  6.     select * from mybatis.blog where 1=1
  7.     <if test="title!=null">
  8.         and title = #{title}
  9.     </if>
  10.     <if test="author!=null">
  11.         and author = #{author}
  12.     </if>
  13. </select>
  14.     //增加新的用户
  15.     boolean insertNewUser(User u);
  16.     //删除用户
  17.     boolean deleteUserById(int id);
  18.     boolean deleteUserByName(String name);
  19.     //修改用户
  20.     boolean updateUserById(User u);
  21. }
复制代码
3.2 xxxMapper.xml文件
  1. <?xml version="1.0" encoding="utf8" ?>
  2. <!DOCTYPE mapper
  3.         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.qian.dao.UserMapper">
  6.     <select id="getUserList" resultType="com.qian.pojo.User">
  7.         select * from mybatis.user
  8.     </select>
  9.     <select id="getUserById" parameterType="int" resultType="com.qian.pojo.User">
  10.     @Test
  11. public void queryBlogIf(){
  12.     SqlSession sqlSession = MybatisUtils.getSqlSession();
  13.     BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
  14.     Map<String,Object> map=new HashMap<String,Object>();
  15.     mapper.queryBlogIf(map);
  16.     System.out.println("==================");
  17.     map.put("title",(String)"MyBatis如此简单");
  18.     mapper.queryBlogIf(map);
  19.     System.out.println("==================");
  20.     map.put("author",(String)"狂神说");
  21.     mapper.queryBlogIf(map);
  22.     System.out.println("==================");
  23.     sqlSession.close();
  24. }
  25.     </select>
  26.    
  27.     <insert id="insertNewUser" parameterType="com.qian.pojo.User">
  28.         insert into mybatis.user (id, name, pwd) VALUES (#{id},#{name},#{pwd})
  29.     </insert>
  30.     <delete id="deleteUserById" parameterType="int">
  31.         delete from mybatis.user where id=#{id}
  32.     </delete>
  33.     <delete id="deleteUserByName" parameterType="String">
  34.         delete from mybatis.user where name=#{name}
  35.     </delete>
  36.     <update id="updateUserById" parameterType="com.qian.pojo.User">
  37.         update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
  38.     </update>
  39. </mapper>
复制代码
3.3 Test类

注意:增删改要提交事务!!!
  1. public class UserDaoTest {
  2.     @Test
  3.     public void test(){
  4.         //获取SqlSession对象
  5.         SqlSession sqlSession = MybatisUtils.getSqlSession();
  6.         try{
  7. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  8. @Select("select * from mybatis.user where id=#{id}")
  9. User selectUserById(@Param("id") int id);
  10. @Select("select * from mybatis.user")
  11. List<User> selectAll();
  12. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  13. boolean insertUser(User u);
  14. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  15. boolean updateUser(User u);
  16. @Delete("delete from mybatis.user where id=#{id}")
  17. boolean deleteUser(@Param("id") int id);   //获取mapper
  18. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  19. @Select("select * from mybatis.user where id=#{id}")
  20. User selectUserById(@Param("id") int id);
  21. @Select("select * from mybatis.user")
  22. List<User> selectAll();
  23. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  24. boolean insertUser(User u);
  25. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  26. boolean updateUser(User u);
  27. @Delete("delete from mybatis.user where id=#{id}")
  28. boolean deleteUser(@Param("id") int id);   UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  29. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  30. @Select("select * from mybatis.user where id=#{id}")
  31. User selectUserById(@Param("id") int id);
  32. @Select("select * from mybatis.user")
  33. List<User> selectAll();
  34. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  35. boolean insertUser(User u);
  36. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  37. boolean updateUser(User u);
  38. @Delete("delete from mybatis.user where id=#{id}")
  39. boolean deleteUser(@Param("id") int id);   查询全表
  40. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  41. @Select("select * from mybatis.user where id=#{id}")
  42. User selectUserById(@Param("id") int id);
  43. @Select("select * from mybatis.user")
  44. List<User> selectAll();
  45. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  46. boolean insertUser(User u);
  47. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  48. boolean updateUser(User u);
  49. @Delete("delete from mybatis.user where id=#{id}")
  50. boolean deleteUser(@Param("id") int id);   List<User> list = mapper.getUserList();
  51. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  52. @Select("select * from mybatis.user where id=#{id}")
  53. User selectUserById(@Param("id") int id);
  54. @Select("select * from mybatis.user")
  55. List<User> selectAll();
  56. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  57. boolean insertUser(User u);
  58. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  59. boolean updateUser(User u);
  60. @Delete("delete from mybatis.user where id=#{id}")
  61. boolean deleteUser(@Param("id") int id);   for (User u:list){
  62. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  63. @Select("select * from mybatis.user where id=#{id}")
  64. User selectUserById(@Param("id") int id);
  65. @Select("select * from mybatis.user")
  66. List<User> selectAll();
  67. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  68. boolean insertUser(User u);
  69. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  70. boolean updateUser(User u);
  71. @Delete("delete from mybatis.user where id=#{id}")
  72. boolean deleteUser(@Param("id") int id);       System.out.println(u);
  73. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  74. @Select("select * from mybatis.user where id=#{id}")
  75. User selectUserById(@Param("id") int id);
  76. @Select("select * from mybatis.user")
  77. List<User> selectAll();
  78. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  79. boolean insertUser(User u);
  80. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  81. boolean updateUser(User u);
  82. @Delete("delete from mybatis.user where id=#{id}")
  83. boolean deleteUser(@Param("id") int id);   }
  84. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  85. @Select("select * from mybatis.user where id=#{id}")
  86. User selectUserById(@Param("id") int id);
  87. @Select("select * from mybatis.user")
  88. List<User> selectAll();
  89. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  90. boolean insertUser(User u);
  91. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  92. boolean updateUser(User u);
  93. @Delete("delete from mybatis.user where id=#{id}")
  94. boolean deleteUser(@Param("id") int id);   //根据id查询
  95. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  96. @Select("select * from mybatis.user where id=#{id}")
  97. User selectUserById(@Param("id") int id);
  98. @Select("select * from mybatis.user")
  99. List<User> selectAll();
  100. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  101. boolean insertUser(User u);
  102. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  103. boolean updateUser(User u);
  104. @Delete("delete from mybatis.user where id=#{id}")
  105. boolean deleteUser(@Param("id") int id);   User user = mapper.getUserById(1);
  106. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  107. @Select("select * from mybatis.user where id=#{id}")
  108. User selectUserById(@Param("id") int id);
  109. @Select("select * from mybatis.user")
  110. List<User> selectAll();
  111. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  112. boolean insertUser(User u);
  113. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  114. boolean updateUser(User u);
  115. @Delete("delete from mybatis.user where id=#{id}")
  116. boolean deleteUser(@Param("id") int id);   System.out.println(user);
  117. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  118. @Select("select * from mybatis.user where id=#{id}")
  119. User selectUserById(@Param("id") int id);
  120. @Select("select * from mybatis.user")
  121. List<User> selectAll();
  122. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  123. boolean insertUser(User u);
  124. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  125. boolean updateUser(User u);
  126. @Delete("delete from mybatis.user where id=#{id}")
  127. boolean deleteUser(@Param("id") int id);   //插入新用户,注意:更新,插入,删除都需要提交事务
  128. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  129. @Select("select * from mybatis.user where id=#{id}")
  130. User selectUserById(@Param("id") int id);
  131. @Select("select * from mybatis.user")
  132. List<User> selectAll();
  133. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  134. boolean insertUser(User u);
  135. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  136. boolean updateUser(User u);
  137. @Delete("delete from mybatis.user where id=#{id}")
  138. boolean deleteUser(@Param("id") int id);   User user1 = new User(4,"李四","25615");
  139. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  140. @Select("select * from mybatis.user where id=#{id}")
  141. User selectUserById(@Param("id") int id);
  142. @Select("select * from mybatis.user")
  143. List<User> selectAll();
  144. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  145. boolean insertUser(User u);
  146. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  147. boolean updateUser(User u);
  148. @Delete("delete from mybatis.user where id=#{id}")
  149. boolean deleteUser(@Param("id") int id);   boolean isInserted = mapper.insertNewUser(user1);
  150. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  151. @Select("select * from mybatis.user where id=#{id}")
  152. User selectUserById(@Param("id") int id);
  153. @Select("select * from mybatis.user")
  154. List<User> selectAll();
  155. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  156. boolean insertUser(User u);
  157. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  158. boolean updateUser(User u);
  159. @Delete("delete from mybatis.user where id=#{id}")
  160. boolean deleteUser(@Param("id") int id);   sqlSession.commit();
  161. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  162. @Select("select * from mybatis.user where id=#{id}")
  163. User selectUserById(@Param("id") int id);
  164. @Select("select * from mybatis.user")
  165. List<User> selectAll();
  166. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  167. boolean insertUser(User u);
  168. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  169. boolean updateUser(User u);
  170. @Delete("delete from mybatis.user where id=#{id}")
  171. boolean deleteUser(@Param("id") int id);   //代码优化
  172. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  173. @Select("select * from mybatis.user where id=#{id}")
  174. User selectUserById(@Param("id") int id);
  175. @Select("select * from mybatis.user")
  176. List<User> selectAll();
  177. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  178. boolean insertUser(User u);
  179. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  180. boolean updateUser(User u);
  181. @Delete("delete from mybatis.user where id=#{id}")
  182. boolean deleteUser(@Param("id") int id);   if (mapper.insertNewUser(new User(4,"李四","25615"))) sqlSession.commit();
  183. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  184. @Select("select * from mybatis.user where id=#{id}")
  185. User selectUserById(@Param("id") int id);
  186. @Select("select * from mybatis.user")
  187. List<User> selectAll();
  188. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  189. boolean insertUser(User u);
  190. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  191. boolean updateUser(User u);
  192. @Delete("delete from mybatis.user where id=#{id}")
  193. boolean deleteUser(@Param("id") int id);   //删除用户
  194. ////多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  195. @Select("select * from mybatis.user where id=#{id}")
  196. User selectUserById(@Param("id") int id);
  197. @Select("select * from mybatis.user")
  198. List<User> selectAll();
  199. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  200. boolean insertUser(User u);
  201. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  202. boolean updateUser(User u);
  203. @Delete("delete from mybatis.user where id=#{id}")
  204. boolean deleteUser(@Param("id") int id);   if (mapper.deleteUserById(4))sqlSession.commit();
  205. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  206. @Select("select * from mybatis.user where id=#{id}")
  207. User selectUserById(@Param("id") int id);
  208. @Select("select * from mybatis.user")
  209. List<User> selectAll();
  210. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  211. boolean insertUser(User u);
  212. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  213. boolean updateUser(User u);
  214. @Delete("delete from mybatis.user where id=#{id}")
  215. boolean deleteUser(@Param("id") int id);    if (mapper.deleteUserByName("李四"))sqlSession.commit();
  216. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  217. @Select("select * from mybatis.user where id=#{id}")
  218. User selectUserById(@Param("id") int id);
  219. @Select("select * from mybatis.user")
  220. List<User> selectAll();
  221. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  222. boolean insertUser(User u);
  223. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  224. boolean updateUser(User u);
  225. @Delete("delete from mybatis.user where id=#{id}")
  226. boolean deleteUser(@Param("id") int id);    //修改用户
  227. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  228. @Select("select * from mybatis.user where id=#{id}")
  229. User selectUserById(@Param("id") int id);
  230. @Select("select * from mybatis.user")
  231. List<User> selectAll();
  232. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  233. boolean insertUser(User u);
  234. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  235. boolean updateUser(User u);
  236. @Delete("delete from mybatis.user where id=#{id}")
  237. boolean deleteUser(@Param("id") int id);   if (mapper.updateUserById(new User(4,"王五","6849816")))sqlSession.commit();
  238.         }finally {
  239. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  240. @Select("select * from mybatis.user where id=#{id}")
  241. User selectUserById(@Param("id") int id);
  242. @Select("select * from mybatis.user")
  243. List<User> selectAll();
  244. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  245. boolean insertUser(User u);
  246. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  247. boolean updateUser(User u);
  248. @Delete("delete from mybatis.user where id=#{id}")
  249. boolean deleteUser(@Param("id") int id);   //关闭SqlSession
  250. //多个参数情况下,有两种解决方式,一个map封装,另一种是注解Param
  251. @Select("select * from mybatis.user where id=#{id}")
  252. User selectUserById(@Param("id") int id);
  253. @Select("select * from mybatis.user")
  254. List<User> selectAll();
  255. @Insert("insert into mybatis.user() values(#{id},#{name},#{password}) ")
  256. boolean insertUser(User u);
  257. @Update("update user set name=#{name},pwd=#{password} where id = #{id}")
  258. boolean updateUser(User u);
  259. @Delete("delete from mybatis.user where id=#{id}")
  260. boolean deleteUser(@Param("id") int id);   sqlSession.close();
  261.         }
  262.     }
  263. }
复制代码
3.4 常见错误

3.5 万能Map

3.6 模糊查询

4 配置解析

4.1 核心配合文件

4.2 环境配置(environments)

MyBatis 可以配置成适应多种环境,尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
MyBatis默认事务连接器就是JDBC,连接池POOLED
4.3 属性(properties)

这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。
4.4 类型别名(typeAliases)

类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。
4.5 设置(settings)

这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。
4.6 映射器(mappers)

MapperRegistry:注册绑定我们的Mapper文件
4.7 其他配置

4.8 生命周期和作用域(Scope)

理解我们之前讨论过的不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题。
SqlSessionFactoryBuilder
SqlSessionFactory
SqlSession
5 解决属性名和字段名不一致

5.1 新建项目mybatis-03

5.2 解决方法

6 日志

6.1 日志工厂

如果数据库操作出现异常,需要通过日志获取sql语句,方便纠错。
6.2 测试输出

<ul>STDOUT_LOGGING
  1. Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
  2. /*
  3.         ...
  4. */
  5. Created connection 471579726.
  6. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1c1bbc4e]
  7. ==>  Preparing: select * from mybatis.user where id=?
  8. ==> Parameters: 1(Integer)
  9. <==    Columns: id, name, pwd
  10. <==        Row: 1, 千树, 123
  11. <==      Total: 1
  12. User{id=1, name='千树', password='123'}
  13. Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1c1bbc4e]
  14. Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1c1bbc4e]
  15. Returned connection 471579726 to pool.
复制代码
  1. ......
  2. Caused by: org.apache.ibatis.exceptions.PersistenceException:
  3. ### Error building SqlSession.
  4. ### The error may exist in SQL Mapper Configuration
  5. ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation.
  6. ......
  7. Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation.  Cause: java.lang.NoClassDefFoundError: org/apache/log4j/Priority
  8. ...
复制代码
  1. <dependency>
  2.     <groupId>log4j</groupId>
  3.     <artifactId>log4j</artifactId>
  4.     <version>1.2.17</version>
  5. </dependency>
复制代码
  1. log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
  2. log4j:WARN Please initialize the log4j system properly.
  3. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  4. User{id=1, name='千树', password='123'}
复制代码
  1. #newhappy  log4j.properties start
  2. log4j.rootLogger=DEBUG,console,file
  3. #控制台输出 console appender
  4. log4j.appender.console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.console.Target=System.out
  6. log4j.appender.console.threshold=DEBUG
  7. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
  9. #文件输出 rolling file appender
  10. log4j.appender.file=org.apache.log4j.RollingFileAppender
  11. log4j.appender.file.File=./log/yu.log
  12. log4j.appender.file.MaxFileSize=10mB
  13. log4j.appender.file.MaxBackupIndex=2
  14. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  15. log4j.appender.file.layout.ConversionPattern=%d{mmm d,yyyy hh:mm:ss a} : %p [%t] %m%n
  16. log4j.appender.file.threshold=DEBUG
  17. #日志输出级别 logger
  18. log4j.logger.org.mybatis=DEBUG
  19. log4j.logger.java.sql=DEBUG
  20. log4j.logger.java.sql.Statement=DEBUG
  21. log4j.logger.java.sql.ResultSet=DEBUG
  22. log4j.logger.java.sql.PreparedStatement=DEBUG
  23. #newhappy log4j.properties end
复制代码
11.3按照结果嵌套处理

11.4 回顾Mysql多对一查询方式

11.5 一对多的处理

11.6 小结

注意点:
12 动态SQL

什么是动态SQL?
动态SQL是指根据不同的条件生成不同的SQL语句
如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。
12.1 搭建环境

创建基础工程
12.2 if

12.3 choose、when、otherwise

12.4 trim、where、set

12.5 SQL片段

12.6 foreach

13 缓存

13.1 简介

13.2 Mybatis缓存

13.3 一级缓存

13.4 二级缓存

13.5 缓存原理

13.6 自定义缓存ehcache


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4