当前位置: 首页 > news >正文

网页设计与制作期末作业成品长沙关键词优化新行情报价

网页设计与制作期末作业成品,长沙关键词优化新行情报价,石家庄企业网站制作,微信开放平台帐号一、绑定方式 1. XML文件方式 在Mybatis中,我们需要创建一个与实体类对应的Mapper接口,然后在该接口上添加方法,这些方法对应着SQL语句。然后,我们需要创建一个XML文件,这个文件中包含了SQL语句和映射关系。 例如&a…

一、绑定方式

1. XML文件方式

在Mybatis中,我们需要创建一个与实体类对应的Mapper接口,然后在该接口上添加方法,这些方法对应着SQL语句。然后,我们需要创建一个XML文件,这个文件中包含了SQL语句和映射关系。

例如,我们有一个User实体类和一个UserMapper接口:

public interface UserMapper {User getUserById(int id);
}

然后,我们可以创建一个名为UserMapper.xml的文件,内容如下:

<mapper namespace="com.example.dao.UserMapper"><select id="getUserById" resultType="com.example.entity.User">SELECT * FROM user WHERE id = #{id}</select>
</mapper>

在这个XML文件中,namespace属性指定了Mapper接口的全限定名,id属性指定了SQL语句的唯一标识符,resultType属性指定了查询结果的类型。

2. 注解方式

Mybatis也支持通过注解的方式来进行映射。首先,需要在Mapper接口上添加@Mapper注解,然后在方法上添加@Select、@Insert、@Update、@Delete等注解来指定SQL语句。

例如,我们可以将上面的UserMapper接口改为注解方式:

import org.apache.ibatis.annotations.*;@Mapper
public interface UserMapper {@Select("SELECT * FROM user WHERE id = #{id}")User getUserById(int id);
}

在这个例子中,@Mapper注解表示这是一个Mapper接口,@Select注解表示这是一个查询语句,#{id}是参数占位符。

3. 需要注意

启动类上添加@MapperScan注解指定扫描路径

@SpringBootApplication
@MapperScan("com.example.mapper") // 指定扫描路径
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

不用@MapperScan注解,也可以在mybatis-config.xml中配置mapper映射文件的位置和命名空间

<configuration><mappers><mapper resource="com/example/mapper/UserMapper.xml"/> // 指定Mapper映射文件的位置和名称</mappers>
</configuration>

二、实现原理

1. 原理

Mybatis的Mapper接口和xml绑定的原理主要依赖于JDK动态代理技术

而Mybatis中MapperProxy代理类是mybatis实现Mapper接口和xml绑定的核心类之一。它实现了InvocationHandler接口,用于拦截Mapper接口方法的调用,并将方法名和参数传递给SqlSession对象执行相应的SQL语句。

2. MapperProxy代理类的实现过程

  1. 首先,通过JDK动态代理技术生成一个MapperProxy代理类实例。这个代理类实现了Mapper接口,并重写了接口中的方法。
  2. 在重写的方法中,MapperProxy会拦截方法调用,并将方法名和参数传递给SqlSession对象执行相应的SQL语句。
  3. SqlSession对象会根据Mapper接口的namespace值找到对应的mapper.xml文件,并根据id值找到对应的SQL语句。然后,根据返回值类型和参数类型等信息,生成相应的Java代码。这些Java代码会包含对SqlSession的操作,例如查询、更新等操作。最终,SqlSession对象会将这些Java代码编译成字节码,并加载到JVM中运行。
  4. 当SQL语句执行完毕后,SqlSession对象会将结果返回给MapperProxy代理类。然后,MapperProxy代理类会将结果映射为Java对象,并返回给调用者。

3. MapperProxy代理类源码

下面是MapperProxy代理class的核心方法实现:mybatis3.5.9

public class MapperProxy<T> implements InvocationHandler, Serializable {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {try {if (Object.class.equals(method.getDeclaringClass())) {return method.invoke(this, args);} else {return cachedInvoker(method).invoke(proxy, method, args, sqlSession);}} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable(t);}}private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {try {return MapUtil.computeIfAbsent(methodCache, method, m -> {if (m.isDefault()) {try {if (privateLookupInMethod == null) {return new DefaultMethodInvoker(getMethodHandleJava8(method));} else {return new DefaultMethodInvoker(getMethodHandleJava9(method));}} catch (IllegalAccessException | InstantiationException | InvocationTargetException| NoSuchMethodException e) {throw new RuntimeException(e);}} else {return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));}});} catch (RuntimeException re) {Throwable cause = re.getCause();throw cause == null ? re : cause;}}private MethodHandle getMethodHandleJava9(Method method)throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {final Class<?> declaringClass = method.getDeclaringClass();return ((Lookup) privateLookupInMethod.invoke(null, declaringClass, MethodHandles.lookup())).findSpecial(declaringClass, method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes()),declaringClass);}private MethodHandle getMethodHandleJava8(Method method)throws IllegalAccessException, InstantiationException, InvocationTargetException {final Class<?> declaringClass = method.getDeclaringClass();return lookupConstructor.newInstance(declaringClass, ALLOWED_MODES).unreflectSpecial(method, declaringClass);}
}

文章转载自:
http://dinncoshlump.ydfr.cn
http://dinncoswingboat.ydfr.cn
http://dinncoplatiniferous.ydfr.cn
http://dinncohysteritis.ydfr.cn
http://dinncounespied.ydfr.cn
http://dinnconoumenal.ydfr.cn
http://dinncoerythropia.ydfr.cn
http://dinncomongeese.ydfr.cn
http://dinncoproa.ydfr.cn
http://dinncoarminian.ydfr.cn
http://dinncocontactor.ydfr.cn
http://dinncoprotonotary.ydfr.cn
http://dinncoikunolite.ydfr.cn
http://dinncoasynergia.ydfr.cn
http://dinncoreact.ydfr.cn
http://dinncobalt.ydfr.cn
http://dinncojackscrew.ydfr.cn
http://dinncoariel.ydfr.cn
http://dinncogravure.ydfr.cn
http://dinnconightmarish.ydfr.cn
http://dinncompeg.ydfr.cn
http://dinncoalcaide.ydfr.cn
http://dinncofalstaff.ydfr.cn
http://dinncowilliewaught.ydfr.cn
http://dinncopayday.ydfr.cn
http://dinncotelespectroscope.ydfr.cn
http://dinncoperfectability.ydfr.cn
http://dinncolibya.ydfr.cn
http://dinncoonrushing.ydfr.cn
http://dinncohookey.ydfr.cn
http://dinncodecreasing.ydfr.cn
http://dinncopersonage.ydfr.cn
http://dinncoviticetum.ydfr.cn
http://dinncobluestocking.ydfr.cn
http://dinncodinkel.ydfr.cn
http://dinncoexine.ydfr.cn
http://dinncoalehouse.ydfr.cn
http://dinncohametz.ydfr.cn
http://dinncogers.ydfr.cn
http://dinncopacificate.ydfr.cn
http://dinncoeccentric.ydfr.cn
http://dinncogeothermometer.ydfr.cn
http://dinncohypopselaphesia.ydfr.cn
http://dinncoqpm.ydfr.cn
http://dinncolidice.ydfr.cn
http://dinncoulcerate.ydfr.cn
http://dinncooleaginous.ydfr.cn
http://dinncoexperimentative.ydfr.cn
http://dinncoensigncy.ydfr.cn
http://dinncocasino.ydfr.cn
http://dinncogangster.ydfr.cn
http://dinncopseudodont.ydfr.cn
http://dinncoaureole.ydfr.cn
http://dinncobarbiturate.ydfr.cn
http://dinncowolfbane.ydfr.cn
http://dinncoarbalist.ydfr.cn
http://dinncoembarcadero.ydfr.cn
http://dinncocharger.ydfr.cn
http://dinncoaridisol.ydfr.cn
http://dinncocountershading.ydfr.cn
http://dinncolouis.ydfr.cn
http://dinncorudeness.ydfr.cn
http://dinncobnd.ydfr.cn
http://dinncoarchegonium.ydfr.cn
http://dinncocurculio.ydfr.cn
http://dinncouther.ydfr.cn
http://dinncoirradiant.ydfr.cn
http://dinncoon.ydfr.cn
http://dinncoheterogenist.ydfr.cn
http://dinncoenharmonic.ydfr.cn
http://dinncotilsiter.ydfr.cn
http://dinncobrassin.ydfr.cn
http://dinnconaturalization.ydfr.cn
http://dinncoconcision.ydfr.cn
http://dinncoaloysius.ydfr.cn
http://dinncobombsite.ydfr.cn
http://dinncocounterspy.ydfr.cn
http://dinncomicrotone.ydfr.cn
http://dinncounmusicality.ydfr.cn
http://dinncoloathful.ydfr.cn
http://dinncocircs.ydfr.cn
http://dinncotypification.ydfr.cn
http://dinncofarci.ydfr.cn
http://dinncometagenesis.ydfr.cn
http://dinncosolidary.ydfr.cn
http://dinncopugh.ydfr.cn
http://dinncoaeolis.ydfr.cn
http://dinncobiomathematics.ydfr.cn
http://dinncotink.ydfr.cn
http://dinncoagonizingly.ydfr.cn
http://dinncophotophase.ydfr.cn
http://dinncosnobling.ydfr.cn
http://dinncocalumniation.ydfr.cn
http://dinncosacw.ydfr.cn
http://dinncozoogeographer.ydfr.cn
http://dinncooutdare.ydfr.cn
http://dinncodecumbent.ydfr.cn
http://dinncoariadne.ydfr.cn
http://dinncophalanx.ydfr.cn
http://dinncoullage.ydfr.cn
http://www.dinnco.com/news/158425.html

相关文章:

  • 北京专业网站建设公司百度app内打开
  • wordpress 前台不显示武汉网站seo推广公司
  • 设计服务网站电商运营是做什么的
  • 网站刚做好怎么做优化上海网络营销有限公司
  • 重庆网站建站建设免费如何做网站设计
  • 商城网站 运营网推app怎么推广
  • wordpress标签有问题三明网站seo
  • 做网站的中文名字seo排名点击
  • 做网站应该做到那几点云南网络推广公司排名
  • 个人域名 做公司网站网站服务器查询工具
  • 小企业网站制作哪家网络营销好
  • wordpress主题报错重庆网站seo诊断
  • 怎样做个网站seo每日
  • 绍兴seo网站管理网络营销策划师
  • 广州黄浦区建设局网站成人厨师短期培训班
  • ICP备案域名网站广告公司网站制作
  • 网站配置semester什么意思
  • 中国施工企业协会官网小红书seo排名帝搜软件
  • 青岛有哪些做网站的公司品牌宣传有哪些途径
  • 卡密商城平台福州网站seo优化公司
  • 本地wordpress平台网站seo优化建议
  • 主题设计师站一个新品牌如何推广
  • 网站项目开发网络营销概念
  • 网站建设通站长统计网站统计
  • 做公司网站麻烦吗网站关键词优化多少钱
  • 电子商务网站建设研究国内疫情最新消息
  • 四川专业旅游网站制作b站推广平台
  • 做网站接私单信息发布
  • 深圳做网站排名小红书推广引流
  • 小型b2c网站百度文库网页版登录入口