资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot集成Mybatis的实例代码(简洁版)

概述

我们提供的服务有:成都做网站、成都网站制作、微信公众号开发、网站优化、网站认证、月湖ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的月湖网站制作公司

现在互联网应用中,大部分还是使用Mybatis来操作数据库的,本文介绍一下Spring Boot中如何集成Mybatis。

上篇介绍了Spring Boot 直接用jar运行项目的方法,需要的朋友点击查看。

创建Spring Boot工程

在 Spring Boot 开篇-创建和运行 一文中有一个小节介绍了如何使用Spring Boot的组件来创建工程。如果要集成Mybatis,只需要把MySQL和Mybatis这两个组件勾选一下即可。

Spring Boot集成Mybatis的实例代码(简洁版) 

当然也可以不通过这种方式,直接在POM.xml文件中添加依赖也是可以的。我选择的是直接在POM.xml文件中直接添加依赖这种方式。

dependency>
  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  1.3.1


  mysql
  mysql-connector-java
  5.1.34


  com.alibaba
  druid
  1.1.7

数据源使用阿里的druid。完整的POM.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0
 com.springboot
 study
 0.0.1-SNAPSHOT
 jar
 study
 Demo project for Spring Boot
 
 org.springframework.boot
 spring-boot-starter-parent
 1.5.10.RELEASE
  
 
 
 UTF-8
 UTF-8
 1.8
 
 
 
  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  1.3.1
 
 
  org.springframework.boot
  spring-boot-starter-web
 
 
  mysql
  mysql-connector-java
  5.1.34
 
 
  com.alibaba
  druid
  1.1.7
 
 
  com.alibaba
  fastjson
  1.2.45
 
 
  org.springframework.boot
  spring-boot-starter-test
  test
 
 
 
 
  
  org.springframework.boot
  spring-boot-maven-plugin
  
 
 

创建table

CREATE TABLE `user` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='user信息';

创建entity

package com.springboot.entity;
public class User {
 private Long id;
 private String name;
 public Long getId() {
 return id;
 }
 public void setId(Long id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return "User{" +
  "id=" + id +
  ", name='" + name + '\'' +
  '}';
 }
}

创建Mybatis映射文件和repo

UserRepo.java

package com.springboot.repo;
import com.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Mapper
public interface UserRepo {
 int insert(User user);
 User selectByPrimaryKey(Long id);
 int updateByPrimaryKey(User user);
 int deleteByPrimaryKey(Long id);
 List selectAll();
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>


 
 
 
 
 
 id,
 name
 
 
 
 
 update user
 
  
  `name`= #{name,jdbcType=VARCHAR},
  
 
 where id = #{id,jdbcType=BIGINT}
 
 
 delete from user
 where id = #{id,jdbcType=BIGINT}
 
 
 insert into user
 
  name
 
 
  #{name,jdbcType=VARCHAR}
 
 

编写application.properties

在Spring Boot为我们生成的application.properties文件中添加如下内容:

spring.datasource.name=spring_boot_study spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.springboot.entity

单元测试

package com.springboot;
import com.springboot.entity.User;
import com.springboot.repo.UserRepo;
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 java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
 @Autowired
 private UserRepo userRepo;
 @Test
 public void testInsert() {
 User user = new User();
 user.setName("test2");
 userRepo.insert(user);
 }
 @Test
 public void testUpdate() {
 User user = new User();
 user.setId(6L);
 user.setName("test3");
 userRepo.updateByPrimaryKey(user);
 }
 @Test
 public void testDelete() {
 userRepo.deleteByPrimaryKey(6L);
 }
 @Test
 public void testSelectByPrimaryKey() {
 User user = userRepo.selectByPrimaryKey(7L);
 System.out.println(user);
 }
 @Test
 public void testSelectAll() {
 List userList = userRepo.selectAll();
 System.out.println(userList);
 }
}

总结

以上所述是小编给大家介绍的Spring Boot集成Mybatis的实例代码(简洁版),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


网页名称:SpringBoot集成Mybatis的实例代码(简洁版)
标题URL:http://cdkjz.cn/article/iichjs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220