MyBatis

81 阅读1分钟

一,创建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>
    
</mapper>

image.png

二.在application.yml配置文件中配置mapper文件夹

注意需要进行顶格添加

mybatis-plus:
  mapper-locations: classpath:mappers/*.xml

image.png

三.dao下mapper中创建一个方法(此处以登录为例)

image.png

四.xml文件中通过namespace指定DAO接口的路径

image.png

五.增加Login的sql语句

<select id="login" resultType="com.example.homework.entity.User">
    select from t_user where user_name=#{userName} and user_pwd=#{userPwd}
</select>

通过#{}插入参数

image.png

六.测试类中增加代码并运行测试

浏览器输入localhost:8080/login?userName=andy&userPwd=123

image.png

image.png

image.png

七.mapper.xml代码(增删改查)

注意:select需要通过resultType给返回值,其余不需要

<select id="login" resultType="com.example.hello.entity.User">
    select * from t_user where user_name=#{userName} and user_pwd=#{passwd}
</select>

<insert id="addUser">
    insert into t_user(id,user_name,user_pwd) values (#{id},#{userName},#{userPwd});
</insert>

<delete id="re">
    delete  from t_user where id=#{id}
</delete>

<update id="change">
    update t_user set user_name=#{userName},user_pwd=#{userPwd} where id=#{id}