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 ...
随机推荐
- 实验5 Spark SQL编程初级实践
今天做实验[Spark SQL 编程初级实践],虽然网上有答案,但都是用scala语言写的,于是我用java语言重写实现一下. 1 .Spark SQL 基本操作将下列 JSON 格式数据复制到 Li ...
- 串口RS232和485通信的波形分析
一.串行数据的格式 异步串行数据的一般格式是:起始位+数据位+停止位,其中起始位1 位,数据位可以是5.6.7.8位,停止位可以是1.1.5.2位. 起始位是一个值为0的位,所以对于正逻辑的TTL电平 ...
- Anveshak: Placing Edge Servers In The Wild
Anveshak:在野外放置边缘服务器 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促 ...
- nova vnc proxy基本原理
先上图 VNC Proxy的功能: 将公网(public network)和私网(private network)隔离 VNC client运行在公网上,VNCServer运行在私网上,VNC Pro ...
- Linux atop监控
200 ? "200px" : this.width)!important;} --> 介绍 atop是一个功能非常强大的linux服务器监控工具,它的数据采集主要包括:CP ...
- [Swift]LeetCode283. 移动零 | Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- Hive篇--相关概念整理一
一.前述 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低 ...
- 【Git】(2)---checkout、branch、log、diff、.gitignore
常用命令 一.命令 1.checkout 切换分支 git checkout 分支名 #切换分支 #如果在当前分支上对文件进行修改之后,没有commit就切换到另外一个分支b, 这个时候会报错,因为没 ...
- vs17 破解密钥
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(VS201 ...
- EFCore合并多条迁移记录
方法来自 merge-migrations-in-entity-framework-core 更新数据库到最新结构 删除迁移目录下的所有迁移脚本 新建一个迁移 注释掉Up()和Down()方法中的代码 ...