您现在的位置是:首页 > 个人日记个人日记

mybatis-03-Mapper代理

2019-02-02 15:49:52【个人日记】210人已围观

简介这里将介绍第三部分,使用命名空间加方法名直接代理,不再使用DAO实现

Mapper代理开发


注意

1) mapper代理相当于DAO,但不需要写接口实现类

> 2) 在运行时mybatis会通过JDK动态代理机制生成一个接口的代理实现类

开发规范

1) mapper接口的全限定名要和mapper的namespace值一致
2) mapper接口的方法名称要和mapper的statement的id名一致
3) mapper接口的方法参数类型要和mapper映射文件中的statement的parameterType的值一致,参数唯一

> 4) mapper接口的方法返回值要和mapper映射文件的statement的resultType的值一致

其他xml没有改变

仅仅改变Mapper映射文件

配置mapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--接口的全限定名-->
  6. <mapper namespace="com.huangxin.mapper.ProductCategoryMapper">
  7. <!--查询所有-->
  8. <select id="getAll" resultType="com.huangxin.model.ProductCategory">
  9. SELECT * FROM product
  10. </select>
  11. <!--查询一个-->
  12. <select id="getById" parameterType="java.lang.Long" resultType="com.huangxin.model.ProductCategory">
  13. SELECT * FROM product WHERE id = #{id}
  14. </select>
  15. <!--${只能放value}-->
  16. <!--使用${}可能会有sql注入-->
  17. <!--使用"%"#{*}"#"也可以-->
  18. <select id="findByCategoryName" parameterType="java.lang.String" resultType="com.huangxin.model.ProductCategory">
  19. SELECT * FROM product WHERE supplier LIKE '%${value}%'
  20. </select>
  21. <!--插入新数据-->
  22. <insert id="insert" parameterType="com.huangxin.model.ProductCategory">
  23. INSERT INTO product(productName,salePrice,supplier,brand,costPrice)
  24. VALUES(#{productName},#{salePrice},#{supplier},#{brand},#{costPrice})
  25. </insert>
  26. <!--修改数据-->
  27. <update id="update" parameterType="com.huangxin.model.ProductCategory">
  28. UPDATE product SET supplier=#{supplier} WHERE id=#{id}
  29. </update>
  30. <!--删除指定数据-->
  31. <delete id="deleteById" parameterType="java.lang.Long">
  32. DELETE FROM product WHERE id = #{id}
  33. </delete>
  34. </mapper>

配置Mapper.java接口文件

  1. //包名,全限定名
  2. package com.huangxin.mapper;
  3. import com.huangxin.model.ProductCategor;
  4. import java.util.List;
  5. public interface ProductCategoryMapper {
  6. /**
  7. * 根据id删除信息
  8. *
  9. * @param id
  10. */
  11. void deleteById(Long id);
  12. /**
  13. * 通过ProductCategory对象插入信息
  14. *
  15. * @param productCategory
  16. */
  17. void insert(ProductCategory productCategory);
  18. /**
  19. * 更改商品信息
  20. *
  21. * @param productCategory
  22. */
  23. void update(ProductCategory productCategory);
  24. /**
  25. * 查询所有商品信息
  26. *
  27. * @return
  28. */
  29. List<ProductCategory> getAll();
  30. /**
  31. * 根据id查询信息
  32. *
  33. * @param id
  34. */
  35. ProductCategory getById(Long id);
  36. /**
  37. * 模糊查询
  38. *
  39. * @param categoryName
  40. * @return
  41. */
  42. List<ProductCategory> findByCategoryName(String categoryName);
  43. }

Junit测试类

  1. package com.huangxin.mapper;
  2. import com.huangxin.model.ProductCategory;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import static org.junit.Assert.*;
  12. public class ProductCategoryMapperTest {
  13. private SqlSessionFactory factory;
  14. /**
  15. * 初始化SqlSession工厂
  16. *
  17. * @throws IOException
  18. */
  19. @Before
  20. public void init() throws IOException {
  21. //将全局配置文件读取
  22. InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
  23. factory = new SqlSessionFactoryBuilder().build(in);
  24. }
  25. @Test
  26. public void deleteById() {
  27. SqlSession sqlSession = factory.openSession();
  28. //通过mybatis的动态代理机制,生成了一个ProductCategory接口实现类
  29. ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);
  30. System.out.println(mapper.getAll());
  31. }
  32. @Test
  33. public void getById() {
  34. SqlSession sqlSession = factory.openSession();
  35. //通过mybatis的动态代理机制,生成了一个ProductCategory接口实现类
  36. ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);
  37. System.out.println(mapper.getById(5l));
  38. sqlSession.close();
  39. }
  40. }

切记Mapper规范

Tags: JavaWeb  

评论区

    2024-04-23 23:56:44

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我