欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot之mybatis注解形式

程序员文章站 2022-05-21 10:49:29
springboot整合mybatis对数据库进行访问,本实例采用注解的方式,如下: pom.xml文件 domain类 package com.rookie.bigdata.domain; /** * @author * @date 2018/10/9 */ public class Studen ......

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类

springboot之mybatis注解形式
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 +
                '}';
    }
}
view code
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采用多数据源进行配置的方式