B038-Spring基础

mybatis高级查询(动态sql)

EmpMapper

public interface EmpMapper {

	List<Emp> findAll();
	
	/**
	 * 根据条件查询
	 */
	List<Emp> findByQuery(Emp emp);
}

EmpMapper.xml

<mapper namespace="cn.itsource.mapper.EmpMapper">

	<!-- List<Emp> findAll() -->
	<select id="findAll" resultType="Emp">
		select * from emp
	</select>
	
	<!-- List<Emp> findByQuery(Emp emp) -->
	<select id="findByQuery" resultType="Emp">
		select * from emp
		<!-- where: 中间加条件 :自动识别是否需要and
			 if: 判断使用 
		-->
		<where>
			<if test="deptno != null">
				and deptno = #{deptno}
			</if>
			<!-- ognl表达式   判断ename不是null,并且不是空字符串
				trim():去除两端空白
				concat():拼接参数
			-->
			<if test="ename != null and ''!=ename.trim()" >
				and ename like concat('%',trim(#{ename}),'%')		
			</if>			
			<if test="address != null and ''!=address.trim()" >
				and address like concat('%',trim(#{address}),'%')		
			</if>
			<if test="sal != null">
				<choose>
					<when test="sal==3000">
						<!-- 查询的就是3000以下的薪水的员工信息 -->
						and sal &lt; 3000
					</when>
					<when test="sal==5000">
						<!-- 查询的就是3000到5000薪水的员工信息 -->
						and sal &gt;= 3000 and sal &lt; 5000
					</when>
					<when test="sal==8000">
						<!-- 查询的就是5000到8000的薪水的员工信息 -->
						and sal &gt;= 5000 and sal &lt; 8000
					</when>
					<when test="sal==8001">
						<!-- 查询的就是8000以上的薪水的员工信息 -->
						and sal &gt;= 8000
					</when>
				</choose>	
			</if>
		</where>
	</select>
	
</mapper>

EmpTest

	@Test
	public void testName() throws Exception {
		SqlSession session = MybatisUtils.getSqlSession();
		EmpMapper mapper = session.getMapper(EmpMapper.class);
//		List<Emp> list = mapper.findAll();
		
		Emp emp = new Emp(null, " 张 ", null, 8001.0, null);
		List<Emp> list = mapper.findByQuery(emp);
		list.forEach(System.out::println);
	}

spring

spring简介

为什么要使用Spring?

1.使代码实现松耦合【低耦合】 -> 减低维护难度
2.在单例和多例之间进行切换非常简单
3.管理事务非常简单
4.提供更加简单的测试

spring
是一个轻量级的开源的IOC和AOP容器框架
不属于哪一层,三层都在用
没有spring,java就可能已经被淘汰

IOC和AOP介绍

spring:是一个轻量级的开源的IOC和AOP容器框架

轻量级:相对于EJB【使用和框架设计都很复杂】来说
IOC:Inverse of control - 控制反转,让spring来创建和管理对象
AOP:面向切面编程,(后面再讲)
容器框架:用来管理对象。创建对象、保存对象、初始化和销毁

入门案例
导包

在这里插入图片描述
新建Java project,根目录新建lib文件夹,导包:core、context、beans、expression、logging

核心配置文件

tips:
schme约束的好处,代码提示,错误提醒;
引入本地约束文件后没有网约束也能生效

applicationContext.xml

<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
	
</beans>
获取对象

applicationContext.xml

<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="user" class="cn.ming.domain.User"></bean>
	
</beans>

SpringTest

	@Test
	public void testName() throws Exception {
		// 获取spring容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获取交给容器管理的类
		Object bean1 = ac.getBean("user");
		User bean2 = (User)ac.getBean("user");
		User bean3 = ac.getBean("user", User.class);
		User bean4 = ac.getBean(User.class);
		System.out.println(bean1+" - "+bean2+" - "+bean3+" - "+bean4);
		//cn.ming.domain.User@3701eaf6 - 
		//cn.ming.domain.User@3701eaf6 - 
		//cn.ming.domain.User@3701eaf6 - 
		//cn.ming.domain.User@3701eaf6
	}
迫切加载和懒加载

迫切加载:在创建容器的时候就创建了对象,如执行代码

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

延迟加载:在获取对象的时候才创建对象,如执行代码

User bean = ac.getBean("user", User.class);

如何将迫切加载转成延迟加载
(1).单个bean配置延迟加载,加lazy-init=“true”

<bean id="user" class="cn.ming.domain.User" lazy-init="true" ></bean>

(2).所有bean配置延迟加载,加default-lazy-init=“true”

<?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
http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true" >
	
	<bean id="user" class="cn.ming.domain.User" ></bean>
	
</beans>

测试代码见工程

BeanFactory和ApplicationContext区别和联系

联系:ApplicationContext接口继承自BeanFactory,拥有更多的功能
区别:ApplicationContext默认迫切加载,BeanFactory默认懒加载

测试代码见工程

spring管理bean

可以管理自定义的类,也可以管理jdk自带的类,如Date,ArrayList,但不能管理接口,如List

一般我们用spring管理外部bean,api自带的类在项目中自己new

DI依赖注入

含义:IoC是一种思想,它的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的;

Spring中对象的属性字段赋值,这称为依赖注入DI(Dependency Injection)

xml注入

必须有对应的setter方法,所有这种注入方式又称之为属性注入或setter方法注入

applicationContext.xml

	<bean id="dog111" class="cn.ming.domain.Dog">
		<property name="name" value="二哈"></property>		
	</bean>
	
	<bean id="person" class="cn.ming.domain.Person">
		<!--setter方法注入 
			name: 属性名称 - set方法后面的属性
			value: 赋值 - 只能赋值普通字段,不能赋值对象
			ref: 引用外部bean
		-->
		<property name="id" value="123"></property>
		<property name="name" value="王天霸"></property>
		<property name="age" value="20"></property>
		<property name="dog" ref="dog111"></property>
	</bean>

SpringTest

		// 获取容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 从容器中获取bean   person
		Person p1 = (Person) ac.getBean("person",Person.class);
		System.out.println(p1);		//Person [id=123, name=王天霸, age=20, dog=Dog [name=二哈]]
注解注入(简单介绍 后面用)

将注解写在setter方法上,也可以写在字段上,如果写在字段上可以不需要setter方法;

加上注解后会去配置文件找对应的bean进行注入

@Autowired:为Spring提供的注解,
默认按照类型匹配,也可以指定bean名称

 	@Autowired
	private Dog dog;

 	@Autowired
 	@Qualifier("dog111")
	private Dog dog;

@Resource:由J2EE提供,需要导入包javax.annotation.Resource,spring支持这个注解,
默认按照名字匹配,类型也必须一致

	@Resource
	private Dog dog;

	@Resource(name="dog111")
	private Dog dog;
Spring测试

背景:原始的测试方法代码量多,测试效率低 - 要重复的创建容器对象

案例:
1.导包
spring-test-4.1.2.RELEASE.jar,spring-aop-4.1.2.RELEASE.jar

2.代码
Student

public class Student {

	public void eat() {
		System.out.println("吃饭中。。。");
	}
}

applicationContext.xml

<bean id="student" class="cn.ming.domain.Student" > </bean>

SpringTest2

@ContextConfiguration("classpath:applicationContext.xml") // 加载核心配置文件  创建容器
@RunWith(SpringJUnit4ClassRunner.class)// 我使用的是spring的测试类
public class SpringTest2 {
	
//	@Autowired
	@Resource
	private Student student;
	
	@Test
	public void testName() throws Exception {
		student.eat();
	}

}
bean的作用域

spring管理的bean,默认是单例的
测试:

	@Autowired
	private Student student1;
	
	@Resource
	private Student student2;
	
	@Test
	public void test1(){
		// spring管理的bean,默认是单例的
		System.out.println(student1==student2);		//true
	}

bean配置加上scope="prototype"可改为多例

<bean id="student" class="cn.ming.domain.Student" scope="prototype"> </bean>
	@Autowired
	private Student student1;
	
	@Resource
	private Student student2;
	
	@Test
	public void test1(){
		// spring管理的bean,默认是单例的
		System.out.println(student1==student2);		//false
	}
bean的生命周期

Spring中Bean的生命周期:Spring中管理的对象从创建到销毁的一个过程

1.实例化和初始化:Spring容器帮我们调用了无参构造【创建对象】和指定的初始化方法【一般做初始化工作】,如果是迫切加载就是创建容器的时候实例化和初始化,如果是懒加载就是获取对象的时候实例化和初始化

2.使用:服务方法在被调用的时候执行

3.销毁:容器关闭的时候调用销毁方法,但是仅仅是的调用了这个方法,并没有销毁这个对象,可以在销毁方法中做一些销毁和清理工作【释放资源,关流,关闭连接】,后期jvm的垃圾回收器真实销毁对象

代码:

LifeBean

public class LifeBean {

	public void init(){
		System.out.println("初始化了。。。。");
	}
	
	public void service(){
		System.out.println("服务方法。。。");
	}
	
	public void destroy(){
		System.out.println("销毁方法。。。。");
	}
}

applicationContext.xml

<!-- init-method="init":指定初始化方法 ; destroy-method="destroy":指定销毁方法-->
<bean id="lifeBean" class="cn.ming.domain.LifeBean" init-method="init" destroy-method="destroy"></bean>

SpringTest2

	@Autowired
	private LifeBean lifeBean; 
	
	@Test
	public void test2(){
		lifeBean.service();
	}	//初始化了。。。。	服务方法。。。	销毁方法。。。。
多例默认是懒加载

因为它不知道创建多少个对象,只能懒加载

测试见工程

  • 19
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值