<typeAliases>
<package name="com.shitou.springbootdemos.mybatis.bean"/>
</typeAliases>
如我们的User实体类,在下图所示的这个目录
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>
=========================================================================
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三个字段
=======================================================================================
代码贴出来给大家
/**
* 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();
}
=======================================================================
我创建数据库表是用idea自带的可视化管理工具创建的,我前面有写文章讲过,如果借助idea来可视化管理mysql数据库。大家可以去翻看我之前的文章。
可视化建表
这时候mybatis的配置还不算成功,我们需要把mapper的路径暴露给spring 让它来扫描管理,所以我们需要在Chapter4Application.java文件上加上注解@MapperScan(“com.shitou.springbootdemos.mybatis.repository”) 扫描mapper的所在位置
=====================================================================================
至此。我们mybaits的接口算是编写完毕,接下来我们来测试一下吧。
===========================================================================
如上图所示,我们定义一个controller来实现一个增加数据的接口。和一个查询所有用户的接口。
1, 启动项目。
2, 在浏览器中访问save接口
返回1代表添加新用户成功,到user表里看下,也能看到数据添加成功
3,在浏览器里访问getAll接口
可以看到我们成功的访问到我们上一步添加的数据。
到这里我们就轻松的实现了springboot2结合mybatis来管理数据库的功能了
================================================================
https://github.com/qiushi123/springboot-demos
=================================================================
https://edu.csdn.net/course/detail/23443
=================================================================