{"id":8520,"date":"2022-11-14T19:29:09","date_gmt":"2022-11-14T11:29:09","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8520"},"modified":"2022-11-14T19:29:09","modified_gmt":"2022-11-14T11:29:09","slug":"mybatis-tkmybatis-%e4%bb%8b%e7%bb%8d%e5%92%8c%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8520","title":{"rendered":"Mybatis TKMybatis \u4ecb\u7ecd\u548c\u4f7f\u7528"},"content":{"rendered":"\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f TKMybatis<\/strong><\/p>\n\n\n\n<p>TKMybatis \u662f\u57fa\u4e8e Mybatis \u6846\u67b6\u5f00\u53d1\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5185\u90e8\u5b9e\u73b0\u4e86\u5bf9\u5355\u8868\u7684\u57fa\u672c\u6570\u636e\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u7b80\u5355\u7ee7\u627f TKMybatis \u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u5c31\u80fd\u591f\u5b9e\u73b0\u65e0\u9700\u7f16\u5199\u4efb\u4f55 sql \u5373\u80fd\u5b8c\u6210\u5355\u8868\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001TKMybatis \u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.1 Springboot \u9879\u76ee\u4e2d\u52a0\u5165\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--\u901a\u7528mapper\u8d77\u6b65\u4f9d\u8d56-->\n&lt;dependency>\n    &lt;groupId>tk.mybatis&lt;\/groupId>\n    &lt;artifactId>mapper-spring-boot-starter&lt;\/artifactId>\n    &lt;version>2.0.4&lt;\/version>\n&lt;\/dependency><\/pre>\n\n\n\n<p>\u5728 POJO \u7c7b\u4e2d\u52a0\u5165\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--\u6bcf\u4e2a\u5de5\u7a0b\u90fd\u6709Pojo\uff0c\u90fd\u9700\u8981\u7528\u5230\u8be5\u5305\u5bf9\u5e94\u7684\u6ce8\u89e3-->\n&lt;dependency>\n    &lt;groupId>javax.persistence&lt;\/groupId>\n    &lt;artifactId>persistence-api&lt;\/artifactId>\n    &lt;version>1.0&lt;\/version>\n    &lt;scope>compile&lt;\/scope>\n&lt;\/dependency><\/pre>\n\n\n\n<p>\u5728\u542f\u52a8\u7c7b\u4e2d\u914d\u7f6e @MapperScan \u626b\u63cf<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\n@MapperScan(basePackages = {\"com.tom.order.mapper\"})\npublic class OrderApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(OrderApplication.class, args);\n    }\n}\n<\/pre>\n\n\n\n<p><strong>2.2\u00a0\u4f7f\u7528\u8bb2\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.2.1 \u5b9e\u4f53\u7c7b\u4e2d\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>\u5728\u5b9e\u4f53\u7c7b\u4e2d\uff0c\u5e38\u7528\u7684\u6ce8\u89e3\u548c\u610f\u4e49\u4e3a\uff1a<\/p>\n\n\n\n<ul><li><strong>@Table\uff1a<\/strong>\u63cf\u8ff0\u6570\u636e\u5e93\u8868\u4fe1\u606f\uff0c\u4e3b\u8981\u5c5e\u6027\u6709name(\u8868\u540d)\u3001schema\u3001catalog\u3001uniqueConstraints\u7b49\u3002<\/li><li><strong>@Id\uff1a<\/strong>\u6307\u5b9a\u8868\u4e3b\u952e\u5b57\u6bb5\uff0c\u65e0\u5c5e\u6027\u503c\u3002<\/li><li><strong>@Column\uff1a<\/strong>\u63cf\u8ff0\u6570\u636e\u5e93\u5b57\u6bb5\u4fe1\u606f\uff0c\u4e3b\u8981\u5c5e\u6027\u6709name(\u5b57\u6bb5\u540d)\u3001columnDefinition\u3001insertable\u3001length\u3001nullable(\u662f\u5426\u53ef\u4e3a\u7a7a)\u3001precision\u3001scale\u3001table\u3001unique\u3001updatable\u7b49\u3002<\/li><li><strong>@ColumnType\uff1a<\/strong>\u63cf\u8ff0\u6570\u636e\u5e93\u5b57\u6bb5\u7c7b\u578b\uff0c\u53ef\u5bf9\u4e00\u4e9b\u7279\u6b8a\u7c7b\u578b\u4f5c\u914d\u7f6e\uff0c\u8fdb\u884c\u7279\u6b8a\u5904\u7406\uff0c\u4e3b\u8981\u5c5e\u6027\u6709jdbcType\u3001column\u3001typeHandler\u7b49\u3002<\/li><li>\u5176\u4ed6\u6ce8\u89e3\u5982\uff1a@Transient\u3001@ColumnResult\u3001@JoinColumn\u3001@OrderBy\u3001@Embeddable\u7b49\u6682\u4e0d\u63cf\u8ff0<\/li><\/ul>\n\n\n\n<p><strong>2.2.2 dao\u4e2d\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p>\u5355\u8868\u64cd\u4f5c\uff0c\u53ea\u9700\u8981\u7ee7\u627f tk.mybatis \u4e0b\u7684 Mapper \u63a5\u53e3\u5373\u53ef\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tk.mybatis.mapper.common.Mapper;\n \n@Repository\npublic interface BrandMapper extends Mapper&lt;Brand> {\n}<\/pre>\n\n\n\n<p>\u67e5\u770b\u5177\u4f53\u4f7f\u7528\uff1a\u5185\u90e8\u90fd\u5df2\u7ecf\u5c01\u88c5\u4e86\u57fa\u672c\u7684\u5355\u8868\u64cd\u4f5c<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-1024x214.png\" alt=\"\" class=\"wp-image-8521\" width=\"551\" height=\"115\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-1024x214.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-300x63.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-768x161.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-830x173.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-230x48.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-350x73.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208-480x100.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-208.png 1330w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.2.3 Service \u5c42\u4e2d\u4f7f\u7528<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-1024x898.png\" alt=\"\" class=\"wp-image-8522\" width=\"546\" height=\"478\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-1024x898.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-300x263.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-768x674.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-1536x1348.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-830x728.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-230x202.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-350x307.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209-480x421.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-209.png 1550w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.3 \u5b9e\u9645\u6848\u4f8b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2.3.1 dao \u5c42\u4f7f\u7528<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> \nimport tk.mybatis.mapper.common.Mapper;\n \n\/**\n * DAO \u4f7f\u7528\u901a\u7528Mapper\n * DSO\u63a5\u53e3\u9700\u8981\u7ee7\u627f tk.mybatis.mapper.common.Mapper\n *\/\n@Repository\npublic interface BrandMapper extends Mapper&lt;Brand> {\n \n \n}<\/pre>\n\n\n\n<p><strong>2.3.2 service \u5c42\u4f7f\u7528<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import com.github.pagehelper.PageHelper;\nimport com.github.pagehelper.PageInfo;\nimport org.apache.commons.lang.StringUtils;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport tk.mybatis.mapper.entity.Example;\n \nimport java.util.List;\n \n@Service\npublic class BrandServiceImpl implements BrandService {\n \n    @Autowired\n    private BrandMapper brandMapper;\n \n    public Example createExample(Brand brand) {\n        \/\/ \u81ea\u5b9a\u4e49\u6761\u4ef6\u641c\u7d22\u5bf9\u8c61 Example\n        Example example = new Example(Brand.class);\n        Example.Criteria criteria = example.createCriteria(); \/\/\u6761\u4ef6\u6784\u9020\u5668\n \n        if (brand != null) {\n            if (!StringUtils.isEmpty(brand.getName())) {\n                criteria.andLike(\"name\", '%' + brand.getName() + '%');\n            }\n \n            if (!StringUtils.isEmpty(brand.getLetter())) {\n                criteria.andEqualTo(\"letter\", brand.getLetter());\n            }\n        }\n        return example;\n    }\n \n    @Override\n    public List&lt;Brand> findAll() {\n        return brandMapper.selectAll();\n    }\n \n    @Override\n    public List&lt;Brand> findList(Brand brand) {\n        Example example = createExample(brand);\n        return brandMapper.selectByExample(example);\n    }\n \n    @Override\n    public Brand findById(Integer id) {\n        return brandMapper.selectByPrimaryKey(id);\n    }\n \n    \/**\n     * \u5206\u9875\u67e5\u8be2\n     * @param page  \u5f53\u524d\u9875\n     * @param size  \u6bcf\u9875\u663e\u793a\u7684\u6761\u6570\n     * @return\n     *\/\n    @Override\n    public PageInfo&lt;Brand> pageSearch(Integer page, Integer size) {\n        \/\/ \u5206\u9875\u5b9e\u73b0\n        \/\/ \u540e\u9762\u7684\u67e5\u8be2\u5fc5\u987b\u662f\u7d27\u8ddf\u96c6\u5408\u67e5\u8be2\n        PageHelper.startPage(page, size);\n        \/\/ \u67e5\u8be2\u96c6\u5408\n        List&lt;Brand> brands = brandMapper.selectAll();\n        return new PageInfo&lt;Brand>(brands);\n    }\n \n    @Override\n    public PageInfo&lt;Brand> pageSearchAndCondition(Brand brand, Integer page, Integer size) {\n        \/\/ \u5f00\u59cb\u5206\u9875\n        PageHelper.startPage(page, size);\n        \/\/ \u641c\u7d22\u6570\u636e\n        Example example = createExample(brand);\n        List&lt;Brand> list = brandMapper.selectByExample(example);\n        return new PageInfo&lt;Brand>(list);\n    }\n \n    \/**\n     * \u589e\u52a0\u54c1\u724c\n     * @param brand\n     *\/\n    @Override\n    public void add(Brand brand) {\n        \/\/ \u4f7f\u7528\u901a\u7528 Mapper.insertSelective\n        \/\/ \u65b9\u6cd5\u4e2d\u4f46\u51e1\u5e26\u6709selective\u5c31\u4f1a\u5ffd\u7565\u7a7a\u503c\n        int i = brandMapper.insertSelective(brand);\n    }\n \n    \/**\n     * \u6839\u636eid\u4fee\u6539\u54c1\u724c\n     * @param brand\n     *\/\n    @Override\n    public void update(Brand brand) {\n        \/\/ \u4f7f\u7528\u901a\u7528mapper.update();\n        brandMapper.updateByPrimaryKeySelective(brand);\n    }\n \n    \/**\n     * \u6839\u636eid\u5220\u9664\n     * @param id\n     *\/\n    @Override\n    public void del(Integer id) {\n        brandMapper.deleteByPrimaryKey(id);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ec0\u4e48\u662f TKMybatis TKMybatis \u662f\u57fa\u4e8e Mybatis \u6846\u67b6\u5f00\u53d1\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5185\u90e8\u5b9e\u73b0\u4e86\u5bf9\u5355 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8520"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8520"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8520\/revisions"}],"predecessor-version":[{"id":8523,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8520\/revisions\/8523"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}