springboot中怎么實現(xiàn)mybatis注解形式

這篇文章將為大家詳細(xì)講解有關(guān)springboot中怎么實現(xiàn)mybatis注解形式,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)是一家專業(yè)從事網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計公司,作為專業(yè)的成都網(wǎng)站建設(shè)公司,創(chuàng)新互聯(lián)依托強(qiáng)大的技術(shù)實力、以及多年的網(wǎng)站運營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計開發(fā)服務(wù)!

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='" + name + '\'' +
        ", age=" + age +
        '}';
  }
}

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 {
  }
}

關(guān)于springboot中怎么實現(xiàn)mybatis注解形式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站標(biāo)題:springboot中怎么實現(xiàn)mybatis注解形式
URL分享:http://muchs.cn/article4/ipgioe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站營銷、企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、App設(shè)計移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)