springboot中怎么实现mybatis注解形式
这篇文章将为大家详细讲解有关springboot中怎么实现mybatis注解形式,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联是一家专业从事网站设计、网站建设、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!
pom.xml文件
org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web MySQL mysql-connector-java 5.1.45 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-configuration-processor true org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.springframework.boot spring-boot-maven-plugin
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") ListfindAll(); @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 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 { } }
关于springboot中怎么实现mybatis注解形式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前标题:springboot中怎么实现mybatis注解形式
分享网址:http://lswzjz.com/article/ipgioe.html