mybatis

导入依赖环境

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>

配置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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<--每一个mapper.xml都要配置一个mapper-->
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>

创建mapper的xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<-- namespace 是之那个对象 class
id 是获取这个mapper的名字,唯一名
resultType 是返回类型
parementType 是参数类型-->
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>

spring

  • 基础配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
  • 导入其他xml
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
</beans>
  • p标签的依赖注入
  • set注入
  • p的意思就是property
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="misterkaoli"/>
</beans>
  • c标签的依赖注入
  • 这个是构造器注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean> <!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/> </beans>

使用注解实现自动装配

  • jdk 1.5支持注解 spring 2.5支持注解

  • 配置注解支持 - 需要导入context约束 就是把beans复制粘贴,把beans改成context就行,改成aop就是aop约束

  • 开启注解支持<context:annotation-config/>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解支持-->
<context:annotation-config/> </beans>
  • @Autowired

  • 直接在属性上用即可,也可以用在set方法上

  • 如果定义了@Autowired(requires = flase)可以属性为null

  • autowried 默认先byType的方式实现,再按照byName方式寻找

  • @Autowired和@Qualifier(value = "beanId") 可以自己定义把这个bean装配到这个属性上

@Autowired
@Qualifier(value="beanId")
private Cat cat;
  • java原生注解 @Resource 可以达到一样的效果 @Resource(value= "beanId")
  • 先进行byName方式寻找,在根据byType寻找,不行就报错了

使用注解开发

  • 属性怎么注入
  • 衍生注解
  • 自动装配
  • 作用域
  • 小结

注解开发必须导入aop包

必须导入context约束,增加注解支持

一般导入spring-webmvc就导入了

  • 开启注解支持

  • 扫描这个包下面注解<context:component-scan base-package="com.hjk.pojo"/>

  • 注解

//@Component 组件,放在class类上面就代表这个class被spring托管了
//等价于 <bean id = "user" class="com.hjk.pojo.User">
//这个注解的id就是这个class的小写
@Component
@Scope("prototypr")//模式,原型模式
public class User{
@Value("hjk")//属性注入值 等价于<perpoty name = "name" value = "hjk"/>
private String name;
}
  • 也可以写在set方法上
@Component
public class User{
private String name;
@Value("hjk")//属性注入值 等价于<perpoty name = "name" value = "hjk"/>
public void setName(String name){
this.name = name;
}
}
  • @Component在web开发中根据mvc不同层级有不同的注解 ,都需要扫描包
  • @Repository
  • @Service
  • @Controller

这四个注解是一样的,只是在不同的层级有不同的名字罢了。

  • xml更加万能,维护比较方便
  • 注解 不是自己的类用不了,维护比较复杂
  • 最佳实践,xml用来管理bean,注解完成属性的注入
  • bean就是一个个bean,注解添加属性
  • 我们在使用的过程中,必须让注解生效,开启注解支持
  • 扫描包,开启注解支持

mybatis和spring的xml基本配置的更多相关文章

  1. spring web.xml 难点配置总结

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  2. spring web.xml 难点配置总结【转】

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  3. mybatis和spring整合的关键配置

    spring配置文件 applicationContext.xml: <beans xmlns="http://www.springframework.org/schema/beans ...

  4. SSM框架中spring的XML文件配置

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  5. spring web.xml基本配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  6. Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有很大不同,多了很多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"?> ...

  7. Spring(十二)使用Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...

  8. spring——通过xml文件配置IOC容器

    创建相关的类(这里是直接在之前类的基础上进行修改) package com.guan.dao; public interface Fruit { String getFruit(); } packag ...

  9. Spring中xml文件配置也可以配置容器list、set、map

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

随机推荐

  1. Typecho 如何安装主题和插件

    Typecho的主题和插件都安装在Typecho的usr目录下,这个不是主机根目录的usr,是Typecho本身文件夹根目录下的usr. 里面有两个文件夹,plugins存放插件,themes存放主题 ...

  2. Choregraphe 2.8.6.23动作失效

    动作和动画执行完以后,无法自动还原成默认状态,自然接下来动作无法执行了.之后各种操作可能诱发软件原有的bug.需要开关自主生活模块才能恢复. 部分连贯的动作不需要恢复就能执行,动画不行. 站立动作好像 ...

  3. CSS基本语法(三)

    目录 CSS基础语法(三) 十五.CSS定位 1.为什么要使用定位 2.定位组成 定位模式 静态定位 相对定位 绝对定位** 固定定位 粘性定位 边偏移 子绝父相 3.定位的叠放次序 4.拓展 绝对定 ...

  4. vscode开发PHP攻略

    前言 此文主要介绍如何使用vscode开发PHP,开发体验可以说和php死桃木不相上下(虽然我没用过php死桃木) PHP扩展组合 一.卡巴斯基组合 PHP IntelliSense PHP Debu ...

  5. debian下编译安装redis并加入到systemd启动管理

    原文地址: http://blog.duhbb.com/2022/02/09/compile-and-install-redis-debian-and-add-to-systemd/ 欢迎访问我的个人 ...

  6. 密码学之PRP/PRF转换引理

    本文将介绍密码学中的PRF.PRP等相关概念,并介绍 PRP/PRF 转换引理及其证明,希望读完本文后,你能对现代密码学中这几个基础概念有所了解. 在开始本文前,希望你有如下预备知识: 现代密码学是怎 ...

  7. VMware网络连接模式(桥接、NAT以及仅主机模式的详细介绍和区别)

    VMware 桥接模式 VMware桥接模式,也就是将虚拟机的虚拟网络适配器与主机的物理网络适配器进行交接,虚拟机中的虚拟网络适配器可通过主机中的物理网络适配器直接访问到外部网络(例如图中所示的局域网 ...

  8. java - HashMap原理及实现 (转)

    众所周知,HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做Entry.这些个键值对(Entry)分散存储在一个数组当中,这个数组就是HashMap的主干. HashMap ...

  9. 关于IBAction、IBOutlet前缀IB的解释

    - 全称:Interface Builder - 以前的UI界面开发模式:Xcode3 + Interface Builder - 从Xcode4开始,Interface Builder已经整合到Xc ...

  10. Category基本概念

    1.什么是Category Category有很多种翻译: 分类 \ 类别 \ 类目 (一般叫分类) Category是OC特有的语法, 其他语言没有的语法 Category的作用 可以在不修改原来类 ...