Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第一步:

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第二步:

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第三步:File--->New--->others

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第四步:选择项目,finish

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第五步:打开项目中这个xml文件

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

第六步:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables">
<commentGenerator>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true" />  
        </commentGenerator>

 <!-- JDBC驱动信息,我用的是sql server,其他的自行搜索 -->  
<jdbcConnection 
connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=DatabaseName
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
password="XXXXXX" 
userId="sa" 
/>

<javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>

<!-- targetProject:生成PO类的位置 -->  
<javaModelGenerator 
targetPackage="com.test.model"
targetProject="TEST/src" >
<!-- enableSubPackages:是否让schema作为包的后缀 -->  
            <property name="enableSubPackages" value="true" />  
            <!-- 从数据库返回的值被清理前后的空格 -->  
            <property name="trimStrings" value="true" />  
        </javaModelGenerator> 
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator 
targetPackage="mappers" 
targetProject="TEST/src" >
<!-- enableSubPackages:是否让schema作为包的后缀 -->  
            <property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->  
<javaClientGenerator 
targetPackage="com.test.dao"
targetProject="TEST/src" 
type="XMLMAPPER" >
<!-- enableSubPackages:是否让schema作为包的后缀 -->  
            <property name="enableSubPackages" value="false" /> 
</javaClientGenerator> 

  <!-- 要生成的数据库中的表 -->
<table tableName="Campus">
<!-- <columnOverride column="???" property="???" /> -->
</table>
</context>
</generatorConfiguration>

第七步:右击xml文件

Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud

至此代码到dao层已生成;

service层自己复制下就ok;

使用:

增:

  Campus record = new Campus();
record.setName("XX大学XX校区");
record.setRemark("XX路123号");
//insertSelective 可巧妙的避开id,如果用insert有自增id会出问题,可见我上一篇博客
campusService.insertSelective(record);

删:

  CampusExample example = new CampusExample();
example.or().andNameEqualTo("XX大学XX校区");

  campusService.deleteByExample(example);

改:

  Campus record = new Campus();
record.setName("XX大学XX校区");
record.setRemark("XX路234号");//要修改信息

  CampusExample example = new CampusExample();
example.or().andNameEqualTo("XX大学XX校区");//where 后参数

 campusService.updateByExampleSelective(record, example);

查:

 CampusExample example = new CampusExample();
example.or().andNameEqualTo("XX大学XX校区");//where 后参数

 List<Campus> list= campusService.selectByExample(example);
JSONArray json = new JSONArray();
json.put(list);
return json.toString();

更详细的用法参见:

https://www.cnblogs.com/pixy/p/5038275.html