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类
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 { } }
大家可以自己编写测试类进行测试一下,后续会更新xml的配置方式和mybatis采用多数据源进行配置的方式
上一篇: gcc的编译流程详解
下一篇: C语言左右法则
推荐阅读
-
mybatis学习笔记之mybatis注解配置详解
-
springboot使用Mybatis(xml和注解)过程全解析
-
SpringBoot系列-整合Mybatis(注解方式)
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
SpringBoot之集成MyBatis
-
springboot之mybatis注解形式
-
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
-
源码系列【springboot之@Import注解多个类引入同一个类源码解析】
-
SpringBoot整合Mybatis注解实战 -- 插入数据
-
springboot 整合Mybatis步骤(有mybatis核心配置文件mybatis-config.xml的形式)