Spring的学习(二)
1.spring-jdbc
需要的jar包:spring-jdbc
1.1 spring.xml内部配置jdbc参数
Spring.xml代码:
<!-- 数据源获取连接 -->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/yulu"></property>
<property name="username" value="root"></property>
<property name="password" value="zyp"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 创建模板,模板封装了操作数据的常用方法 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSouce"></property>
</bean>
Java代码:
public class Test {
static JdbcTemplate jt;
static {
// 获取容器
ApplicationContext context = new GenericXmlApplicationContext("classpath:lesson2/jdbc/spring.xml");
// 创建jdbc模板
jt = (JdbcTemplate) context.getBean("jdbcTemplate");
}
public static void main(String[] args) {
query("select * from text");
}
/**
* 查
*/
public static void query(String sql) {
//获取数据queryForList
List<Map<String, Object>> list = jt.queryForList(sql);
// 遍历数据
for (Map<String, Object> map : list) {
System.out.println(map);
}
}
/**
* 增、删、改
* */
public static void sendSql(String sql){
//发送sql语句execute
jt.execute(sql);
}
}
1.2 spring.xml中获取jdbc参数(properties文件)
1.2.1 bean定义读取资源文件
<!-- username是关键字,bean定义读取资源文件,优先读取文件的值 -->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:lesson2/jdbc/jdbc.properties"></property>
</bean>
<!-- 数据源获取连接 -->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 创建模板,模板封装了操作数据的常用方法 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSouce"></property>
</bean>
1.2.2 context命名空间 读取资源文件
<!-- username是关键字,context命名空间读取资源文件,优先读取系统,读取的值为“Administrator”,需要将username换成别的值-->
<context:property-placeholder location="classpath:lesson2/jdbc/jdbc.properties"/>
<!-- 数据源获取连接 -->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 创建模板,模板封装了操作数据的常用方法 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSouce"></property>
</bean>
2.Classpath中扫描Bean
组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解(annotation)的Bean.
特定注解包括:
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 :
base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
当需要扫描多个包时, 可以使用逗号分隔.
如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:
<context:component-scan base-package="com.et">
<context:include-filter type=“annotation” expression=“org.*.stereotype.Service"/>
<context:exclude-filter type="assignable" expression="com.et.bean.Address"/>
</context:component-scan>
<context:include-filter> 子节点表示要包含的目标类
<context:exclude-filter> 子节点表示要排除在外的目标类