一、基于注解方式的CRUD
把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(); }
2、在mybatis-config.xml配置文件中,配置对ProductMapper接口的映射,至于原来的Product.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> <typeAliases> <package name="com.demo.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="admin"/> </dataSource> </environment> </environments> <mappers> <!--基于xml配置的映射文件--> <mapper resource="com/demo/pojo/Product.xml"/> <!--基于注解方式配置的映射--> <mapper class="com.demo.mapper.ProductMapper"/> </mappers> </configuration>
3、TestMyBatis测试如下:
进行简单的CRUD测试。
package com.demo; import java.io.IOException; import java.io.InputSteam; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resouces; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.demo.pojo.Product; import com.demo.mapper.ProductMapper; public class TestMyBatis{ public static void main(String[] args) throws IOException{ String resouce="mybatis-config.xml"; InputSteam inputStream=Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); ProductMapper mapper=session.getMapper(ProductMapper.class);//加载基于注解方式配置的映射文件。 select(mapper); insert(mapper); update(mapper); delete(mapper); selectAll(mapper); session.commit(); session.close(); } private static void select(ProductMapper mapper){ Product p=mapper.get(1);//获取id=1的产品 System.out.println(p); } private static void insert(ProductMapper mapper){ Product p=new Product(); p.setName("新增一个product"); mapper.add(p); selectAll(mapper); } private static void update(ProductMapper mapper){ Product p=new Product(); p.setId(1); mapper.update(p); selectAll(mapper); } private static void delete(ProductMapper mapper){ mapper.delete(1); selectAll(mapper); } private static void selectAll(ProductMapper mapper){ List<Product> list=mapper.list(); for(Product p:list){ System.out.println(p); } } }