一、maven项目的使用方式
1、生成文件的最终结果如下
2、pom generator插件的配置
3、generatorConfig的配置
<?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> <!-- 此处路径自己配置 --> <classPathEntry location="D:\Java\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" /> <context id="context1" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example" /> <property name="replaceString" value="Criteria" /> </plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx:3306/xx?useUnicode=true&characterEncoding=UTF-8" userId="xxx" password="xxx" /> <!-- 此处是将Example改名为Criteria 当然 想改成什么都行 --> <!--DTO的路径 --> <javaModelGenerator targetPackage="com.xxx.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成SQL-XML的路径 --> <sqlMapGenerator targetPackage="mapper.xxx" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--DAO的路径 --> <javaClientGenerator targetPackage="com.xxx.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="t_test" domainObjectName="TestValue"> <generatedKey column="id" sqlStatement="MySql" identity="true" /> </table> </context> </generatorConfiguration>
4、运行插件生成配置文件
http://blog.csdn.net/mynamepg/article/details/79414871
http://blog.csdn.net/mynamepg/article/details/79414948
面向对象查询Criteria:
示例1, 查询列表:
ConnPersonDepartCriteria query = new ConnPersonDepartCriteria(); ConnPersonDepartCriteria.Criteria critria = query.createCriteria(); critria.andCompanyIdEqualTo(company); if(department != null) critria.andDepartIdEqualTo(department); query.setOrderByClause("person_id desc"); List<ConnPersonDepart> list = connPersonDepartMapper.selectByExample(query);
示例2,统计:
ConnPersonDepartCriteria query = new ConnPersonDepartCriteria();
query.createCriteria().andCompanyIdEqualTo(company).andDepartIdEqualTo(department); return connPersonDepartMapper.countByExample(query);示例3, 删除:
ConnPersonDepartCriteria query=new ConnPersonDepartCriteria(); query.createCriteria().andCompanyIdEqualTo(companyId); connPersonDepartMapper.deleteByExample(query);