Spring配置文件(入门级)
1.配置文件根节点:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2.提供数据库连接(dataSource)(提供数据库连接参数):
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db_java1ssm?useSSL=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
3.创建jdbcTemplate对象,自动注入dataSource(对jdbc实现深层次封装):
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
4.扫描注解:扫面Spring包中的注解(扫描注解的类,然后注册为bean,用于连接的):
<context:component-scan base-package="com.spring"/>
5.创建dao层对象:
<bean id="springDao" class="com.spring.dao.impl.SpringDaoImpl"/>
6.创建service层对象:
<bean id="springService" class="com.spring.service.impl.SpringServiceImpl"/>
6.完整代码:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--1.提供数据库连接(dataSource)(提供参数)-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db_java1ssm?useSSL=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--2.创建jdbcTemplate对象,自动注入dataSource(实现了对jdbc的深层次封装)-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3.自动扫描器-->
<context:component-scan base-package="com.spring"/>
<!-- 4.创建dao层对象-->
<bean id="springDao" class="com.spring.dao.impl.SpringDaoImpl"/>
<!-- 5.创建service层对象-->
<bean id="springService" class="com.spring.service.impl.SpringServiceImpl"/>
</beans>