您现在的位置是:首页 > 个人日记个人日记
mybatis-03-Mapper代理
2019-02-02 15:49:52【个人日记】296人已围观
简介这里将介绍第三部分,使用命名空间加方法名直接代理,不再使用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文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--接口的全限定名--><mapper namespace="com.huangxin.mapper.ProductCategoryMapper"><!--查询所有--><select id="getAll" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product</select><!--查询一个--><select id="getById" parameterType="java.lang.Long" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product WHERE id = #{id}</select><!--${只能放value}--><!--使用${}可能会有sql注入--><!--使用"%"#{*}"#"也可以--><select id="findByCategoryName" parameterType="java.lang.String" resultType="com.huangxin.model.ProductCategory">SELECT * FROM product WHERE supplier LIKE '%${value}%'</select><!--插入新数据--><insert id="insert" parameterType="com.huangxin.model.ProductCategory">INSERT INTO product(productName,salePrice,supplier,brand,costPrice)VALUES(#{productName},#{salePrice},#{supplier},#{brand},#{costPrice})</insert><!--修改数据--><update id="update" parameterType="com.huangxin.model.ProductCategory">UPDATE product SET supplier=#{supplier} WHERE id=#{id}</update><!--删除指定数据--><delete id="deleteById" parameterType="java.lang.Long">DELETE FROM product WHERE id = #{id}</delete></mapper>
配置Mapper.java接口文件
//包名,全限定名package com.huangxin.mapper;import com.huangxin.model.ProductCategor;import java.util.List;public interface ProductCategoryMapper {/*** 根据id删除信息** @param id*/void deleteById(Long id);/*** 通过ProductCategory对象插入信息** @param productCategory*/void insert(ProductCategory productCategory);/*** 更改商品信息** @param productCategory*/void update(ProductCategory productCategory);/*** 查询所有商品信息** @return*/List<ProductCategory> getAll();/*** 根据id查询信息** @param id*/ProductCategory getById(Long id);/*** 模糊查询** @param categoryName* @return*/List<ProductCategory> findByCategoryName(String categoryName);}
Junit测试类
package com.huangxin.mapper;import com.huangxin.model.ProductCategory;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import static org.junit.Assert.*;public class ProductCategoryMapperTest {private SqlSessionFactory factory;/*** 初始化SqlSession工厂** @throws IOException*/@Beforepublic void init() throws IOException {//将全局配置文件读取InputStream in = Resources.getResourceAsStream("mybatis-config.xml");factory = new SqlSessionFactoryBuilder().build(in);}@Testpublic void deleteById() {SqlSession sqlSession = factory.openSession();//通过mybatis的动态代理机制,生成了一个ProductCategory接口实现类ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);System.out.println(mapper.getAll());}@Testpublic void getById() {SqlSession sqlSession = factory.openSession();//通过mybatis的动态代理机制,生成了一个ProductCategory接口实现类ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);System.out.println(mapper.getById(5l));sqlSession.close();}}
切记Mapper规范
Tags: JavaWeb
上一篇: spring-05-复杂数据类型注入
下一篇: mybatis-08-延迟加载
相关文章
随机图文
评论区
2025-11-09 04:26:22
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
