通过mybatis |
您所在的位置:网站首页 › 小木人涂色教程 › 通过mybatis |
一、前言 下面通过mybatis-generator-core-1.3.1的版本,其中依赖包(mybatis-generator-core-1.3.1.jar、mysql-connector-java-5.1.6-bin.jar、sqljdbc4.jar、ojdbc14.jar),其中通过在eclipse中和批处理文件run.bat两种不同的方式基于generator.xml配置文件来生产dao、mappers及model层代码类,需下载完整的源码跳转到“mybatis-generator-core-1.3.1包”示例下载页面 二、方法一(基于批处理方式) 1.在mysql的数据库test中创建guser表脚本 CREATE TABLE 'guser' (@b@ 'id' int(20) NOT NULL COMMENT '主键',@b@ 'name' varchar(60) DEFAULT NULL COMMENT '用户名',@b@ 'create_by' varchar(60) DEFAULT NULL COMMENT '创建人',@b@ 'create_time' date DEFAULT NULL COMMENT '创建时间',@b@ 'update_by' varchar(60) DEFAULT NULL COMMENT '更新人',@b@ 'update_time' date DEFAULT NULL COMMENT '更新时间',@b@ PRIMARY KEY ('id')@b@) ENGINE=MyISAM DEFAULT CHARSET=utf8;2.generator.xml的配置内容如下 @b@@b@@b@@b@ @b@ @b@@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@如果连接oracle数据库修改部分如下 @b@@b@@b@@b@ @b@ @b@@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ ....@b@ @b@ @b@ @b@3.配置JAVA_HOME环境,后配置run.bat批处理文件,内容如下 java -jar C:\WS\mybatis-generator-core-1.3.1\lib\mybatis-generator-core-1.3.1.jar -configfile C:\WS\mybatis-generator-core-1.3.1\generator.xml -overwrite4.运行run.bat能正常在target目录生成dao、mappers及model代码,如下图所示 三、方法二(基于eclipse方式) 1.在项目的generator.xml,配置如下 @b@@b@@b@@b@ @b@ @b@@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@2.在项目中引入mybatis-generator-core-1.3.1.jar、mysql-connector-java-5.1.6-bin.jar及mybatis-3.0.2.jar.zip, 3.编写GeneratorExecuter执行类入口,代码如下 import java.io.File;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import org.mybatis.generator.api.MyBatisGenerator;@b@import org.mybatis.generator.config.Configuration;@b@import org.mybatis.generator.config.xml.ConfigurationParser;@b@import org.mybatis.generator.internal.DefaultShellCallback;@b@@b@public class GeneratorExecuter {@b@@b@ public void generator() throws Exception{@b@ List warnings = new ArrayList();@b@ boolean overwrite = true;@b@ @b@ File configFile = new File("C:\\WS\\NJ\\project\\xwood-project\\JavaTest\\src\\com\\xwood\\gen\\generator.xml"); @b@ ConfigurationParser cp = new ConfigurationParser(warnings);@b@ Configuration config = cp.parseConfiguration(configFile);@b@ DefaultShellCallback callback = new DefaultShellCallback(overwrite);@b@ MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,@b@ callback, warnings);@b@ myBatisGenerator.generate(null);@b@ }@b@ public static void main(String[] args) throws Exception {@b@ try {@b@ GeneratorExecuter generatorSqlmap = new GeneratorExecuter();@b@ generatorSqlmap.generator();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@}4.运行结果如下图 四、方法三 (基于eclipse插件方式) 具体使用步骤请参考“使用eclipse的mybatis-generator-plugins插件来生成代码” 五、生成结果如下 1.dao层代码GuserMapper.java import com.xwood.gen.model.Guser;@b@import com.xwood.gen.model.GuserExample;@b@import java.util.List;@b@import org.apache.ibatis.annotations.Param;@b@@b@public interface GuserMapper {@b@ int countByExample(GuserExample example);@b@@b@ int deleteByExample(GuserExample example);@b@@b@ int deleteByPrimaryKey(Integer id);@b@@b@ int insert(Guser record);@b@@b@ int insertSelective(Guser record);@b@@b@ List selectByExample(GuserExample example);@b@@b@ Guser selectByPrimaryKey(Integer id);@b@@b@ int updateByExampleSelective(@Param("record") Guser record, @Param("example") GuserExample example);@b@@b@ int updateByExample(@Param("record") Guser record, @Param("example") GuserExample example);@b@@b@ int updateByPrimaryKeySelective(Guser record);@b@@b@ int updateByPrimaryKey(Guser record);@b@}2.mappers层代码GuserMapper.xml @b@@b@@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ and ${criterion.condition}@b@ @b@ @b@ and ${criterion.condition} #{criterion.value}@b@ @b@ @b@ and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}@b@ @b@ @b@ and ${criterion.condition}@b@ @b@ #{listItem}@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ and ${criterion.condition}@b@ @b@ @b@ and ${criterion.condition} #{criterion.value}@b@ @b@ @b@ and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}@b@ @b@ @b@ and ${criterion.condition}@b@ @b@ #{listItem}@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ id, name, create_by, create_time, update_by, update_time@b@ @b@ @b@ select@b@ @b@ distinct@b@ @b@ @b@ from guser@b@ @b@ @b@ @b@ @b@ order by ${orderByClause}@b@ @b@ @b@ @b@ select @b@ @b@ from guser@b@ where id = #{id,jdbcType=INTEGER}@b@ @b@ @b@ delete from guser@b@ where id = #{id,jdbcType=INTEGER}@b@ @b@ @b@ delete from guser@b@ @b@ @b@ @b@ @b@ @b@ insert into guser (id, name, create_by, @b@ create_time, update_by, update_time@b@ )@b@ values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, @b@ #{createTime,jdbcType=DATE}, #{updateBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=DATE}@b@ )@b@ @b@ @b@ insert into guser@b@ @b@ @b@ id,@b@ @b@ @b@ name,@b@ @b@ @b@ create_by,@b@ @b@ @b@ create_time,@b@ @b@ @b@ update_by,@b@ @b@ @b@ update_time,@b@ @b@ @b@ @b@ @b@ #{id,jdbcType=INTEGER},@b@ @b@ @b@ #{name,jdbcType=VARCHAR},@b@ @b@ @b@ #{createBy,jdbcType=VARCHAR},@b@ @b@ @b@ #{createTime,jdbcType=DATE},@b@ @b@ @b@ #{updateBy,jdbcType=VARCHAR},@b@ @b@ @b@ #{updateTime,jdbcType=DATE},@b@ @b@ @b@ @b@ @b@ select count(*) from guser@b@ @b@ @b@ @b@ @b@ @b@ update guser@b@ @b@ @b@ id = #{record.id,jdbcType=INTEGER},@b@ @b@ @b@ name = #{record.name,jdbcType=VARCHAR},@b@ @b@ @b@ create_by = #{record.createBy,jdbcType=VARCHAR},@b@ @b@ @b@ create_time = #{record.createTime,jdbcType=DATE},@b@ @b@ @b@ update_by = #{record.updateBy,jdbcType=VARCHAR},@b@ @b@ @b@ update_time = #{record.updateTime,jdbcType=DATE},@b@ @b@ @b@ @b@ @b@ @b@ @b@ @b@ update guser@b@ set id = #{record.id,jdbcType=INTEGER},@b@ name = #{record.name,jdbcType=VARCHAR},@b@ create_by = #{record.createBy,jdbcType=VARCHAR},@b@ create_time = #{record.createTime,jdbcType=DATE},@b@ update_by = #{record.updateBy,jdbcType=VARCHAR},@b@ update_time = #{record.updateTime,jdbcType=DATE}@b@ @b@ @b@ @b@ @b@ @b@ update guser@b@ @b@ @b@ name = #{name,jdbcType=VARCHAR},@b@ @b@ @b@ create_by = #{createBy,jdbcType=VARCHAR},@b@ @b@ @b@ create_time = #{createTime,jdbcType=DATE},@b@ @b@ @b@ update_by = #{updateBy,jdbcType=VARCHAR},@b@ @b@ @b@ update_time = #{updateTime,jdbcType=DATE},@b@ @b@ @b@ where id = #{id,jdbcType=INTEGER}@b@ @b@ @b@ update guser@b@ set name = #{name,jdbcType=VARCHAR},@b@ create_by = #{createBy,jdbcType=VARCHAR},@b@ create_time = #{createTime,jdbcType=DATE},@b@ update_by = #{updateBy,jdbcType=VARCHAR},@b@ update_time = #{updateTime,jdbcType=DATE}@b@ where id = #{id,jdbcType=INTEGER}@b@ @b@3.model层代码Guser.java、GuserExample.java public class Guser {@b@ private Integer id;@b@@b@ private String name;@b@@b@ private String createBy;@b@@b@ private Date createTime;@b@@b@ private String updateBy;@b@@b@ private Date updateTime;@b@@b@ public Integer getId() {@b@ return id;@b@ }@b@@b@ public void setId(Integer id) {@b@ this.id = id;@b@ }@b@@b@ public String getName() {@b@ return name;@b@ }@b@@b@ public void setName(String name) {@b@ this.name = name == null ? null : name.trim();@b@ }@b@@b@ public String getCreateBy() {@b@ return createBy;@b@ }@b@@b@ public void setCreateBy(String createBy) {@b@ this.createBy = createBy == null ? null : createBy.trim();@b@ }@b@@b@ public Date getCreateTime() {@b@ return createTime;@b@ }@b@@b@ public void setCreateTime(Date createTime) {@b@ this.createTime = createTime;@b@ }@b@@b@ public String getUpdateBy() {@b@ return updateBy;@b@ }@b@@b@ public void setUpdateBy(String updateBy) {@b@ this.updateBy = updateBy == null ? null : updateBy.trim();@b@ }@b@@b@ public Date getUpdateTime() {@b@ return updateTime;@b@ }@b@@b@ public void setUpdateTime(Date updateTime) {@b@ this.updateTime = updateTime;@b@ }@b@}public class GuserExample {@b@ protected String orderByClause;@b@@b@ protected boolean distinct;@b@@b@ protected List oredCriteria;@b@@b@ public GuserExample() {@b@ oredCriteria = new ArrayList();@b@ }@b@@b@ public void setOrderByClause(String orderByClause) {@b@ this.orderByClause = orderByClause;@b@ }@b@@b@ public String getOrderByClause() {@b@ return orderByClause;@b@ }@b@@b@ public void setDistinct(boolean distinct) {@b@ this.distinct = distinct;@b@ }@b@@b@ public boolean isDistinct() {@b@ return distinct;@b@ }@b@@b@ public List getOredCriteria() {@b@ return oredCriteria;@b@ }@b@@b@ public void or(Criteria criteria) {@b@ oredCriteria.add(criteria);@b@ }@b@@b@ public Criteria or() {@b@ Criteria criteria = createCriteriaInternal();@b@ oredCriteria.add(criteria);@b@ return criteria;@b@ }@b@@b@ public Criteria createCriteria() {@b@ Criteria criteria = createCriteriaInternal();@b@ if (oredCriteria.size() == 0) {@b@ oredCriteria.add(criteria);@b@ }@b@ return criteria;@b@ }@b@@b@ protected Criteria createCriteriaInternal() {@b@ Criteria criteria = new Criteria();@b@ return criteria;@b@ }@b@@b@ public void clear() {@b@ oredCriteria.clear();@b@ orderByClause = null;@b@ distinct = false;@b@ }@b@@b@ protected abstract static class GeneratedCriteria {@b@ protected List criteria;@b@@b@ protected GeneratedCriteria() {@b@ super();@b@ criteria = new ArrayList();@b@ }@b@@b@ public boolean isValid() {@b@ return criteria.size() > 0;@b@ }@b@@b@ public List getAllCriteria() {@b@ return criteria;@b@ }@b@@b@ public List getCriteria() {@b@ return criteria;@b@ }@b@@b@ protected void addCriterion(String condition) {@b@ if (condition == null) {@b@ throw new RuntimeException("Value for condition cannot be null");@b@ }@b@ criteria.add(new Criterion(condition));@b@ }@b@@b@ protected void addCriterion(String condition, Object value, String property) {@b@ if (value == null) {@b@ throw new RuntimeException("Value for " + property + " cannot be null");@b@ }@b@ criteria.add(new Criterion(condition, value));@b@ }@b@@b@ protected void addCriterion(String condition, Object value1, Object value2, String property) {@b@ if (value1 == null || value2 == null) {@b@ throw new RuntimeException("Between values for " + property + " cannot be null");@b@ }@b@ criteria.add(new Criterion(condition, value1, value2));@b@ }@b@@b@ protected void addCriterionForJDBCDate(String condition, Date value, String property) {@b@ if (value == null) {@b@ throw new RuntimeException("Value for " + property + " cannot be null");@b@ }@b@ addCriterion(condition, new java.sql.Date(value.getTime()), property);@b@ }@b@@b@ protected void addCriterionForJDBCDate(String condition, List values, String property) {@b@ if (values == null || values.size() == 0) {@b@ throw new RuntimeException("Value list for " + property + " cannot be null or empty");@b@ }@b@ List dateList = new ArrayList();@b@ Iterator iter = values.iterator();@b@ while (iter.hasNext()) {@b@ dateList.add(new java.sql.Date(iter.next().getTime()));@b@ }@b@ addCriterion(condition, dateList, property);@b@ }@b@@b@ protected void addCriterionForJDBCDate(String condition, Date value1, Date value2, String property) {@b@ if (value1 == null || value2 == null) {@b@ throw new RuntimeException("Between values for " + property + " cannot be null");@b@ }@b@ addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);@b@ }@b@@b@ public Criteria andIdIsNull() {@b@ addCriterion("id is null");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdIsNotNull() {@b@ addCriterion("id is not null");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdEqualTo(Integer value) {@b@ addCriterion("id =", value, "id");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdNotEqualTo(Integer value) {@b@ addCriterion("id ", value, "id");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdGreaterThan(Integer value) {@b@ addCriterion("id >", value, "id");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdGreaterThanOrEqualTo(Integer value) {@b@ addCriterion("id >=", value, "id");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andIdLessThan(Integer value) {@b@ addCriterion("id ", value, "name");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andNameGreaterThanOrEqualTo(String value) {@b@ addCriterion("name >=", value, "name");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andNameLessThan(String value) {@b@ addCriterion("name ", value, "createBy");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andCreateByGreaterThanOrEqualTo(String value) {@b@ addCriterion("create_by >=", value, "createBy");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andCreateByLessThan(String value) {@b@ addCriterion("create_by ", value, "createTime");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andCreateTimeGreaterThanOrEqualTo(Date value) {@b@ addCriterionForJDBCDate("create_time >=", value, "createTime");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andCreateTimeLessThan(Date value) {@b@ addCriterionForJDBCDate("create_time ", value, "updateBy");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andUpdateByGreaterThanOrEqualTo(String value) {@b@ addCriterion("update_by >=", value, "updateBy");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andUpdateByLessThan(String value) {@b@ addCriterion("update_by ", value, "updateTime");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andUpdateTimeGreaterThanOrEqualTo(Date value) {@b@ addCriterionForJDBCDate("update_time >=", value, "updateTime");@b@ return (Criteria) this;@b@ }@b@@b@ public Criteria andUpdateTimeLessThan(Date value) {@b@ addCriterionForJDBCDate("update_time |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |