博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
备忘:mybatis 3的使用记录
阅读量:6326 次
发布时间:2019-06-22

本文共 35390 字,大约阅读时间需要 117 分钟。

  这是一篇记录。mybatis是一个部分模仿ORM的framework。是一个介于ORM和原始JDBC的框架。既可以提供ORM的操作对象的特性,又能从详细地控制最终的SQL。达到一个平衡。我们还是得写sql,同时mybatis负责类到数据库记录的映射。mybatis 3之前叫做ibatis, 2.x时代在apache上。后来移到了别的地方。现在似乎在。 文档在:,源代码现在似乎移到了github:。名字也改成了mybatis。有人开发了一个.net版的mybatis。这个给.net世界带来了一个很好的思维。只可惜目前多数人在关注ORM。

.net 版mybatis:  

mybatis 3有很多改进。在mybatis 3,为eclipse准备了集成工具。叫做:mybatis generator。这个工具可以帮我们省很多事情。

用下面这个URL来获得mybatis generator。

  在Eclipse Juno中,Help->Install new software, 在弹出的对话框,Work with对话框中输入以上URL, 点击Add,然后输入这个site的名字。比如输入mybatis generator。然后等对话框从服务器取到信息,就成了这样:

我得说一句,这个工具依赖于maven,所以m2e一定得先装好。到Eclipse marketplace里去先装上maven integration for eclipse Juno or newer。

装好了m2e才能顺利装好mybatis generator。

好了,勾上那些框,next, next, 同意,同意就行了。这样就把mybatis generator装好了。

在项目里用:

pom.xml文件如下:

4.0.0
mymaven
mymaven
0.0.1-SNAPSHOT
org.hibernate
hibernate
3.5.4-Final
pom
org.hibernate
hibernate-annotations
3.5.4-Final
org.hibernate
hibernate-core
3.5.4-Final
org.javassist
javassist
3.18.2-GA
org.slf4j
slf4j-api
1.7.7
ch.qos.logback
logback-classic
1.1.2
ch.qos.logback
logback-core
1.1.2
mysql
mysql-connector-java
5.1.31
org.mybatis
mybatis
3.2.7
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.1
true
true
Generate MyBatis Artifacts
generate

除此之外还需要一个generatorConfig.xml用来配置mybatis generator:

这个generatorConfig.xml可以放到src\main\resources目录中。这样,这个资源文件会被mybatis generator找到并解析。这个配置一看就明白。有那么几个表,需要用mybatis generator产生代码。

然后就用maven来产生代码。右键点你的项目,选择Run as->Run configurations,出来对话框如图:

如果没有这个Maven build 配置的化,就新建一个。取个名叫run mybatis generator的配置。然后里面Goals写上: mybatis-generator:generate,如图:

 输入对了以后就点Run了。后面就是一些maven的log了,可以看到产生了很多代码。

[INFO] Scanning for projects...Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.3.1/maven-install-plugin-2.3.1.pomDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.3.1/maven-install-plugin-2.3.1.pom (5 KB at 5.0 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.0/maven-site-plugin-3.0.pomDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.0/maven-site-plugin-3.0.pom (20 KB at 24.7 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.1/maven-dependency-plugin-2.1.pomDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.1/maven-dependency-plugin-2.1.pom (8 KB at 20.6 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.1/maven-dependency-plugin-2.1.jarDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.1/maven-dependency-plugin-2.1.jar (104 KB at 60.2 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.pomDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.pom (8 KB at 20.7 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/release/maven-release/2.0/maven-release-2.0.pomDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/release/maven-release/2.0/maven-release-2.0.pom (7 KB at 12.1 KB/sec)Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.jarDownloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-release-plugin/2.0/maven-release-plugin-2.0.jar (38 KB at 48.3 KB/sec)[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building mymaven 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.1:generate (default-cli) @ mymaven ---[INFO] Connecting to the Database[INFO] Introspecting table Category[INFO] Introspecting table Comment[INFO] Introspecting table Group[INFO] Introspecting table Message[INFO] Introspecting table Post[INFO] Introspecting table Sensitivekeyword[INFO] Introspecting table User[INFO] Generating Example class for table category[INFO] Generating Record class for table category[INFO] Generating Mapper Interface for table category[INFO] Generating SQL Map for table category[INFO] Generating Example class for table comment[INFO] Generating Record class for table comment[INFO] Generating Mapper Interface for table comment[INFO] Generating SQL Map for table comment[INFO] Generating Example class for table group[INFO] Generating Record class for table group[INFO] Generating Mapper Interface for table group[INFO] Generating SQL Map for table group[INFO] Generating Example class for table message[INFO] Generating Record class for table message[INFO] Generating Mapper Interface for table message[INFO] Generating SQL Map for table message[INFO] Generating Example class for table post[INFO] Generating Record class for table post[INFO] Generating Mapper Interface for table post[INFO] Generating SQL Map for table post[INFO] Generating Example class for table sensitivekeyword[INFO] Generating Record class for table sensitivekeyword[INFO] Generating Mapper Interface for table sensitivekeyword[INFO] Generating SQL Map for table sensitivekeyword[INFO] Generating Example class for table user[INFO] Generating Record class for table user[INFO] Generating Mapper Interface for table user[INFO] Generating SQL Map for table user[INFO] Saving file CategoryMapper.xml[INFO] Saving file CommentMapper.xml[INFO] Saving file GroupMapper.xml[INFO] Saving file MessageMapper.xml[INFO] Saving file PostMapper.xml[INFO] Saving file SensitivekeywordMapper.xml[INFO] Saving file UserMapper.xml[INFO] Saving file CategoryExample.java[INFO] Saving file Category.java[INFO] Saving file CategoryMapper.java[INFO] Saving file CommentExample.java[INFO] Saving file Comment.java[INFO] Saving file CommentMapper.java[INFO] Saving file GroupExample.java[INFO] Saving file Group.java[INFO] Saving file GroupMapper.java[INFO] Saving file MessageExample.java[INFO] Saving file Message.java[INFO] Saving file MessageMapper.java[INFO] Saving file PostExample.java[INFO] Saving file Post.java[INFO] Saving file PostMapper.java[INFO] Saving file SensitivekeywordExample.java[INFO] Saving file Sensitivekeyword.java[INFO] Saving file SensitivekeywordMapper.java[INFO] Saving file UserExample.java[INFO] Saving file User.java[INFO] Saving file UserMapper.java[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 10.614s[INFO] Finished at: Sun Jul 13 22:47:25 CST 2014[INFO] Final Memory: 9M/123M[INFO] ------------------------------------------------------------------------

按照之前generatorConfig.xml的配置,dao类都会在org.nf.dao目录中。virtual object就都在org.nf.vo目录中。

查看一下generator为我们产生的代码。发现它产生的代码还挺全面的。

例如我们的model类

package org.nf.vo;public class Category {    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database column category.CategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    private Integer categoryid;    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database column category.CategoryName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    private String categoryname;    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database column category.ParentCategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    private Integer parentcategoryid;    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database column category.ShortName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    private String shortname;    /**     * This method was generated by MyBatis Generator.     * This method returns the value of the database column category.CategoryId     *     * @return the value of category.CategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public Integer getCategoryid() {        return categoryid;    }    /**     * This method was generated by MyBatis Generator.     * This method sets the value of the database column category.CategoryId     *     * @param categoryid the value for category.CategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public void setCategoryid(Integer categoryid) {        this.categoryid = categoryid;    }    /**     * This method was generated by MyBatis Generator.     * This method returns the value of the database column category.CategoryName     *     * @return the value of category.CategoryName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public String getCategoryname() {        return categoryname;    }    /**     * This method was generated by MyBatis Generator.     * This method sets the value of the database column category.CategoryName     *     * @param categoryname the value for category.CategoryName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public void setCategoryname(String categoryname) {        this.categoryname = categoryname;    }    /**     * This method was generated by MyBatis Generator.     * This method returns the value of the database column category.ParentCategoryId     *     * @return the value of category.ParentCategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public Integer getParentcategoryid() {        return parentcategoryid;    }    /**     * This method was generated by MyBatis Generator.     * This method sets the value of the database column category.ParentCategoryId     *     * @param parentcategoryid the value for category.ParentCategoryId     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public void setParentcategoryid(Integer parentcategoryid) {        this.parentcategoryid = parentcategoryid;    }    /**     * This method was generated by MyBatis Generator.     * This method returns the value of the database column category.ShortName     *     * @return the value of category.ShortName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public String getShortname() {        return shortname;    }    /**     * This method was generated by MyBatis Generator.     * This method sets the value of the database column category.ShortName     *     * @param shortname the value for category.ShortName     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    public void setShortname(String shortname) {        this.shortname = shortname;    }}

这个类映射数据库里的Category表。

这没有多少。无非就是一些fields加get和set方法。还是CategoryExample类的内容多一些。

package org.nf.vo;import java.util.ArrayList;import java.util.List;public class CategoryExample {    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    protected String orderByClause;    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    protected boolean distinct;    /**     * This field was generated by MyBatis Generator.     * This field corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    protected List
oredCriteria; /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public CategoryExample() { oredCriteria = new ArrayList
(); } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public String getOrderByClause() { return orderByClause; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public void setDistinct(boolean distinct) { this.distinct = distinct; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public boolean isDistinct() { return distinct; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public List
getOredCriteria() { return oredCriteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public void or(Criteria criteria) { oredCriteria.add(criteria); } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ protected abstract static class GeneratedCriteria { protected List
criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList
(); } public boolean isValid() { return criteria.size() > 0; } public List
getAllCriteria() { return criteria; } public List
getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andCategoryidIsNull() { addCriterion("CategoryId is null"); return (Criteria) this; } public Criteria andCategoryidIsNotNull() { addCriterion("CategoryId is not null"); return (Criteria) this; } public Criteria andCategoryidEqualTo(Integer value) { addCriterion("CategoryId =", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidNotEqualTo(Integer value) { addCriterion("CategoryId <>", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidGreaterThan(Integer value) { addCriterion("CategoryId >", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidGreaterThanOrEqualTo(Integer value) { addCriterion("CategoryId >=", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidLessThan(Integer value) { addCriterion("CategoryId <", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidLessThanOrEqualTo(Integer value) { addCriterion("CategoryId <=", value, "categoryid"); return (Criteria) this; } public Criteria andCategoryidIn(List
values) { addCriterion("CategoryId in", values, "categoryid"); return (Criteria) this; } public Criteria andCategoryidNotIn(List
values) { addCriterion("CategoryId not in", values, "categoryid"); return (Criteria) this; } public Criteria andCategoryidBetween(Integer value1, Integer value2) { addCriterion("CategoryId between", value1, value2, "categoryid"); return (Criteria) this; } public Criteria andCategoryidNotBetween(Integer value1, Integer value2) { addCriterion("CategoryId not between", value1, value2, "categoryid"); return (Criteria) this; } public Criteria andCategorynameIsNull() { addCriterion("CategoryName is null"); return (Criteria) this; } public Criteria andCategorynameIsNotNull() { addCriterion("CategoryName is not null"); return (Criteria) this; } public Criteria andCategorynameEqualTo(String value) { addCriterion("CategoryName =", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameNotEqualTo(String value) { addCriterion("CategoryName <>", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameGreaterThan(String value) { addCriterion("CategoryName >", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameGreaterThanOrEqualTo(String value) { addCriterion("CategoryName >=", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameLessThan(String value) { addCriterion("CategoryName <", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameLessThanOrEqualTo(String value) { addCriterion("CategoryName <=", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameLike(String value) { addCriterion("CategoryName like", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameNotLike(String value) { addCriterion("CategoryName not like", value, "categoryname"); return (Criteria) this; } public Criteria andCategorynameIn(List
values) { addCriterion("CategoryName in", values, "categoryname"); return (Criteria) this; } public Criteria andCategorynameNotIn(List
values) { addCriterion("CategoryName not in", values, "categoryname"); return (Criteria) this; } public Criteria andCategorynameBetween(String value1, String value2) { addCriterion("CategoryName between", value1, value2, "categoryname"); return (Criteria) this; } public Criteria andCategorynameNotBetween(String value1, String value2) { addCriterion("CategoryName not between", value1, value2, "categoryname"); return (Criteria) this; } public Criteria andParentcategoryidIsNull() { addCriterion("ParentCategoryId is null"); return (Criteria) this; } public Criteria andParentcategoryidIsNotNull() { addCriterion("ParentCategoryId is not null"); return (Criteria) this; } public Criteria andParentcategoryidEqualTo(Integer value) { addCriterion("ParentCategoryId =", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidNotEqualTo(Integer value) { addCriterion("ParentCategoryId <>", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidGreaterThan(Integer value) { addCriterion("ParentCategoryId >", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidGreaterThanOrEqualTo(Integer value) { addCriterion("ParentCategoryId >=", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidLessThan(Integer value) { addCriterion("ParentCategoryId <", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidLessThanOrEqualTo(Integer value) { addCriterion("ParentCategoryId <=", value, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidIn(List
values) { addCriterion("ParentCategoryId in", values, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidNotIn(List
values) { addCriterion("ParentCategoryId not in", values, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidBetween(Integer value1, Integer value2) { addCriterion("ParentCategoryId between", value1, value2, "parentcategoryid"); return (Criteria) this; } public Criteria andParentcategoryidNotBetween(Integer value1, Integer value2) { addCriterion("ParentCategoryId not between", value1, value2, "parentcategoryid"); return (Criteria) this; } public Criteria andShortnameIsNull() { addCriterion("ShortName is null"); return (Criteria) this; } public Criteria andShortnameIsNotNull() { addCriterion("ShortName is not null"); return (Criteria) this; } public Criteria andShortnameEqualTo(String value) { addCriterion("ShortName =", value, "shortname"); return (Criteria) this; } public Criteria andShortnameNotEqualTo(String value) { addCriterion("ShortName <>", value, "shortname"); return (Criteria) this; } public Criteria andShortnameGreaterThan(String value) { addCriterion("ShortName >", value, "shortname"); return (Criteria) this; } public Criteria andShortnameGreaterThanOrEqualTo(String value) { addCriterion("ShortName >=", value, "shortname"); return (Criteria) this; } public Criteria andShortnameLessThan(String value) { addCriterion("ShortName <", value, "shortname"); return (Criteria) this; } public Criteria andShortnameLessThanOrEqualTo(String value) { addCriterion("ShortName <=", value, "shortname"); return (Criteria) this; } public Criteria andShortnameLike(String value) { addCriterion("ShortName like", value, "shortname"); return (Criteria) this; } public Criteria andShortnameNotLike(String value) { addCriterion("ShortName not like", value, "shortname"); return (Criteria) this; } public Criteria andShortnameIn(List
values) { addCriterion("ShortName in", values, "shortname"); return (Criteria) this; } public Criteria andShortnameNotIn(List
values) { addCriterion("ShortName not in", values, "shortname"); return (Criteria) this; } public Criteria andShortnameBetween(String value1, String value2) { addCriterion("ShortName between", value1, value2, "shortname"); return (Criteria) this; } public Criteria andShortnameNotBetween(String value1, String value2) { addCriterion("ShortName not between", value1, value2, "shortname"); return (Criteria) this; } } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table category * * @mbggenerated do_not_delete_during_merge Sun Jul 13 22:47:25 CST 2014 */ public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } /** * This class was generated by MyBatis Generator. * This class corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ public static class Criterion { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(String condition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler) { super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List
) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue) { this(condition, value, secondValue, null); } }}

这个CategoryExample给我们一个很好的写条件表达式的例子。对于integer类型的字段,有诸如greaterthan, lessthan之类的方法。而串类型的字段,就有like 这样的方法。然后不同的条件之间可以用and和or关系。这个给我们查询某些特殊条件的数据是带来的方便。

之后两个可以说是关键部分,一个是Mapper接口和Mapper的xml定义。一个定义了这个model类有什么方法可以调用,另一个定义了这些方法的实现方式。

这是Mapper接口:

package org.nf.dao;import java.util.List;import org.apache.ibatis.annotations.Param;import org.nf.vo.Category;import org.nf.vo.CategoryExample;public interface CategoryMapper {    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    int countByExample(CategoryExample example);    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    int deleteByExample(CategoryExample example);    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    int deleteByPrimaryKey(Integer categoryid);    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    int insert(Category record);    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    int insertSelective(Category record);    /**     * This method was generated by MyBatis Generator.     * This method corresponds to the database table category     *     * @mbggenerated Sun Jul 13 22:47:25 CST 2014     */    List
selectByExample(CategoryExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ Category selectByPrimaryKey(Integer categoryid); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ int updateByExampleSelective(@Param("record") Category record, @Param("example") CategoryExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ int updateByExample(@Param("record") Category record, @Param("example") CategoryExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ int updateByPrimaryKeySelective(Category record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table category * * @mbggenerated Sun Jul 13 22:47:25 CST 2014 */ int updateByPrimaryKey(Category record);}

 

这是CategoryMapper.xml:

and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
CategoryId, CategoryName, ParentCategoryId, ShortName
delete from category where CategoryId = #{categoryid,jdbcType=INTEGER}
delete from category
insert into category (CategoryId, CategoryName, ParentCategoryId, ShortName) values (#{categoryid,jdbcType=INTEGER}, #{categoryname,jdbcType=VARCHAR}, #{parentcategoryid,jdbcType=INTEGER}, #{shortname,jdbcType=VARCHAR})
insert into category
CategoryId,
CategoryName,
ParentCategoryId,
ShortName,
#{categoryid,jdbcType=INTEGER},
#{categoryname,jdbcType=VARCHAR},
#{parentcategoryid,jdbcType=INTEGER},
#{shortname,jdbcType=VARCHAR},
update category
CategoryId = #{record.categoryid,jdbcType=INTEGER},
CategoryName = #{record.categoryname,jdbcType=VARCHAR},
ParentCategoryId = #{record.parentcategoryid,jdbcType=INTEGER},
ShortName = #{record.shortname,jdbcType=VARCHAR},
update category set CategoryId = #{record.categoryid,jdbcType=INTEGER}, CategoryName = #{record.categoryname,jdbcType=VARCHAR}, ParentCategoryId = #{record.parentcategoryid,jdbcType=INTEGER}, ShortName = #{record.shortname,jdbcType=VARCHAR}
update category
CategoryName = #{categoryname,jdbcType=VARCHAR},
ParentCategoryId = #{parentcategoryid,jdbcType=INTEGER},
ShortName = #{shortname,jdbcType=VARCHAR},
where CategoryId = #{categoryid,jdbcType=INTEGER}
update category set CategoryName = #{categoryname,jdbcType=VARCHAR}, ParentCategoryId = #{parentcategoryid,jdbcType=INTEGER}, ShortName = #{shortname,jdbcType=VARCHAR} where CategoryId = #{categoryid,jdbcType=INTEGER}

这个CategoryMapper定义了一些CRUD方法的实现方式。比如updateByPrimaryKeySelective就能根据输入的参数来决定更新哪些字段。同时我们还可以在其参数CategoryExample中传入若干个条件。比如id大于一个数值,name中包含某某字符等等条件。那么更新哪些记录就可以自己定义条件了。这是一个很大的进步。很方便。

把Mapper xml放到src/main/resources目录下,这样这些xml就会打包到发布的jar包里。

试着调用这些产生的代码:

package org.nf;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.hibernate.*;import org.hibernate.cfg.*;import org.nf.model.Category;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class myfirst {    private static SqlSessionFactory sqlSessionFactory;    private static Reader reader;    /**     * @param args     */    public static void main(String[] args) {            //mybatis        try{            reader    =  Resources.getResourceAsReader("Configuration.xml");            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);        }catch(Exception e){            e.printStackTrace();        }        SqlSession sessionmybatis = sqlSessionFactory.openSession();        try {            org.nf.vo.Category categorymybatis = (org.nf.vo.Category) sessionmybatis.selectOne("org.nf.dao.CategoryMapper.selectByPrimaryKey", 5);            System.out.println(categorymybatis.getCategoryname());        } finally {            sessionmybatis.close();        }        logger.info("End");    }}

 

 

 

转载地址:http://kbgaa.baihongyu.com/

你可能感兴趣的文章
自学java时的笔记(一)
查看>>
Qt之文本编辑器(二)
查看>>
python编译时检查语法错误
查看>>
考题纠错2
查看>>
SQL——索引
查看>>
Python新手快速入门教程-基础语法
查看>>
JVM性能调优入门
查看>>
关于raid的基本原理、软raid的实现演示
查看>>
科技企业的幕后推手,人工智能究竟有何魔力
查看>>
详解Oracle临时表的几种用法及意义
查看>>
HTML(七)------ 表格
查看>>
如何成为一个设计师和程序员混合型人才
查看>>
unable to load selinux policy. machine is in enforcing
查看>>
2015年10月23日作业
查看>>
MySQL5.7 加强了root用户登录安全性
查看>>
CentOS 6.3_Nagios安装配置与登录
查看>>
加强型的记录集权限(数据集权限、约束表达式设置功能)实现方法界面参考...
查看>>
Linux 内存机制
查看>>
linux下定时任务
查看>>
SharePoint 2013 部署 Part 1
查看>>