您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

springboot中mybatis注解形式的介绍(代码)

2024/3/5 6:28:06发布40次查看
本篇文章给大家带来的内容是关于springboot中mybatis注解形式的介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
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 */@mapperpublic 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)@springboottestpublic 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采用多数据源进行配置的方式
以上就是springboot中mybatis注解形式的介绍(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product