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
*/
@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采用多数据源进行配置的方式
springboot之mybatis注解形式的更多相关文章
- springboot整合redis(注解形式)
springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- SpringBoot使用Mybatis注解进行一对多和多对多查询(2)
SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...
- springboot整合mybatis(注解)
springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> ...
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...
- SpringBoot整合MyBatis(注解版)
详情可以参考Mybatis官方文档 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ (1). ...
- Mybatis 注解形式
1.查询 // 查询 @Select("select id, name, type, numbers, cancelled, completed, percentage from c ...
- springboot(二十一):SpringBoot使用Mybatis注解开发教程-分页-动态sql
https://blog.csdn.net/KingBoyWorld/article/details/78948304
- mybatis 注解形式设置批量新增、批量更新数据
1. 批量更新: @Update({"<script>" + "<foreach collection=\"smsConfigTemplate ...
随机推荐
- OJ002
register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对. 因为,如果定义了很多register变量,可能会超过CPU的寄 ...
- JS Fetch
使用Fetch 1.进行 fetch 请求 一个基本的 fetch请求设置起来很简单.看看下面的代码: fetch('http://example.com/movies.json') .then(fu ...
- ASP.NET Core知多少(7):对重复编译说NO -- dotnet watch
ASP.NET Core知多少系列:总体介绍及目录 1. 引言 我们一般的开发过程,就是编码-->编译-->运行-->调试-->定位问题--->修改代码-->编译- ...
- 用Vue2仿京东省市区三级联动效果
三级联动,随着越来越多的审美,出现了很多种,好多公司都仿着淘宝的三级联动 ,好看时尚,so我们公司也一样……为了贴代码方便,我把写在data里面省市区的json独立了出来,下载贴进去即可用,链接如下 ...
- MySQL乐观锁为什么可以防止并发
问题引入 本文介绍的是最常用的也是mysql默认的innoDB引擎 Read committed隔离级别下事物的并发.这种情况下的事物特点是 读:在一个事物里面的select语句 不会受到其他事物(不 ...
- [Swift]LeetCode483. 最小好进制 | Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [Swift]LeetCode967. 连续差相同的数字 | Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- Ceres Solver 在win8+vs2013环境下的安装
参考博文:https://blog.csdn.net/wzheng92/article/details/79504709
- ASP.NET Core Web API 与 SSL
SSL 一直没有真正研究过SSL,不知道下面的理解是否正确. SSL是Secure Sockets Layer的缩写,它用来保护服务器和客户端之前的通信.它是基于信任+加密的概念. 在介绍SSL的原理 ...
- Javascript sort方法
sort()方法用于对数组的元素进行排序 语法:array.Object.sort(sortBy) sortBy:可选.规定排序顺序.必须是函数 返回值:对数组的引用.数组在原数组上进行排序,不生成副 ...