spring5.0学习总结

spring总结

1 、spring

1.1 spring 简介

简化了项目的开发、但配置依然很繁琐!!!
扩展:

SSH:Struct2 + Spring + Hibernate

SSM: SpringMvc +Spring + Mybatis

spring官网:https://spring.io/

spring下载: http://repo.spring.io/libs-release-local/org/springframework/spring/

maven配置:https://mvnrepository.com/
在这里插入图片描述

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.20</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.20</version>
</dependency>
1.2 优点

一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

1.3 spring的组成(重要)

img

1.4 扩展

spring->springboot->springcloud->spring网格

框架的一般原则:约定>配置>编码

学习springcloud要先学springboot、spring、java基础依次类推~~

2 、IOC(重要)

原始:程序主动创建对象、控制权在程序员上

spring:使用set注入(lombok简化开发springboot)程序不在具有主动性、而是变成了被动接受的对象!

2.1 IOC的本质

IOC是spring的核心内容!可以使用配置文件(xml)和注解的方式实现IOC、其实现方式是依赖注入(DI)

public class hello(){
    private String str;
    getter/setter()
        toString()
}
xml:官网复制
<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">
    
    <!--使用spring来创建对象、在spring这里都称为bean
		类型 变量名 = new 类型
		Hello hello = new Hello();
		id =变量名 
		class=new 的对象
		property 相当于给对象中的属性设置一个值。
		bean = 对象 new hello();
		name:
		value:具体的值、基本数据类型
		ref:应用spring容器中创建好的对象
	-->
    <bean id="hello" calss="com.pojo.hello">
        <property name="str" value="Spring"></property>
    </bean>
    
</beans>

public class testMain(){
	public static void main(String[] args){
		//获取spring的上下文对象 固定写法
		ApplicationContext context = new ClassPathApplicationContext(beans.xml)
		//对象都在spring中管理了、我们要使用直接从对象里面取出来 就可以了。
		Hello hello = (hello) context.getBean("hello")
		System.out.println(hello)
	}
}
public class testlh(){
	public static  void main(String[] args){
        //获取ApplictionContext:拿到spring的容器
        ApplicationContext context =  new ClassPathXmlApplicationContext("beans.xml");
        //容器在手、天下我有、需要什么get什么
        UserServiceImpl userserviceimpl =  context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
    }
}

IOC:对象由spring来创建、管理、装配

3、spring的创建方式

public class User(){
	private String name;
    getter/setter
        toString
}
public class MyTest(){
	public static void main(String[] args){
		Application context = new ClassPathXmlApplicationContext("beans.xml")
        User user = (User) context.getBean("user");
        user.show();
	}
}
<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">
	
	<bean id="user" class="com.lh.pojo.User">
    	<property name="name" value="lihui">
        
        </property>
    </bean>
	
</bean>

IOC创建对象的方式:(重要)

1、使用无参构造创建对象、默认

2、使用有参构造创建对象:三种方式

<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">
	<!--
	 	第一种方式:下标赋值
	-->
	<bean id="user" class="com.lh.pojo.User">
    	<constrctor-arg index="0" value="lihui"></constrctor-arg>
    </bean>
    <!--
		第二种方式:不建议使用、通过类型创建的
	-->
	<bean id="user" class="com.lh.pojo.User">
    	<constructor-arg type="java.lang.String" value=“lihui>				</constructor-arg>
    </bean>
    <!--
		第三种方式:直接通过参数名称设置(常用)
	-->
    	<bean id="user" class="com.lh.pojo.User">
    	<constructor-arg name="name" value="lihui">							</constructor-arg>
    </bean>
</bean>

总结:在配置文件加载的时候、容器中管理的对象就已经开始初始化了!!!

4、Spring配置

4.1 别名、配置、import
<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">
    
    <!--别名:名字和数据库中的名字一样,如果添加了别名、我们也可以通过别名来获取这对象-->
    <alias name="user" alias="userNew"></alias>
    
    <!--
		配置:
		id:bean的唯一标识符,也就是相当于我们学的对象名
		class:bean 对象所对应的全限定名:包名+类型
		name:也是别名,而且name更高级、可以同时取多个别名、
			可以通过空格、逗号、分号进行分割!!!
	-->
    <bean id="userId" class="com.lh.pojo.User" name="userId2"></bean>
    
    <import resource="applicationContext.xml"></import>
	<import resource="beans.xml"></import>
    <import resource="beans1.xml"></import>
    <import resource="beans2.xml"></import>
</beans>
public class MyTest(){
	public static void main(String[] args){
		Application context = new ClassPathXmlApplicationContext("beans.xml")
        User user = (User) context.getBean("user,u2,u3,u4");
        user.show();
	}
}

5、依赖注入(DI)

5.1 构造器注入
5.2 Set注入(重要)

依赖注入:set注入

​ 依赖:bean对象的创建依赖于容器

​ 注入:bean对象中的所有属性、由容器来注入

【环境搭建】

public class Address(){
    private String address;
   	...
}

public class Student(){
    //各种不同属性的注入方式
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,Sring> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

<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">
    <!--第一种:普通值注入:,value-->   
    <bean id="student" class="com.lh.pojo.Student">
        <property name="name" value="lihui"></property>
    </bean>
     <!--第二种:bean注入 ref-->   
    <bean id="address" class="com.lh.pojo.Address"></bean>
     <!--第三种:数组注入 ref-->  
    <property name="books">
        <arrar>
        	<value>hahh</value>
            <value>youyuio</value>
            <value>kafghj</value>
            <value>oiuh</value>
        </arrar>
    </property>
     <!--list注入-->
    <property name="list">
    	<list>lkgjha</list>
        <list>ioregh</list>
        <list>oaigh</list>
    </property>
     <!--map注入-->
    <property name="map">
    	<map>
        	<entry key="id" value="h1234"></entry>
            <entry key="akfg" value="dlakgh"></entry>
        </map>
    </property>
      <!--set注入-->
    <property name="set">
    	<set>
        	<value>algh</value>
            <value>cos</value>
            <value>32oriu</value>
        </set>
    </property>
      <!--null注入-->
    <property name="wife">
   		<null></null>
    </property>
    <!--properties注入-->
 	<property name="info">
        <props>
        	<prop key="ihi">2345</prop>
            <prop key="afskl">jalfkj</prop>
            <prop key="weil"></prop>
            <prop key="aklegja"></prop>
        </props>
    </property>
<beans>
public class testStudent(){
    public static void main(String[] args){
       ApplicationContext context = new ClassPathXmlContext("bean.xml");
    	Student student = (Student)context.getBean("student")
        System.out.println(student.getName());
    }
}
<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">
    <!--扩展方式注入:
		注意点:p命名和c命名不能直接使用,需要导入xml约束!!!
	-->
    <!--C命名和P命名(官网添加)-->
    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.lh.pojo.User" p:name="lihui" p:age="18">
    </bean>
    <!--c命名空间注入,可以通过构造器注入:construct-args-->
     <bean id="user" class="com.lh.pojo.User" c:name="lihui" c:age="18">
    </bean>
    
</beans>
p命名空间和c命名空间的使用:
......

5.3 bean的作用域

共六种:

1、单列默认(默认)

scope=“singleton”

2、原型模式:每此从容器中get的时候、都会产生一个新对象~

scope=“prototype”

3、其余的request、session、application、这些都只在web开发中使用!!!

6、Bean的自动装配

自动装配是spring满足bean依赖的一种方式。

spring会在上下文中自动寻找、并自动给bean装配属性

在spring中有三种装配的方式

1、在xml中显示的配置

2、在java中显示装配

3、隐式的自动装配bean【重要】

6.1、测试
public class Cat(){
   public void shout(){
       System.out.println("miao~~~");
   }
}
public class Dog(){
    public void shout(){
        System.out.println("wang~~~~");
    }
}
public class People(){
    private Cat cat;
    private Dog dog;
    private String name;
    .......
}
bean.xml:
<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">
    
    <bean id="cat" class="com.lh.pojo.Cat"></bean>
    <bean id="dog" class="com.lh.pojo.Dog"></bean>
    <bean id="people" class="com.lh.pojo.people">
    	<property name="name" value="lihui"></property>
        <property name="dog" value="dog"></property>
        <property name="cat" value="cat"></property>
    </bean>
    
</beans>
public class test(){
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        People people = context.getBean("people",People.class);
        people.getDog().shout();
        people.getCat().shout();
    }
}
6.2、自动装配:autowire(byName、byType)
bean.xml:
<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">
    
    <bean id="cat" class="com.lh.pojo.Cat"></bean>
    <bean id="dog" class="com.lh.pojo.Dog"></bean>
    <!--
		byName:会自动在容器上下文中查找,和自己对象set方法后面对应的值的bean-id
		byType:会自动在容器上下文中查找,和自己属性类型相同的bean
		都有弊端:byName:要保证所有bean的id唯一、并且这个bean需要和自动注入的属性的set方法的值一致。
				byType:需要保证所有的bean的class唯一、并且这个bean需要自动注入的属性的类型一致
	-->
    <bean id="people" class="com.lh.pojo.people" autowire="byName" autowire="byType">
    	<property name="name" value="lihui"></property>
    </bean>
    
</beans>
6.3、使用注解实现自动装配(重要)

jdk1.5以上支持的注解、spring2.5以上支持注解

要使用注解:

1、导入约束:xml中配置context约束

2、配置注解的支持:context:annotation-config/

<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">
    <!----!!>学会自己配置xml配置文件</!----!!>
    <!--配置注解的支持-->
    <context:annotation-config></context:annotation-config>
    
</beans>

@Autowired

直接在属性上使用即可!也可以在set方式上使用。

使用Autowired可以不用编写set方法、前提是自动装配的属性在IOC(Spring)容器中存在,且符合名字byName!

@Nullable:字段标记了这个属性、
@Autowired(required=false):required的值为false,说明对象可以为空、否则对象不能为空!!!
@Qualifier(value="dog"):如果自动装配的环境毕节复杂,一个注解无法完成时可以用@Qualifier指定装配多个对象
-------------------
@Resource:结合了名字(name)和类型(type)

总结:@Resource和@Autowired的区别?

  • 都是用来自动装配的,都可以实现放在属性字段上
  • @Autowired通过byType的方式实现、而且必须要求这个对象存在![常用]
  • @Resource默认通过byName的方式实现、如果找不到名字、则通过byType实现、如果两个都找不到就报错!!!
  • 执行顺序不同:
  • @Autowired 先通过byTpe的方式实现。再通过byName的方式实现
    • 如果Autowired不能唯一自动装配实现,则需要通过@Qulifier(value=“xxx”)来实现
  • @Resource 先通过使用byName再通过byType的方式实现!!!

7、使用注解开发

在spring4之后、要使用注解开发、必须保证aop的包导入了!!!

需要导入注解的支持~

<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 component-scan="com.lh.pojo"/>
    <context:annotation-config></context:annotation-config>
    
</beans>
@Component:组件
@Component
public class User(){
    @Value("lihui")
    private String name;@Value("lihui")
    private void setName(String name){
        this.name=name;
    }
}
1、bean
2、属性如何注入
3、衍生的注解

@Component有几个衍生的注解,我们在web开发中会按照mvc三层架构分层!!

dao:【@Repository】

service:【@Service】

controller:【@Controller】

这四个注解功能一样:都是代表将某个类注入到spring容器中~

4、自动装配
上面已陈述
5、作用域
@Scope("singleton","property")
6、小节
  • xml与注解

    • xml更加万能,适用于任何场合~,维护相对复杂
    • 注解不是自己类使用不了,维护相对复杂
  • xml与注解的最佳实践

    • xml用来管理bean

    • 注解只负责完成属性的注入。

    • 在使用的过程中只需要主要一个问题:要让注解生效,必须开启注解的支持和扫描对应的包

    • <!--指定要扫描的包,这个包下的注解就会生效-->
         <context component-scan="com.lh.pojo"/>
         <context:annotation-config></context:annotation-config>
      

8、使用java的方式配置spring

不使用spring的xml配置,完全使用java来注解来做~~

javaconfig

//这个注解的意思说明这个类被spring接管了,注册到了容器中。
@Component
public class User(){
    @Value("lihui")//属性注入值
    private String name;
    ......
}
@Configuration//这个也会被spring容器托管,注册到spring容器中,因为他也是一个组件,代表这是一个配置类,和beans.xml一样的
@ComponentScan("com.lh.pojo")
@Import("lihui.class")//新特性~~~~
public class lihuiConfig(){
    //注册一个bean就相当于,我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User user(){
        return new User(); //就是返回要注入到bean的对象~
    }
}
public class test(){
    public static void main(String[] args){
        //如果完全使用了配置类方式去做,我们就要通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载~
        Application context = new AnnotationConfigApplicationContext(lihui.class);
       User getUser =(User) context.getBean("getUser")
        System.out.println(getUser.getName());
    }
    
}

这个纯java的配置方式,在springboot中随处可见~~~

9、AOP(重点)

9.1 代理模式

为啥要学习代理模式?

因为这就是springAop的底层【springAOP和springMVC重要】

代理模式的分类:

我---->中介----->买房

9.2、静态代理:

角色分析:

  • 抽象角色:一般会使用接口或者抽象类来解决

  • 真实角色:被代理的角色

  • 代理角色:代理真实角色,代理真实角色后,我们一般会做附属操作

  • 客户:访问代理对象的人

    //租房接口
    public interface Rent(){
        public void rent();
    }
    
    
    //房东
    public class Host implements Rent(){
        public void rent(){
            System.out.println("房东要出租房子");
        }
    }
    
    
    public class Client{
        public static void main(String[] args){
            Host host = new Host();
            //代理,代理一般会有一些附属操作~~~
            Proxy proxy = new Proxy(host);
            proxy.rent();
        }
    }
    
    
    public class Proxy{
        private Host host;
        public Proxy(){}
        public proxy(Host host){
            this.host=host;
        }
        public void rent(){
            host.rent();
        }
        //看房
        public void seeHose(){
            System.out.println("中介带你看房");
        }
        //收中介费
        public void fare(){
            Syetem.out.println("收中介费");
        }
        
    }
    
    

    代理模式的好处:

    • 可以是真实角色的操作根据纯粹,不要去关注一些公共的业务

    • 公共业务就交给代理角色,实现了业务的分工

    • 公共业务发生扩展的时候,方便集中管理

      缺点:

      代码量翻倍,开发效率较低~~~

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zJL1CTEm-1661669913089)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1661652710109.png)]

9.3、动态代理:
  • 动态代理和静态代理一样,
  • 动态代理的代理类是动态生成的,不是我们直接写好的
  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
    • 基于接口----JDK动态代理【建议使用】
    • 基于类:cglib
    • java字节码实现:javasist

需要了解两个类:Proxy:代理、InvocationHandler:调用处理程序

InvocationHandler

//用这个类,自动生成代理类
public class ProxyInvocationHandler implements InvocationHandle{
    //被代理的接口
    private Rent rent;
    public void setRent(Rent rent){
        this.rent=rent;
    }
    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
    }
    //处理代理实例,并返回结果
    public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
        //动态代理的本质,就是使用反射机制实现!!!
        Object result = method.invoke(rent,args);
        return result;
    }
    //添加
    public void seeHose(){
        System.out.println("hafgjkllj");
    }
}

//真实角色
public class true(){
    Host host = new Host();
    //代理角色
    ProxyInvocationHandler pih = new ProxyInvocationHandler();
    pih.setRent(host);
    Rent proxy = (Rent) pih.getProxy();
    proxy.rent();
}

动态代理的好处:

  • 具有静态代理的所有好处
  • 一个动态代理类代理的是一个接口,一般就是对应的一个业务。
  • 一个动态代理类可以代理多个类,只有实现了接口。
9.3、AOP(面向多面编程)
AOP的实现方式一
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
    <scope>runtime</scope>
</dependency>

public interface UserService{
    public void add();
    public void delete();
    public void update();
    public void select();
}

public class UserServiceImpl implements UserService{
    public void add(){
        sout("...")
    }
    public void delete(){
        sout("...")
    }
    public void update(){
        sout("...")
    }
    public void select(){
        sout("...")
    }
}

方式一:使用spring的接口【主要是springAPI接口实现】

public class log implements MethOdBeforeAdvice{
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    public void before(Method method,Object[] args,Object target) throws Throwable{
		sout(target.getClass().getName()+method.getMethod());
    }
}

//注册bean
<bean id="userService" class="com.lh.pojo">
</bean>
//配置aop:需要导入aop的约束
<aop:config>
	//切入点 expression表达式,execution(要执行的位置****)
    <aop:pointcut id="pointcut" expression="execution(要执行的位置!*****)"></aop:>
    //执行环绕
    <aop:advisor advice-ref="afterLog"></aop:>
</aop:config>

//自定义类
<bean id="diy" calss="com.lh.diy.DiyPointCut"></bean>
<aop:config>
	//自定义切面 ref要引用的类
    <aop:aspect ref="diy">
    	//切入点
        <aop:pointcut id="point" expression="execution(* com.lh.service.UserServiceImpl.*(..))">
         //通知
            <aop:before method="before" pointcut-ref="point"></aop:>
         </aop:>
    </aop:>
</aop:config>

//注解实现
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

注:动态代理代理的是接口,不能代理实现类!!!

方式二:使用自定义类实现【主要是实现切面】

public class DiyPointCut{
    public void before(){
        sout("...")
    }
    public void after(){
        sout("...")
    }
}

方式三:注解实现

@Aspect//标注这是一个切面
public class AnnotationPointCut{
    @Before("execution(* com.lh.UserserviceImpl*(..))")
    public void before(){
        sout("....")
    }
}

10、整合Mybatis

步骤:

1、导入相关jar包

-  junit
-  mybatis
-  mysql
-  spring相关
-  aop织入
-  mybatis-spring【new package】
<dependencies>
	...<>
</dependencies>

2、编写配置文件

3、测试

10.1、回顾mybatis

步骤:

  • 编写实体类

    @Data
    public class User(){
        private String name;
        private Integer age;
    }
    
    
  • 编写核心配置文件

    <typeAliases>
    	<package name="com.lh.pojo"></package>
    </typeAliases>
    
    <environments default="development">
    	<environment id="development">
        	<transactionManager type="JDBC">					</transactionManager>
            <dataSource type="POOLED">
            	<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
                <property name="url" value="xxx"></property>
                <property name="username" value="xxx"></property>
                <property name="password" value="xxx"></property>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
    	<mapper class="com.lh.mapper.UserMapper">			</mapper>
    </mappers>
    
    
  • 编写接口

    public interface UserMapper(){
        public List<User> selectUser();
    }
    
    
  • 编写mapper.xml

    <mapper namespace="com.lh.mapper.UserMapper">
    	<select id="selectUser" resultType="user">	
        	select *from mybatis.user;
        </select>
    </mapper>
    
    
  • 测试

    public class mytest(){
        @Test
        public void test(){
            String resources ="mybatis-config.xml";
            Resources input = Resources.getResourcesAsStream(resources);
            ......
        }
    }
    
    
    10.2 mybatis-spring

    整合mybatis的方式一:

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>
    
    
    //DataSource:使用spring的数据源替换mybatis的配置 c3p0 dbcp druid
    //使用spring直接提供的JDBC
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSPIRCE">
    	<property name="driverClassName" value="xxx"></property>
        <property name="url" value="xxx"></property>
        <property name="username" value="xxx"></property>
        <property name="password" value="xxx"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="" value=""></property>
        <property name="" value=""></property>
    </bean>
    
    

整合mybatis的方式二:(略)

11、声明式事务

1、事务?
  • 把一组业务当做一个业务来做,要么成功,要么失败!
  • 事务在项目开发中,十分重要,设计到数据的一致性问题,不可马虎~~
  • 确保完整性和一致性

事务的ACID原则:

  • 原子性
  • 一致性
  • 隔离性
    • 多个事务可能操作同一个资源,防止数据损坏
  • 持久性
    • 事务一旦提交,无论系统发生什么问题,结果都不在被影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51110014

你的三连是我更新的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值