回顾
1,spring概述
spring是一个轻量级的一站式框架。
2,Ioc
控制反转。
bean标签:
-
id属性: 定义bean的名称
-
class属性: 配置全类名
-
scope属性: 配置bean的作用范围
- singleton(单例):默认。在创建核心容器时创建bean对象,并存储到核心容器中。
- prototype(多例): 在获取bean对象的时候创建bean对象。
-
生命周期相关的属性
- init-method
- destory-method
3,DI
依赖注入。spring将应用程序所依赖的资源赋值给应用程序。
注入方式:
-
set注入
<bean id="" class=""> <property name="属性名" ref|value=""/> </bean>
注意: 必须提供set方法。
-
构造方法注入(了解)
<bean id="" class=""> <constructor-arg name="参数名称" value|ref=""/> </bean>
4,给集合注入数据
-
list集合
<bean id="" class=""> <property name="属性名" > <list> <value>元素1</value> <value>元素2</value> </list> </property> </bean>
-
properties集合
<bean id="" class=""> <property name="属性名" > <props> <prop key="键">值1</prop> <prop key="键">值2</prop> </props> </property> </bean>
5,引入外部配置文件
-
properties配置文件
<context:property-placeholder location="classpath:配置文件路径">
使用${}来获取配置文件的数据。
-
xml配置文件
<import resource="配置文件路径">
今日内容
spring整合mybatis(使用xml配置)
注解开发
- 常用的注解
纯注解开发
spring整合mybatis(使用注解方式)
1,配置第三方的bean
2,spring整合mybatis
2.1 引入jar包
<!--mybatis的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql数据库驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring支持的jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--连接池技术包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--spring整合mybatis依赖的jar-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
2.2 替换mybatis的核心配合文件
<!--加载perperties配置文件的信息-->
<context:property-placeholder location="classpath:*.properties"/>
<!--加载druid资源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--
SqlSessionFactoryBean对象是用来创建SqlSessionFactory对象中
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.itheima.domain"/>
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
3,注解开发
作用: 简化或者替换xml配置。
优缺点:
- 优点:简化
- 缺点:增加了耦合性
4,bean定义的注解
在使用注解前需要先开启注解支持
<context:component-scan base-package="com.itheima"/>
-
@Component
作用: 替换xml配置文件中的bean标签
-
衍生注解
- @Controller
- @Service
- @Repository
和@Component注解的作用一样。只是为了做到见名知意。
@Controller --> 使用在web层的类上 @Service --> 使用在service层类上 @Repository --> 使用在dao层类上
-
@Scope(“singleton”) : 替换bean标签中的scope属性的。
-
@PostConstruct : 做初始化操作
注意: 使用在方法上。
-
@PreDestroy :在bean销毁前调用的方法
注意: 使用在方法上。
5,第三方资源bean定义
使用@Bean注解。
使用在方法上,将方法的返回值交给spring管理。
注意: @Bean可以不定义bean的名称,默认是方法的名称。
6,依赖注入
6.1 普通类型的注入
使用@Value注解:
- 使用在成员变量上或者方法上
@Value("30")
private Integer num;
6.2 对象类型的注入
使用@Autowired注解按照类型进行注入。
如果同类型的bean有多个,此时需要按照名称进行注入 @Qualifier
@Autowired
private UserDao userDao;
7,引入外部的properties配置文件
@PropertySource(“classpath:配置文件的路径”)
使用配置文件中的数据: @Value("${键名}")
8,纯注解开发
@Configuration
@ComponentScan(“要扫描的包”)
创建容器对象:
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
9,引入外部的配置类
@Import(外部配置类.class)
外部配置类:
public class JDBCConfig {
@Bean
public DruidDataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
ds.setUsername("root");
ds.setPassword("itheima");
return ds;
}
}
主配置类:
@Configuration
@Import({JDBCConfig.class})
@ComponentScan("com.itheima")
public class SpringConfig {
}
10,spring整合mybatis(注解版)
实现:
-
编写数据源的配置类
public class JDBCConfig { /* @Value("${jdbc.driver}") private String driver;*/ @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Bean("dataSource") public DataSource getDataSource(@Value("${jdbc.driver}") String driver){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }
-
spring整合mybatis的配置类
public class MyBatisConfig { @Bean public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer getMapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; } }
-
编写主配置类
@Configuration //标记该类是主配置类 @ComponentScan("com.itheima") //包扫描,开启注解开发 @PropertySource("classpath:jdbc.properties") // 引入外部的properties配置文件 @Import({JDBCConfig.class,MyBatisConfig.class}) //引入外部的配置类 public class SpringConfig { }
-
service类
@Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; public void save(Account account) { accountDao.save(account); } public void update(Account account){ accountDao.update(account); } public void delete(Integer id) { accountDao.delete(id); } public Account findById(Integer id) { return accountDao.findById(id); } public List<Account> findAll() { return accountDao.findAll(); } }
-
dao接口
public interface AccountDao { @Insert("insert into account(name,money)values(#{name},#{money})") void save(Account account); @Delete("delete from account where id = #{id} ") void delete(Integer id); @Update("update account set name = #{name} , money = #{money} where id = #{id} ") void update(Account account); @Select("select * from account") List<Account> findAll(); @Select("select * from account where id = #{id} ") Account findById(Integer id); }
-
测试类
public class App { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); AccountService accountService = ctx.getBean(AccountService.class); Account ac = accountService.findById(2); System.out.println(ac); } }
11,spring整合junit
-
引入jar包
<!--junit的jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--spring整合junit的依赖的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency>
-
测试代码
//设定spring专用的类加载器 @RunWith(SpringJUnit4ClassRunner.class) //设定加载的spring上下文对应的配置 @ContextConfiguration(classes = SpringConfig.class) //@ContextConfiguration(locations = "applicationContext.xml") public class UserServiceTest { @Autowired private AccountService accountService; @Test public void testFindById(){ Account ac = accountService.findById(2); Assert.assertEquals("Jock1",ac.getName()); } }
快捷创建测试类的快捷键 ctrl + shift + T
spring中有父子容器的概念,子容器可以使用父容器中的bean。
重点小结:
-
spring整合mybatis
- xml方式
- 注解方式
-
注解开发
-
开启注解的支持
- 配置文件
<context:component-scan base-package="com.itheima"/>
- 配置类
@ComponentScan("com.itheima")
-
常用注解
- bean定义
- @Component
- @Controller
- @Service
- 注入相关的注解
- @Autowired : 按照类型注入。如果需要按照名称注入 @Qualifier
- @Resource (了解)
- @Value
- @Bean : 配置第三方提供的类的bean对象
- bean定义
-
纯注解开发
- @Configuration : 标注类为配置类
- @ComponentScan : 包扫描
- @PropertySource : 引入外部的properties配置文件
- @Import : 引入外部的配置类
-