完整开发步骤
- 导包
- spring的jar包
- mybatis的jar包
- mybatis-Spring的jar包
- aop的依赖jar包
- oracle等数据库连接的jar包
- DataSource的jar包
- 日志包
- 导入配置文件
- web.xml
- mvc.xml
- applicationContext.xml
- applicationContext.properties
- 初始化配置
-
web.xml
-
spring核心控制器
-
指定mvc配置文件的路径
-
指定编码的filter
-
`<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springMVC配置文件的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<!-- 设置tomcat启动前去创建servlet对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 指定springMVC的所有控制器的访问路径都以*.do结尾 -->
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>` -
spring工厂初始化的监听器
-
spring配置文件初始化
-
`
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener><!--3. 手动指定配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>`
-
-
mvc.xml
- 注解管理controlller
- 注解开发mvc
- `<context:component-scan base-package="com.baizhi"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>`
-
applicationContext.xml
- 引入外部的配置文件
- ` <context:property-placeholder location="classpath:/application.properties"></context:property-placeholder>`
- 开启注解管理除了Controller
- `<context:component-scan base-package="com.baizhi"></context:component-scan>`
- 连接池
- `<bean id="dataSour" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="${database.username}"></property>
<property name="password" value="${database.password}"></property>
<property name="initialSize" value="${database.initialSize}"></property>
<property name="maxActive" value="${database.maxActive}"></property>
<property name="maxWait" value="${database.maxWait}"></property>
</bean>
` - 事务相关
- `<!-- 配置增强类所需要用到的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSour"></property>
</bean>` - `<!-- 配置能自动生成事务先关的增强类的类 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS" rollback-for="java.lang.RuntimeException"/>
<tx:method name="to*" read-only="false" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/>
<tx:method name="*" read-only="false" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/>
</tx:attributes>
</tx:advice>` - `<!-- aop动态代理选择切入点和组装 -->
<aop:config>
<aop:pointcut expression="execution(* com.baizhi.service.impl.*.*(..))" id="usercut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="usercut"/>
</aop:config>` - Mybatis相关
- `<bean id='sqlSessionFactory' class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSour"></property>
<property name="mapperLocations" value="classpath:/com/baizhi/dao/impl/*.xml"></property>
<property name="typeAliasesPackage" value="com.baizhi.entity"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.baizhi.dao"></property>
</bean>`
-