服务器之家

服务器之家 > 正文

详解springboot中mybatis注解形式

时间:2021-06-03 13:48     来源/作者:Java之家

springboot整合mybatis对数据库进行访问,本实例采用注解的方式,如下:

pom.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<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类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
 
}

测试类如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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采用多数据源进行配置的方式

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部