最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解springboot中mybatis注解形式

 更新時(shí)間:2018年10月12日 14:08:23   投稿:laozhang  
在本文中小編給大家分享了關(guān)于springboot中mybatis注解形式的介紹,有興趣的可以跟著學(xué)習(xí)下。

springboot整合mybatis對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪問,本實(shí)例采用注解的方式,如下:

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);
 
}

測(cè)試類如下:

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

大家可以自己編寫測(cè)試類進(jìn)行測(cè)試一下,后續(xù)會(huì)更新xml的配置方式和mybatis采用多數(shù)據(jù)源進(jìn)行配置的方式

相關(guān)文章

  • Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘

    Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘

    這篇文章主要給大家介紹了關(guān)于Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL的相關(guān)資料,小編覺得挺不錯(cuò)的,對(duì)大家學(xué)習(xí)或者使用Mybatis會(huì)有一定的幫助,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-11-11
  • Spring打包jar包時(shí)jsp頁(yè)面無(wú)法訪問問題解決

    Spring打包jar包時(shí)jsp頁(yè)面無(wú)法訪問問題解決

    這篇文章主要介紹了Spring打包jar包時(shí)jsp頁(yè)面無(wú)法訪問問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 解決idea 項(xiàng)目編譯后沒有class文件的問題

    解決idea 項(xiàng)目編譯后沒有class文件的問題

    這篇文章主要介紹了解決idea 項(xiàng)目編譯后沒有class文件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-12-12
  • 聊聊Redis二進(jìn)制數(shù)組Bitmap

    聊聊Redis二進(jìn)制數(shù)組Bitmap

    這篇文章主要介紹了Redis二進(jìn)制數(shù)組Bitmap,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring aop兩種配置方式

    spring aop兩種配置方式

    這篇文章主要為大家詳細(xì)介紹了spring aop兩種配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以參考下
    2015-09-09
  • Mybatis映射文件規(guī)則實(shí)例詳解

    Mybatis映射文件規(guī)則實(shí)例詳解

    在映射文件當(dāng)中,mapper元素是映射文件的根元素,其他的標(biāo)簽都是它的子元素,下面這篇文章主要給大家介紹了關(guān)于Mybatis映射文件規(guī)則的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 搭建Springboot框架并添加JPA和Gradle組件的方法

    搭建Springboot框架并添加JPA和Gradle組件的方法

    這篇文章主要介紹了搭建Springboot框架并添加JPA和Gradle組件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-07-07
  • Java編程實(shí)現(xiàn)swing圓形按鈕實(shí)例代碼

    Java編程實(shí)現(xiàn)swing圓形按鈕實(shí)例代碼

    這篇文章主要介紹了Java編程實(shí)現(xiàn)swing圓形按鈕實(shí)例代碼,涉及兩個(gè)簡(jiǎn)單的Java實(shí)現(xiàn)按鈕的代碼,其中一個(gè)具有偵測(cè)點(diǎn)擊事件的簡(jiǎn)單功能,具有一定借鑒價(jià)值,需要的朋友可以參考。
    2017-11-11
  • 使用javaMail實(shí)現(xiàn)發(fā)送郵件

    使用javaMail實(shí)現(xiàn)發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了使用javaMail實(shí)現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Java實(shí)現(xiàn)的自定義迭代器功能示例

    Java實(shí)現(xiàn)的自定義迭代器功能示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的自定義迭代器功能,結(jié)合具體實(shí)例形式分析了java簡(jiǎn)單迭代器的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04

最新評(píng)論

山丹县| 和龙市| 宜春市| 海宁市| 通许县| 望城县| 神木县| 田阳县| 鄂托克前旗| 班戈县| 渑池县| 宁强县| 桐梓县| 土默特右旗| 岢岚县| 合江县| 左贡县| 平定县| 丹寨县| 新疆| 中超| 库车县| 福州市| 台南市| 勐海县| 曲水县| 独山县| 江安县| 紫金县| 吴川市| 南阳市| 乐陵市| 贡嘎县| 白水县| 长顺县| 潍坊市| 宁远县| 义乌市| 甘孜| 青龙| 宁安市|