通过xml配置文件,配置Mapper需要的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">
<mapper namespace="cn.tedu.boot10.mapper.ProductMapper">
    <!--id需要和ProductMapper中的方法名一致-->
    <insert id="insert">
        INSERT INTO product VALUES (
                                    NULL ,#{title},#{price},#{num}
                                   )
    </insert>
    <select id="select" resultType="cn.tedu.boot10.entity.Product">
        SELECT id,title,price,num FROM product
    </select>

    <delete id="deleteById">
        DELETE FROM product WHERE id=#{id}
    </delete>

    <update id="update">
        UPDATE product SET
                title=#{title},price=#{price},num=#{num}
                WHERE id=#{id}
    </update>

    <select id="selectById" resultType="cn.tedu.boot10.entity.Product">
        SELECT id,title,price,num FROM product WHERE id=#{id}
    </select>



</mapper>

product.ProductMapper

//因为使用了MapperScan注解 所以这里不再写@Mapper注解
public interface ProductMapper {
    //此处不再使用@Insert注解配置SQL语句, 而是写在xml配置文件中
    void insert(Product product);

    List<Product> select();

    void deleteById(int id);

    void update(Product product);
    //查询单个商品信息
    Product selectById(int id);

config. MapperConfig

@Configuration
@MapperScan("cn.tedu.boot10.mapper")
public class MybatisConfig {

}

 


版权声明:本文为weixin_62186043原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。