欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-24 17:22:04
概述 现在互联网应用中,大部分还是使用mybatis来操作数据库的,本文介绍一下spring boot中如何集成mybatis。 上篇介绍了spring boot 直接...

概述

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

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

创建spring boot工程

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

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

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

dependency>
  <groupid>org.mybatis.spring.boot</groupid>
  <artifactid>mybatis-spring-boot-starter</artifactid>
  <version>1.3.1</version>
</dependency>
<dependency>
  <groupid>mysql</groupid>
  <artifactid>mysql-connector-java</artifactid>
  <version>5.1.34</version>
</dependency>
<dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>druid</artifactid>
  <version>1.1.7</version>
</dependency>

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

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.springboot</groupid>
 <artifactid>study</artifactid>
 <version>0.0.1-snapshot</version>
 <packaging>jar</packaging>
 <name>study</name>
 <description>demo project for spring boot</description>
 <parent>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-parent</artifactid>
 <version>1.5.10.release</version>
 <relativepath/> <!-- lookup parent from repository -->
 </parent>
 <properties>
 <project.build.sourceencoding>utf-8</project.build.sourceencoding>
 <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
 <java.version>1.8</java.version>
 </properties>
 <dependencies>
 <dependency>
  <groupid>org.mybatis.spring.boot</groupid>
  <artifactid>mybatis-spring-boot-starter</artifactid>
  <version>1.3.1</version>
 </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.34</version>
 </dependency>
 <dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>druid</artifactid>
  <version>1.1.7</version>
 </dependency>
 <dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>fastjson</artifactid>
  <version>1.2.45</version>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-test</artifactid>
  <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
  <plugin>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-maven-plugin</artifactid>
  </plugin>
 </plugins>
 </build>
</project>

创建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<user> selectall();
}

usermapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.springboot.repo.userrepo" >
 <resultmap id="baseresultmap" type="com.springboot.entity.user" >
 <id column="id" property="id" jdbctype="bigint" />
 <result column="name" property="name" jdbctype="varchar" />
 </resultmap>
 <sql id="base_column_list" >
 id,
 name
 </sql>
 <select id="selectbyprimarykey" resultmap="baseresultmap" parametertype="java.lang.long" >
 select
 <include refid="base_column_list" />
 from user
 where id = #{id,jdbctype=bigint}
 </select>
 <select id="selectall" resultmap="baseresultmap">
 select
 <include refid="base_column_list" />
 from user
 </select>
 <update id="updatebyprimarykey" parametertype="com.springboot.entity.user" >
 update user
 <set>
  <if test="name != null" >
  `name`= #{name,jdbctype=varchar},
  </if>
 </set>
 where id = #{id,jdbctype=bigint}
 </update>
 <delete id="deletebyprimarykey" parametertype="java.lang.long" >
 delete from user
 where id = #{id,jdbctype=bigint}
 </delete>
 <insert id="insert" parametertype="com.springboot.entity.user" usegeneratedkeys="true" keyproperty="id">
 insert into user
 <trim prefix="(" suffix=")" suffixoverrides="," >
  name
 </trim>
 <trim prefix="values (" suffix=")" suffixoverrides="," >
  #{name,jdbctype=varchar}
 </trim>
 </insert>
</mapper>

编写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<user> userlist = userrepo.selectall();
 system.out.println(userlist);
 }
}

总结

以上所述是小编给大家介绍的spring boot集成mybatis的实例代码(简洁版),希望对大家有所帮助