Spring练习环境搭建
- 创建工程(Project、Module)
- 导入需要的依赖——pom.xml
- 创建包结构(controller、service、dao、domain、utils)
- 创建数据库——.sql
- 创建POJO类
- 配置文件(web.xml、applicationContext.xml、spring-mvc.xml、jdbc.properties、log4j.properties)
web.xml
<!--解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC的前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-mvc.xml
注:加上mvc命名空间、Controller层用组件扫描;Service、Dao、JDBCTemplate用Spring创建注入
<!-- 1、 mvc注解驱动 -->
<mvc:annotation-driven/>
<!-- 2、视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 3、静态资源访问权限开放 -->
<mvc:default-servlet-handler/>
<!-- 4、组件扫描 扫描Controller-->
<context:component-scan base-package="com.heima.controller"/>
<!-- 5、配置权限拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login"/>
<bean class="com.heima.interceptor.PrivilegeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
applicationContext.xml
注意加上context命名空间
<!-- 1、加载jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2、配置数据源对象 -->
<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>
<!-- 3、配置JDBCTemplate模板对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4、配置RoleService -->
<bean id="roleService" class="com.heima.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao"/>
</bean>
<!-- 5、配置RoleDao -->
<bean id="roleDao" class="com.heima.dao.impl.RoleDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--UserService-->
<bean id="userService" class="com.heima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<property name="roleDao" ref="roleDao"/>
</bean>
<!-- UserDao-->
<bean id="userDao" class="com.heima.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>