mybatis使用

一、Mybatis Generator使用:

配置:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
        <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

命令:

在该项目中(不是父项目):

1、mvn clean package -DskipTests
2、mvn assembly:assembly

assembly配置例子如下:

<plugin>  
     <artifactId>maven-assembly-plugin</artifactId>  
     <version>2.2-beta-5</version>  
     <configuration>  
         <finalName>app</finalName>  
         <attach>false</attach>  
         <descriptors>  
             <descriptor>assembly.xml</descriptor>  
         </descriptors>  
     </configuration>  
     <executions>  
         <execution>  
             <id>make-assembly</id>  
             <phase>package</phase>  
             <goals>  
                 <goal>single</goal>  
             </goals>  
         </execution>  
     </executions>  
 </plugin>

二、自定义SQL:

1、@Select的使用

@Select("SELECT ID,QueryId,Application FROM PS_Queue WHERE ExecStatus = 0 ORDER BY id LIMIT 1 FOR UPDATE;")
Map<String, Object> takeOneQueryToExecute();

2、@SelectProvider的使用 

@SelectProvider(type = TaskMapperProvider.class,method = "getTaskLastModificationDate")
String getTaskLastModificationDate(@Param("taskIds") List<Integer> taskIds);
public class TaskMapperProvider {
    public String getTaskLastModificationDate(Map<String, Object> para) {
        String sql = "SELECT" +
                " CASE WHEN start_time > end_time THEN start_time ELSE end_time END" +
                " FROM" +
                " (" +
                "  SELECT" +
                "   MAX(start_time)AS start_time," +
                "    MAX(end_time)AS end_time" +
                "     FROM" +
                "      etl_task_status" +
                "       WHERE task_id IN(" + StringUtils.join((List) para.get("taskIds"), ",") + ")" +
                " ) tttt";
        return sql;
    }
}


refer:

1、mybatis3中@SelectProvider的使用技巧

http://www.blogjava.net/dbstar/archive/2011/08/08/355825.html


你可能感兴趣的:(mybatis使用)