首頁 > Java > java教程 > springboot中mybatis註解形式的介紹(程式碼)

springboot中mybatis註解形式的介紹(程式碼)

不言
發布: 2018-10-11 15:39:42
轉載
2383 人瀏覽過

這篇文章帶給大家的內容是關於springboot中mybatis註解形式的介紹(程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

springboot整合mybatis對資料庫進行訪問,本實例採用註解的方式,如下:

pom.xml檔案

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
登入後複製

domain類別

package com.rookie.bigdata.domain;

/**
 * @author
 * @date 2018/10/9
 */
public class Student {
    private Long stuNo;
    private String name;
    private Integer age;
    public Student() {
    }
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public Student(Long stuNo, String name, Integer age) {
        this.stuNo = stuNo;
        this.name = name;
        this.age = age;
    }
    public Long getStuNo() {
        return stuNo;
    }
    public void setStuNo(Long stuNo) {
        this.stuNo = stuNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        if (stuNo != null ? !stuNo.equals(student.stuNo) : student.stuNo != null) return false;
        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }
    @Override
    public int hashCode() {
        int result = stuNo != null ? stuNo.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }
    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", name=&#39;" + name + &#39;\&#39;&#39; +
                ", age=" + age +
                &#39;}&#39;;
    }
}
登入後複製

StudentMapper類別

package com.rookie.bigdata.mapper;
import com.rookie.bigdata.domain.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;
/**
 * @author
 * @date 2018/10/9
 */
@Mapper
public interface StudentMapper {

    @Select("SELECT * FROM student WHERE name = #{name}")
    Student findByName(@Param("name") String name);

    @Results({
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age")
    })
    @Select("SELECT name, age FROM student")
    List<Student> findAll();

    @Insert("INSERT INTO student(name, age) VALUES(#{name}, #{age})")
    int insert(@Param("name") String name, @Param("age") Integer age);

    @Update("UPDATE student SET age=#{age} WHERE name=#{name}")
    void update(Student student);

    @Delete("DELETE FROM student WHERE id =#{id}")
    void delete(Long id);

    @Insert("INSERT INTO student(name, age) VALUES(#{name}, #{age})")
    int insertByUser(Student student);

    @Insert("INSERT INTO student(name, age) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
    int insertByMap(Map<String, Object> map);

}
登入後複製

測試類別如下:

package com.rookie.bigdata.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
/**
 * @author
 * @date 2018/10/10
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentMapperTest {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void findByName() throws Exception {
        System.out.println(studentMapper.findByName("zhangsan"));
    }

    @Test
    public void findAll() throws Exception {
        System.out.println(studentMapper.findByName("zhangsan"));
    }

    @Test
    public void insert() throws Exception {
        System.out.println(    studentMapper.insert("zhangsan", 20));
    }

    @Test
    public void update() throws Exception {
    }

    @Test
    public void delete() throws Exception {
    }

    @Test
    public void insertByUser() throws Exception {
    }

    @Test
    public void insertByMap() throws Exception {
    }
}
登入後複製

大家可以自己寫測試類別來測試一下,後續會更新xml的設定方式和mybatis採用多資料來源進行設定的方式

以上是springboot中mybatis註解形式的介紹(程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:cnblogs.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板