把xml方式的CRUD修改为注解方式
之前在xml中配置,是在<mapper></mapper>标签下写CRUD
<mapper namespace="com.demo.pojo"> <select id="listProduct" parameterType="Product"> select * from product_table </select> <insert id="addProduct" parameterType="Product"> insert into product_table (name) values (#{name}) </insert> <update id="updateProduct" parameterType="Product"> update product_table set name=#{name} where id=#{id} </update> <delete id="deleteProduct" parameterType="Product"> delete from product_table where id=#{id} </delete> <mapper>
1、因此要增加Mapper接口方式实现
比如要把之前映射文件Product.xml配置方式变成注解方式的,新建一个ProductMapper接口,并在接口中声明的方法上,加上注解就可以(也就是把SQL语句从xml上移到了注解上来)
package com.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.demo.pojo.Product; public interface ProductMapper{ @Insert(" inert into product_table (name) values (#{name}) ") public int add(Product product); @Delete(" delete from product_table where id=#{id} ") public void delete(int id); @Select(" select * from product_table where id=#{id} ") public Product select(int id); @Update(" update product_table set name=#{name} where id=#{id} ") public int update(Product product); @Select(" select * from product_table ") public List<Product> selectAll(); }