
{"id":2787,"date":"2020-12-19T11:35:41","date_gmt":"2020-12-19T03:35:41","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2787"},"modified":"2020-12-19T16:25:50","modified_gmt":"2020-12-19T08:25:50","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bmybatis%e7%9a%84%e5%a2%9e%e5%88%a0%e6%94%b9%e6%9f%a5crud","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2787","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u7684\u589e\u5220\u6539\u67e5CRUD"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u7684\u589e\u5220\u6539\u67e5CRUD<\/h3>\n<h3><strong>\u67e5\u8be2\uff1a<\/strong><\/h3>\n<p><strong>mapper\uff1a<\/strong><\/p>\n<pre>&lt;!-- \u901a\u8fc7 id \u67e5\u8be2\u7528\u6237 --&gt;\r\n&lt;select id=\"selectUser\" resultType=\"Users\" parameterType=\"int\"&gt;\r\n    select * from users where id = #{id}\r\n&lt;\/select&gt;\r\n&lt;!--  \u8bbe\u7f6e ResultMap --&gt;\r\n&lt;resultMap id=\"usersResultMap\" type=\"Users\"&gt;\r\n    &lt;id column=\"id\" property=\"id\" jdbcType=\"INTEGER\"\/&gt;\r\n    &lt;!-- \u5c06 password \u8bbe\u7f6e pwd \u4e3a\u522b\u540d --&gt;\r\n    &lt;result column=\"pwd\" property=\"password\" jdbcType=\"VARCHAR\"\/&gt;\r\n&lt;\/resultMap&gt;\r\n&lt;!--  \u901a\u8fc7 resultMap \u67e5\u8be2\u7528\u6237 --&gt;\r\n&lt;select id=\"selectUser2\" resultMap=\"usersResultMap\" parameterType=\"int\"&gt;\r\n    select * from users where id = #{id}\r\n&lt;\/select&gt;\r\n<!--more--><\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/17.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>dao\uff1a<\/strong><\/p>\n<pre>Users selectUser2(Integer id);<\/pre>\n<p><strong>\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<pre>@Test\r\npublic void test(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    UsersDao mapper = sqlSession.getMapper(UsersDao.class);\r\n    Users users = mapper.selectUser(1);\r\n    sqlSession.close();\r\n    System.out.println(users);\r\n}\r\n@Test\r\npublic void test2(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    Users users = (Users) sqlSession.selectOne(\r\n            \"mybatis.dao.UsersDao.selectUser\", 2\r\n    );\r\n    sqlSession.close();\r\n    System.out.println(users);\r\n}\r\n@Test\r\npublic void test3(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    UsersDao mapper = sqlSession.getMapper(UsersDao.class);\r\n    Users users = mapper.selectUser2(2);\r\n    sqlSession.close();\r\n    System.out.println(users);\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/18.png\" alt=\"\" width=\"680\" \/><br \/>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/19.png\" alt=\"\" width=\"680\" \/><\/p>\n<h3><strong>\u65b0\u589e\uff1a<\/strong><\/h3>\n<p><strong>mapper\uff1a<\/strong><\/p>\n<pre>&lt;!-- \u589e\u52a0  --&gt;\r\n&lt;insert id=\"insertUsers\" parameterType=\"Users\" keyProperty=\"id\"&gt;\r\n    insert into users(username,password,rolename,identity,mobile)\r\n    values(#{username},#{password},#{rolename},#{identity},#{mobile})\r\n&lt;\/insert&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/20.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>dao\uff1a<\/strong><\/p>\n<pre>int insertUsers(Users users);<\/pre>\n<p><strong>\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<pre>@Test\r\npublic void test10(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    UsersDao mapper = sqlSession.getMapper(UsersDao.class);\r\n    Users users=new Users(\"\u53efadmin444\",\"admin444\",\"\u53efadmin444\",\"7\",\"144\");\r\n    int i = mapper.insertUsers(users);\r\n    if(i&gt;0) System.out.println(\"\u65b0\u589e\u6210\u529f\");\r\n    else System.out.println(\"\u65b0\u589e\u5931\u8d25\");\r\n    sqlSession.commit();\r\n    sqlSession.close();\r\n    System.out.println(users);\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/21.png\" alt=\"\" width=\"680\" \/><\/p>\n<h3><strong>\u4fee\u6539\uff1a<\/strong><\/h3>\n<p><strong>mapper\uff1a<\/strong><\/p>\n<pre>&lt;!--\u4fee\u6539 --&gt;\r\n&lt;update id=\"updateUsers\" parameterType=\"Users\"&gt;\r\n    update users set password=#{password} where id=#{id}\r\n&lt;\/update&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/22.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>dao\uff1a<\/strong><\/p>\n<pre>int updateUsers(Users users);<\/pre>\n<p><strong>\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<pre>@Test\r\npublic void test11(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    UsersDao mapper = sqlSession.getMapper(UsersDao.class);\r\n    Users users=new Users();\r\n    users.setId(1);\r\n    users.setPassword(\"admin666\");\r\n    int i = mapper.updateUsers(users);\r\n    if(i&gt;0) System.out.println(\"\u4fee\u6539\u6210\u529f\");\r\n    else System.out.println(\"\u4fee\u6539\u5931\u8d25\");\r\n    sqlSession.commit();\r\n    sqlSession.close();\r\n    System.out.println(users);\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/23.png\" alt=\"\" width=\"680\" \/><\/p>\n<h3><strong>\u5220\u9664\uff1a<\/strong><\/h3>\n<p><strong>mapper\uff1a<\/strong><\/p>\n<pre>&lt;!-- \u5220\u9664 --&gt;\r\n&lt;delete id=\"deleteUsers\" parameterType=\"int\"&gt;\r\n    delete from users where id=#{id}\r\n&lt;\/delete&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/24.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>dao\uff1a<\/strong><\/p>\n<pre>int deleteUsers(Integer id);<\/pre>\n<p><strong>\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<pre>@Test\r\npublic void test12(){\r\n    SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n    UsersDao mapper = sqlSession.getMapper(UsersDao.class);\r\n    int i = mapper.deleteUsers(12);\r\n    if(i&gt;0) System.out.println(\"\u5220\u9664\u6210\u529f\");\r\n    else System.out.println(\"\u5220\u9664\u5931\u8d25\");\r\n    sqlSession.commit();\r\n    sqlSession.close();\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/19\/25.png\" alt=\"\" width=\"680\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u7684\u589e\u5220\u6539\u67e5CRUD \u67e5\u8be2\uff1a mapper\uff1a &lt;!&#8211; \u901a\u8fc7 id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2787","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2787"}],"version-history":[{"count":3,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2787\/revisions"}],"predecessor-version":[{"id":2798,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2787\/revisions\/2798"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}