mybatis使用注解开发

阅读该篇文章将默认您已经能够熟练使用mybatis配置mapper.xml进行开发了。首先把pom.xml贴出来吧。里面有注释 

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.zdsoft</groupId>
    <artifactId>mybatis-test</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <!-- mybatis库 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <!-- json序列化 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <!-- 日志输出 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!-- 数据库访问,mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>mybatis_test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

Java代码mapper如下:

package zdsoft.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import zdsoft.entity.Student;

public interface StudentMapper {
    @Insert("insert into student(id,name,age) values(#{id},#{name},#{age})")
    @SelectKey(keyProperty = "id", before = true, resultType = int.class, statement = "SELECT IFNULL(MAX(id),0)+1 FROM student")
    public void insert(Student stu);

    @Select("select * from student")
    public List<Student> findAll();

    @Delete("delete from student where id=#{id}")
    public int delete(int id);

    @SelectProvider(method = "find", type = StudentBuilder.class)
    public List<Student> find(@Param("name") String name, @Param("age") int age);

    class StudentBuilder {
        public String find(@Param("name") final String name, @Param("age") final int age) {
            return new SQL() {
                {
                    SELECT("*");
                    FROM("student");
                    if (name != null) {
                        WHERE("NAME LIKE CONCAT(#{name},'%')");
                    }
                    if (age > 0) {
                        WHERE("age=#{age}");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

    }
}

其它的就不用多说了,和配置xml一样的。

原文参考:https://www.cnblogs.com/EasonJim/p/7070820.html

原文地址:https://www.cnblogs.com/duanjt/p/8669776.html