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注解形式的更多相关文章

  1. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  2. 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 ...

  3. SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

    SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...

  4. springboot整合mybatis(注解)

    springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> ...

  5. SpringBoot使用Mybatis注解开发教程-分页-动态sql

    代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...

  6. SpringBoot整合MyBatis(注解版)

    详情可以参考Mybatis官方文档 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ (1). ...

  7. Mybatis 注解形式

        1.查询 // 查询 @Select("select id, name, type, numbers, cancelled, completed, percentage from c ...

  8. springboot(二十一):SpringBoot使用Mybatis注解开发教程-分页-动态sql

    https://blog.csdn.net/KingBoyWorld/article/details/78948304

  9. mybatis 注解形式设置批量新增、批量更新数据

    1. 批量更新: @Update({"<script>" + "<foreach collection=\"smsConfigTemplate ...

随机推荐

  1. koa2学习(二) 中间件router

    中间件 koa-router 安装 npm install --save koa-router 使用 const Koa = require('koa'); const Router = requir ...

  2. Nginx实现集群服务器的负载均衡

    1.安装nginx和tomcat 我这里是使用docker安装的.安装流程可参照 dockerfile 这里安装了两个tomcat,端口分别是42000和42001.第二个tomcat的首页随便加了些 ...

  3. git克隆项目出现remote: HTTP Basic: Access denied

    换新电脑,重新装了git,从gitlab上面拉公司项目,出现了remote: HTTP Basic: Access denied错误,说验证失败,百度很多说了很多答案,最后试了这种可以,成功拉下来项目 ...

  4. CS231n 第一次作业KNN中本地CIFAR10数据集的载入

    一.问题描述 网上绝大多数作业参考都是在jupyter下运行的,数据集载入过程一般如下: from cs231n.data_utils import load_CIFAR10 #导入数据集,并打印出数 ...

  5. [Swift]LeetCode264.丑数 II | Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [Swift]LeetCode870. 优势洗牌 | Advantage Shuffle

    Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...

  7. 安装部署jumpserver3.0

    1.安装依赖包yum -y install git readline-devel automake autoconf2.下载 jumpservergit clone https://github.co ...

  8. IO复用(较详细)

    进程与线程的描述 一个进程至少会创建一个线程,多个线程共享一个程序进程的内存.程序的运行最终是靠线程来完成操作的.线程的数量跟CPU核数有关,一个核最多能发出两个线程.线程的操作主要分为:一:给CPU ...

  9. solr之环境配置四

    Solr链接数据库(mysql,mssql) 一.链接mysql 1.使用DataImportHandler导入并索引数据,配置 $SOLR_HOME\core0\conf\solrconfig.xm ...

  10. solr之环境配置二

    安装配置Tomcat 下载Tomcat压缩包 我下载的是7.0.55版本. 1.Tomcat 7.0 的免安装版的配置(假如将Tomcat 解压到C:\Program Files目录,目录结构为:C: ...