整合使用的技术
1). Spring 5.0.2
2). mybatis 3.4.5
3). SpringMVC 5.0.2
4). log4J2 2.9.1
5). bootstrap 3.3.5
6). jquery 1.9.1
引入的依赖
Spring相关的
1). spring-context : Spring容器
2). spring-tx : Spring事务
3). spring-jdbc : SpringJDBC
4). spring-test : Spring单元测试
5). spring-webmvc : SpringMVC
mybatis相关的
1). mybatis : mybatis核心
2). mybatis-spring :mybatis与spring整合
3) 切面相关的
aspectjweaver : AOP切面
4) 数据源相关(选择使用):
c3p0
commons-dbcp
spring自带的数据源
5) 单元测试相关的:
junit : 单元测试,与spring-test放在一起做单元测试
6) ServletAPI相关的
jsp-api : jsp页面使用request等对象
servlet-api : java文件使用request等对象
7) 日志相关的:
log4j-core : log4j2核心包
log4j-api : log4j2的功能包
log4j-web : web项目相关日志功能
slf4j-api : 另外一种日志包,
slf4j:Simple Logging Facade for Java为java做简单的日志记录此处和log4j一起
log4j-slf4j-impl : slf4j的log4j实现类,也就是说slf4j的日志记录功能由log4j实现
log4j-jcl : 程序运行的时候检测用了哪种日志实现类现在叫Apache Common Logging
8) 数据库相关的
mysql-connector-java : mysql的数据库驱动包
ojdbc.jar
9) 页面表达式
JSTL : JSTL标签库必须jar包 基础功能
standard : JSTL标签库的必须jar包 进阶功能
10) 文件上传
commons-fileupload : 上传插件
commons-io : IO操作包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!--对JSP的支持-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- 实体类的创建
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/mybatisdb?characterEncoding=utf8
jdbc.username = root
jdbc.password = root
mappers.package = com.xzw.mapper
单独dao层需要的整合之前的配置文件
\<!--持久层需要的配置 -开始 -->
\<!--1. 引入属性文件:jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
\<!--2. 配置数据源: c3p0 ,dbcp ,spring jdbc自带数据源-->
\<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
\<property name="driverClass" value="${jdbc.driver}"/>
\<property name="jdbcUrl" value="${jdbc.url}"/>
\<property name="user" value="${jdbc.user}"></property>
\<property name="password" value="${jdbc.password}">\</property>
\</bean>
\<!--3. 配置 sessionFactory , session工厂类-->
\<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
\<!--1), 必须注入的一个属性: dataSource-->
\<property name="dataSource" ref="dataSource"></property>
\<!--2) 配置其他的属性:省略 可以加可以不加-->
\<property name="configLocation" value="classpath:SqlMapConfig.xml">\</property>
\<property name="configurationProperties">
\<props>
\<prop key="cacheEnabled ">true</prop>
\</props>
\</property>
\</bean>
\<!--4. 创建动态代理对象: 扫描包,创建动态代理对象-->
\<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
\<!--指定扫描的持久层接口-->
\<property name="basePackage" value="com.xzw.dao"></property>
\</bean>
单独service层的配置文件
\<!--业务层需要的配置-事务- 开始-->
\<!--1. 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入属性:dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--2. 事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--如果是find,get开头的方法,都是只读的事务-->
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<!--如果不是find开头,开启事务管理-->
<tx:method name="*"></tx:method>
</tx:attributes>
</tx:advice>
<!--3. aop管理事务 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"></aop:advisor>
</aop:config>
<!--4. 扫描包,创建对象-->
<context:component-scan base-package="com.itheima.service.impl"></context:component-scan>
<!--业务层需要的配置- 结束-->
整合service和daoceng(Spring和Mybatis)
整合Spring和Mybatis之后Mybatis的配置文件是空的,建议把配置文件留下。
Spring的配置文件建议分开管理
aplicationcontext-dao.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/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">
<!--这个配置是spring的dao的配置,因为Spring涉及的东西太多了-->
<!--如果只是使用一个配置文件,难以管理(现阶段体会不到)-->
<!--所以现在的做法是分开配置,方便管理,使用的时候需要把所有的配置一起加载-->
<!--加载jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<!--配置SqlSessionFactory,需要使用mybatis-spring整合包中的类-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载MyBatis的配置文件,SqlMapConfig.xml.现在为空文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--配置Mapper扫描,需要使用mybatis-spring整合包中的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置Mapper扫描的包-->
<property name="basePackage" value="com.itheima.mapper"/>
</bean>
</beans>
aplicationcontext-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/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">
<!--扫描service的注解,让注解生效-->
<context:component-scan base-package="com.itheima.service"/>
</beans>
applicationContext-trans.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--这里报错,原因是IDEA在这个xml配置中没有找到dataSource-->
<!--不用管这个错误,Spring启动的时候,会把所有的配置全部加在,包括数据源的配置(在dao里)-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置在哪个方法上执行-->
<tx:advice id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<!--所有以trans,save,update,delete开头的方法都会进行事务管理,
因为默认的配置就是要进行事务管理-->
<tx:method name="trans*"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<!--以下配置都不进行事务管理-->
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置在什么地方执行-->
<aop:config>
<aop:advisor advice-ref="tx"
pointcut="execution(* com.itheima.service.impl.*.*(..))"/>
</aop:config>
</beans>
SpringMVC.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/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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置Controller的包扫描-->
<context:component-scan base-package="com.itheima.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>