Spring注解
Spring配置文件比较繁琐,影响开发效率,用注解代替xml配置是一种趋势。
使用注解时,需要在applicationContex中配置组件扫描:
<context:component-scan base-package="com"/>
Spring原始注解
注解有:
实例化类
@Compnent("") 在所有层的类上实例化Bean
@Controller("") 在web层的类上实例化Bean
@Service("") 在service层
@Repository("") 在dao层
实例对象注入
@Autowired() 从Spring容器中匹配并注入,仅限于只有一个
@Qalifier("") 按照id值从容器中匹配并注入,需要结合Autowired()
@Resource("") 相当于上面两个的结合。
一般数据类型注入
@Value("${xxx}") 直接从配置文件中取值并赋值
作用范围
@Scope("prototype") 默认一个,现在可以实例多个类
初始化方法
@PostConstruct init()方法上面
销毁方法
@PreDestory destory()方法上面
Spring新注解
使用上面注解不能全部替代xml配置,有:
- 非自定义的Bean
- 加载properties文件的配置
<context:property-placeholder>
- 组件扫描
<context:component-scan base-package="com"/>
- 引入其他文件
<import>
新注解:
@Configuratioin 标志该类代替applicationContext.xml配置文件
@ComponentScan("com") 组件扫描该包
@PropertySource("classpath:druid.properties") 加载外部文件
@Bean("xxx")
@Import("") 引入其他的配置类
例子:连接池
在resources中放入druid.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db1
jdbc.username=root
jdbc.password=root
新建一个config.SpringConfig类
@Configuration
@ComponentScan("com")
@PropertySource("classpath:druid.properties")
public class SpringConfig {
@Value("${jdbc.driverClassName}") // ${} 这个叫SpringEL
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource")
public DruidDataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
测试类
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
DruidDataSource bean = app.getBean(DruidDataSource.class);
Connection conn = bean.getConnection();
System.out.println(conn);
conn.close();
Spring集成Junit
Spring测试很麻烦
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
DruidDataSource bean = app.getBean(DruidDataSource.class);
为了省略上面这些,需要
- 让SpringJunit测试,但是需要告诉他配置文件的名称
- 将需要测试的Bean,在测试类中注入
步骤:
- 导入spring集成junit的坐标
spring-test
- 使用@Runwith注解原来的运行期
- 使用@ContextConfigurationi指定配置类
- 使用@Autowired注入需要的测试对象
- 创建测试方法进行测试
测试例子
首先导入spring集成的junit坐标 spring-test
编写测试类,测试方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class SpringTest {
@Autowired
private DruidDataSource dataSource;
@Test
public void test1() throws SQLException {
System.out.println(dataSource.getConnection());
}
}
JdbcTemplate
Spring框架提供的JdbcTemplate简化了开发,提供了许多操作模板类。
使用步骤:
-
导入坐标:
spring-jdbc
和spring-tx
<!-- spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- 处理事务--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency>
-
创建数据库表和实体(domain模板)
-
使用JdbcTemplate
在上述测试类中,继续写:
@Test public void testSelectAll() throws SQLException { // 获取对象 JdbcTemplate jdbcTemplate = new JdbcTemplate(); // 获取数据源 jdbcTemplate.setDataSource(this.dataSource); // 操作 String sql = "select * from bank"; // RowMapper接口自动封装对象 List<Bank> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Bank>(Bank.class)); System.out.println(list); }
jdbcTemplate更多操作:
- 更新:
jdbcTemplate.update(sql, params)
- 查询多个:
jdbcTemplate.query(sql, RowMapper, params)
- 查询一个:
jdbcTemplate.queryForObject(sql, RowMapper, params)
- 聚合查询:
Long count = jdbcTemplate.queryForObject(sql, Long.class)