系统环境搭建
-
Spring的jar包11个
com.springsource.org.aopalliance-1.0.0.jar //Aopi联盟的jar包遵循其规则 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar //面向切面编程 spring-aop-5.0.1.RELEASEjar //实现aop spring-aspects-5.0.1.RELEASEjar //aspect整合的jar包 spring-beans-5.0.1.RELEASEjar spring-context-5.0.1.RELEASEjar spring-core-5.0.1.RELEASEjar spring-expression-5.0.1.RELEASEjar spring-jcl-5.0.1.RELEASEjar spring-jdbc-5.0.1.RELEASEjar //访问数据库 spring-tx-5.0.1.RELEASEjar //事务
-
SpringMVC的jar包
spring-web-5.0.1.RELEASEjar spring-webmvc-5.0.1.RELEASEjar
-
MyBatis的jar包1个
mybatis-3.4.3.jar //核心jar包 //依赖包12个
-
MyBatis和Spring整合的jar包1个
mybatis-spring-1.2.1.jar
-
数据库驱动jar包1个
mysql-connector-java-5.1.7-bin.jar
-
数据源(bruid的jar包1个)
druid-1.1.5.jar
-
JSTL的jar包2个
jstl-1.1.2.jar standard-1.1.2.jar
创建配置文件
- spring-mvc.xml中
<?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"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--注册组件扫描,扫描所有的handler包下的注册@Controller--><context:component-scan base-package="com.hrm.**.handler"/><!--mvc注解驱动,需要类型转换时候自动--><mvc:annotation-driven/><mvc:default-servlet-handler/>
</beans>
- spring-service.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 扫描@Service--><context:component-scan base-package="com.hrm.**.service"/></beans>
- spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--生成dao代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.hrm.**.dao"/><property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/></bean><!--注册SqlSessionFactory--><bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="MyDataSource"/><property name="configLocation" value="classpath:mybatis.xml"/></bean>
</beans>
- spring-ds.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:jdbc.properties"/><bean id="MyDataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///hrm
jdbc.username=root
jdbc.password=wqd123
- mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><typeAliases><package name="com.hrm.commons.benas"/></typeAliases><!--注册映射文件--><mappers><!--<mapper resource="com/itheima/dao/IUserDao.xml"/>--><package name="com.hrm.**.dao"></package></mappers>
</configuration>
- spring-tx.xml
<?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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="MyDataSource"/></bean><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/></tx:attributes></tx:advice><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* *..service.*.*(..))"/></aop:config>
</beans>
这些配置文件怎么才能运行:在WEB-INF下的web.xml中配置中央调度器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>hrm</display-name><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern> <!--/会出现静态资源访问不到,3种解决办法--></servlet-mapping> <!--最简单的一种就是springmvc.xml中配置--><!--<mvc:default-servlet-handler>--><!--注册字符过滤器-->
<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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>