mybatis操作数据库

在这里插入图片描述

1.引入第三方工具mybatis的场景启动器
y在这里插入图片描述

如果有错误可以根据Ide提示修改

2.在resources下建立mybatis文件写入mybatis-config.xml全局配置文件

<?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="mapUnderscoreToCameCase" value="ture"/>   //开启驼峰命名规则
      </settings>
</configuration>

再在mybatis下建立mapper文件夹,放入AccountMapper.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.ml.admin.mapper.AccountMapper">
    <select id="getAccount_id" resultType="com.ml.admin.bean.Account">
        select * from student where id = #{id}
    </select>
</mapper>

另一个项目中mapper.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.example.music.mapper.ListSongMapper">
    <resultMap id="BaseSelect" type="com.example.music.entity.ListSong">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="song_id" jdbcType="INTEGER" property="songId"/>
        <result column="song_list_id" jdbcType="INTEGER" property="songListId"/>
    </resultMap>
    <!--增加歌单歌曲-->
    <insert id="insertListSong" parameterType="com.example.music.entity.ListSong">
        insert into list_song
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="songId!=null">song_id,</if>
            <if test="songListId!=null">song_list_id</if>
        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="songId!=null">#{songId},</if>
            <if test="songListId!=null">#{song_list_id}</if>
        </trim>
    </insert>
    <!--删除歌单歌曲-->
    <delete id="deleteListSong" parameterType="java.lang.Integer">
        delete
        from list_song
        where id = #{id}
    </delete>
    <!--修改歌单歌曲-->
    <update id="updateListSong" parameterType="com.example.music.entity.ListSong">
        update list_song
        <set>
            <if test="songId!=null">song_id=#{songId}</if>
            <if test="songListId!=null">song_list_id#{songListId}</if>
        </set>
        where id = #{id}
    </update>
    <!--根据主键id查询歌单歌曲-->
    <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseSelect">
        select *
        from list_song
        where id = # {id}
    </select>
    <!--查询所有的歌曲歌单-->
    <select id="selectAll" resultMap="BaseSelect">
        select *
        from list_song
    </select>
    <!--根据歌单id查询所有的歌曲-->
    <select id="selectBySongListId" resultMap="BaseSelect">
        select *
        from list_song
        where song_list_id = #{songListId}
    </select>

</mapper>

3.在bean下写一个Acount类封装student表中的信息,如下

————————————————————————————————————————————————————————————
package com.ml.admin.bean;

import lombok.Data;

@Data
public class Account {
private Integer id;
private String name;
private Integer age;
}
————————————————————————————————————————————————————————————
4.在mapper文件下写一个AccountMapper接口操作数据表student表,如下

@Mapper
public interface AccountMapper {
public Account getAccount_id(Integer id);
}
————————————————————————————————————————————————————————————
5.在application.yaml中配置mybatis映射规则

spring:
  datasource:
    username: root
    password: 123456
    #假如时区报错了,就增加一个时区配置即可
    url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
#   指定数据源的类型,spring默认的数据源是hikari,指定我们自己引入的德鲁伊数据源
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat,wall,log4j
  jackson:
    date-format:  yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  servlet:
    multipart:
      enabled: true
      max-file-size: 2000MB
      max-request-size: 2000MB
mybatis:
  mapper-locations: classpath:/mybatis/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 8888
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件路径
  mapper-locations: classpath:mybatis/mapper/*.xml   #sql映射文件路径

6.在service下创建AccountService.java文件用于实现AccountMapper接口,如下

————————————————————————————————————————————————————————————

package com.ml.admin.service;

import com.ml.admin.bean.Account;
import com.ml.admin.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {
@Autowired
AccountMapper accountMapper;

public Account get_by_id(Integer id) {
    return accountMapper.getAccount_id(id);
}

}

————————————————————————————————————————————————————————————

7.使用@ResponseBody返回查询数据,代码如下
————————————————————————————————————————————————————————————

@Autowired
AccountService accountService;
@ResponseBody //用于返回json数据,要是没有这个页面就会解析错误
@GetMapping(“/account”)
public Account get_id(@RequestParam(“id”) Integer id){
return accountService.get_by_id(id);

}
————————————————————————————————————————————————————————————

student表
在这里插入图片描述

结果如下
在这里插入图片描述

注意

mybatis:
  #config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件路径
  mapper-locations: classpath:mybatis/mapper/*.xml   #sql映射文件路径
  configuration:  # 和上面的全局配置文件只能存在一个,所以可以删除mybatis-config.xml文件
    map-underscore-to-camel-case: true

在这里插入图片描述
依旧访问正常
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q渡劫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值