Springboot Mybatis实现对数据库增删改查记录

1.步骤

参考博客
https://blog.csdn.net/m0_37284598/article/details/81240843
1.1 建立springboot工程
1.2 在MySQL Workbench中建立数据库表
在这里插入图片描述
建立数据库名为user(SCHEMAS),表名为user。注意数据库名要与工程中application.properties文件的数据库URL相对应
在这里插入图片描述
1 为数据库连接地址,如下图
在这里插入图片描述
2 为数据库名,对应SCHEMAS
在这里插入图片描述
1.3 编辑代码
参照参考博客编写:
entity层的实体类User;
mapper层的UserMapper类;
service层的实现类UserService;
controller层的实现类CRUD;
最终文件目录结构如下:
在这里插入图片描述

UserMapper的映射文件UserMapper.xml。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
    <resultMap id="result" type="com.example.demo.entity.User">
        <result property="name" column="name" />
        <result property="password" column="password"/>
        <result property="number" column="number"/>

    </resultMap>
    <select id="ListUser" resultMap="result">
        SELECT * FROM user
    </select>

    <select id="findUserByName" resultMap="result">
        SELECT * FROM user where name=#{name}
    </select>

    <insert id="insertUser" parameterType="com.example.demo.entity.User"
            keyProperty="id" useGeneratedKeys="true">
        INSERT INTO user(id,name,gender,job,password,number)
        VALUES (#{id},
                #{name, jdbcType=VARCHAR},
                #{gender, jdbcType=VARCHAR},
                #{job, jdbcType=VARCHAR},
                #{password, jdbcType=VARCHAR},
                #{number} )
    </insert>

    <delete id="delete" parameterType="int">
        delete from user where id=#{id}
    </delete>

    <update id="Update" parameterType="com.example.demo.entity.User">
    update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
    </update>
</mapper>

编写配置文件application.properties

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username = root
spring.datasource.password = 199499
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.max-active=20   //指定连接池中最大的活跃连接数
spring.datasource.max-idle=8       //指定连接池中连接的最大的空闲连接数量
spring.datasource.min-idle=8       //指定必须保持连接的最小值
spring.datasource.initial-size=10    //指定启动连接池时,初始建立的连接数量
mybatis.mapper-locations= classpath:mapper/*.xml

注意url要与上文数据库连接和名称对应。
工程创建完毕

2.遇到的问题及解决

2.1 userMmapper错误
参考:https://blog.csdn.net/weixin_41935702/article/details/88852598
在这里插入图片描述
2.2 sql语句报错或者无法识别数据库名(数据库报错汇总)
(1)No data sources are configured to run this SQL and provide advanced以及Cannot resolve table问题
参考:https://blog.csdn.net/xiaocy66/article/details/82766635
IDEA中Mysql数据库的连接需要配置。
按ctrl+shift+a,搜索Data Sources,先点左上角的加号选Mysql,然后添加在Mysql中已经创建好的数据库信息
在这里插入图片描述
(2)上一步Test Connection错误问题(Server returns invalid timezone)

参考:https://blog.csdn.net/ITMan2017/article/details/100601438
在这里插入图片描述
设置时区即可
(3)选择所有表
在这里插入图片描述
至此,数据库配置完成。
(4)IDEA警告:SQL dialect is not configured
参考:https://blog.csdn.net/qq_30833275/article/details/85092663
在报警告的地方,单击过后,按下快捷键Alt+Enter进入设置。
将SQl dialect设置为Mysql。

2.3 编译报错java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
在application.properties中修改url
在这里插入图片描述
在uesr后加

?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT

完整为

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT

至此,完美运行增删改查。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值