1. 程式人生 > >mybatis使用註解開發

mybatis使用註解開發

plugins state let fig href ons class pub model

閱讀該篇文章將默認您已經能夠熟練使用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

mybatis使用註解開發