0
点赞
收藏
分享

微信扫一扫

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查



前面我们讲完了一些java和springboot的基础知识以后,今天我们就来讲下springboot实现数据库的管理。


目前比较主流的方式有两种

1,springboot结合mybatis管理数据库

2,springboot结合jpa管理数据

这两种方式各有各的好,今天我们就先来讲讲springboot2结合mybatis实现数据的增删改查操作,下一节我们再讲jpa。

一,在pom.xml里添加mybatis依赖。

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot2

如上图所示,我们需要添加mybatis和mysql这两个依赖。代码如下

<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

这里做下简单说明

mybatis库是我们实现mybatis的核心。

mysql库,是用来驱动管理数据库用的。

二,在application.yml里配置如下信息

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot2_02

上图就是做了,本地服务器端口配置,数据库关联配置。重要的是下面这段。

mybatis:
mapper-locations: classpath:/mybatis/mapper/*.xml
config-location: classpath:/mybatis/config/mybatis-config.xml

  • mapper-locations 是配置我们mapper.xml文件的位置,我们把它配置在/src/main/resource/mybatis/mapper目录下。
  • config-locations 是配置mybatis-confg.xml文件的位置。我们把它配置在/src/main/resource/mybatis/config目录下。

接下来我们在resource目录下新建mybatis目录,然后在mybatis目录下新建/mapper目录和/config目录

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mysql_03

三,配置mybatis的配置xml文件

1,在/src/main/resource/mybatis/config目录下新建mybatis-config.xml

具体代码如下。

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot2_04

把代码给大家贴出来。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 数据库表对应的bean -->
<typeAliases>
<package name="com.shitou.springbootdemos.mybatis.bean"/>
</typeAliases>
</configuration>

这里要注意的是,typeAliases下的package配置的是我们实体类所在目录。

<typeAliases>
<package name="com.shitou.springbootdemos.mybatis.bean"/>
</typeAliases>

如我们的User实体类,在下图所示的这个目录

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot2_05

2,在/src/main/resource/mybatis/mapper目录下新建UserMapper.xml

这个UserMapper.xml里就是我们要执行的一些sql语句。先把代码给大家贴出来。

<?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">
<!--声明对应bean的命名空间-->
<mapper namespace="com.shitou.springbootdemos.mybatis.repository.UserMapper">

<!-- 查询到的结果返回样式-->
<resultMap id="SysUserResultMap" type="User">
<id property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result property="name" column="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result property="age" column="AGE" javaType="java.lang.Integer" jdbcType="INTEGER"/>
</resultMap>

<!--增-->
<insert id="save" parameterType="User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>

<!-- 删-->
<delete id="deleteById">
delete from user where id=#{id}
</delete>

<!-- 改-->
<update id="update" parameterType="User">
update user
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>


<!-- 查-->
<select id="selectAll" resultMap="SysUserResultMap">
select * from user
</select>
<select id="selectById" resultMap="SysUserResultMap">
select
*
from user
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>

四,创建User实体类。

package com.shitou.springbootdemos.mybatis.bean;

/**
* user数据表对应的bean
*/
public class User {
private Integer id;
private String name;
private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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 String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}

我们这里就简单的创建一个user类,和我们的user表对应,只有id,name,age三个字段

五,创建UserMapper接口,用来做增删改查操作

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mysql_06

代码贴出来给大家

/**
* user表对应的mapper
*/
public interface UserMapper {
//新增用户

int save(User user);

//更新用户信息
int update(User user);

//根据id删除
int deleteById(int id);

//根据id查询
User selectById(int id);

//查询所有用户信息
List<User> selectAll();
}

六,创建user表。

我创建数据库表是用idea自带的可视化管理工具创建的,我前面有写文章讲过,如果借助idea来可视化管理mysql数据库。大家可以去翻看我之前的文章。

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mybatis_07

可视化建表

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mysql_08

这时候mybatis的配置还不算成功,我们需要把mapper的路径暴露给spring 让它来扫描管理,所以我们需要在Chapter4Application.java文件上加上注解@MapperScan(“com.shitou.springbootdemos.mybatis.repository”) 扫描mapper的所在位置

七,在启动类里自动扫描我们定义的mapper接口

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mysql_09

至此。我们mybaits的接口算是编写完毕,接下来我们来测试一下吧。

八,定义controller

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_java_10

如上图所示,我们定义一个controller来实现一个增加数据的接口。和一个查询所有用户的接口。

1, 启动项目。

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot2_11

2, 在浏览器中访问save接口

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_springboot_12

返回1代表添加新用户成功,到user表里看下,也能看到数据添加成功

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_mybatis_13

3,在浏览器里访问getAll接口

java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查_java_14

可以看到我们成功的访问到我们上一步添加的数据。

到这里我们就轻松的实现了springboot2结合mybatis来管理数据库的功能了

源码:

​​https://github.com/qiushi123/springboot-demos​​


举报

相关推荐

0 条评论