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,注解添加属性
  • 我们在使用的过程中,必须让注解生效,开启注解支持
  • 扫描包,开启注解支持

最新文章

  1. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希
  2. 理解Attribute
  3. ubuntu包管理
  4. ngRoute AngularJs自带的路由
  5. ASP.NET MVC 自己实现登陆验证过滤器
  6. 深入理解JVM虚拟机-7虚拟机类加载机制
  7. LeetCode() Valid Anagram 有问题!!!
  8. easyui的datagrid和panel如何让标题动态改变?
  9. addEventListener
  10. HDU-1395-2^x mod n = 1(数学题(二次出错))
  11. OAuth 2.0: Bearer Token Usage
  12. C#中 Equals和= =的区别
  13. Java总结篇:Java多线程
  14. JS 函数作用域及变量提升那些事!
  15. spark2.1:在RDD[unit].foreach(s=&gt;{})内部调用sparkSession对象抛出NullPointException
  16. windows10创建ftp服务器
  17. JSP验证码。
  18. python早期看书笔记
  19. web 基础
  20. HDU 2157(矩阵快速幂)题解

热门文章

  1. 模拟axios的创建[ 实现调用axios()自身发送请求或调用属性的方法发送请求axios.request() ]
  2. NTT 快速数论变换
  3. MySQL函数学习(一)-----字符串函数
  4. yum源 epel源 no package available 更换国内yum源
  5. undo和redo的区别
  6. Docker常用命令速查
  7. windows下使用LTP分词,安装pyltp
  8. Stream流的基本介绍以及在工作中的常用操作(去重、排序以及数学运算等)
  9. 6. java IO 流
  10. Android 存储到SD卡,获取SD的大小及可用空间