详解springboot中mybatis注解形式
程序员文章站
2024-02-26 19:10:40
springboot整合mybatis对数据库进行访问,本实例采用注解的方式,如下:
pom.xml文件
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采用多数据源进行配置的方式
上一篇: 深入了解MyBatis参数
推荐阅读
-
SpringBoot 在IDEA中实现热部署步骤详解(实用版)
-
详解MyEclipse中搭建spring-boot+mybatis+freemarker框架
-
Spring中的事务操作、注解及XML配置详解
-
详解springboot+mybatis多数据源最简解决方案
-
springboot中thymeleaf模板使用详解
-
详解SpringBoot中Session超时原理说明
-
详解SpringBoot 快速整合MyBatis(去XML化)
-
详解spring+springmvc+mybatis整合注解
-
SpringBoot中利用MyBatis进行数据操作的示例
-
java 中@Deprecated 注解的实例详解