继续电商框架搭建~
- 4.服务器Java代码分层
采用 MVC模式:
- M概念对应:model包里的对象类
- V概念对应:WEB-INF/views 下的页面
- C概念对应:web包里的controller,表示逻辑处理。实际上逻辑处理又由多个部分组成 ,controller+service+dao,Controller是主要是用来接收请求+处理部分逻辑,service是用来把controller和dao结合起来的,处理部分逻辑,dao层是完成数据操作的。其中, service层和dao层我们又分别分为两层,接口层+实现层。实际工作中,一个项目有可能涉及到多个人、多个部门,这时候,需要合作的人之间会一起先定好接口,然后具体实现层就各归各位去完成了,合作的人之间不需要互相知道。
综上,我们至少需要新建6个package和1个folder,但因为该项目需要用mybatis,dao层的实现会用xml完成,我一般会放到resources/新建的mapper文件夹中,具体新建情况如下:
这里service.impl包就是service的实现层,dao层的sql实现,都在resources/mapper/下,一般以对应的dao接口为前缀+"mapper.xml" 来命名。
这几个层的调用顺序分别是:页面(发出请求)<--->controller<--->service<--->dao<--->数据库。
- 5.配置Mybatis
1>. 通过maven配置数据库以及Mybatis相关的包,要在pom.xml添加的依赖如下:
<!-- mysql驱动,链接mysql必须要有的包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- 把mybatis和spring结合起来的包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency>
2>.接下来,在web.xml配置整合Spring和mybatis的配置文件,添加内容如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mybatis.xml</param-value> </context-param>
3>.然后在resources/config/下新建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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 自动扫描该包,将注解的类变为bean,并注入上下文直接使用--> <context:component-scan base-package="com.qyuz.service.*" /> <!-- 由于包的兼容性问题, 这里不能用 ${jdbc.driver}类似的形式赋值了--> <!-- context:property-placeholder location="classpath:config/jdbc.properties" ignore-unresolvable="true"/>--> <!-- 1.数据库配置信息 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!--这里需要注意的是'$' 要用 '&'代替 --> <property name="url" value="jdbc:mysql://localhost:3306/ecom?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="jxh"/> <property name="password" value="123456"/> <property name="validationQuery" value="SELECT 1"/> <property name="testWhileIdle" value="true"/> <property name="timeBetweenEvictionRunsMillis" value="3600000"/> <property name="minEvictableIdleTimeMillis" value="1800000"/> <property name="testOnBorrow" value="true"/> </bean> <!-- 2.sessionFactory 将spring和mybatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--property name="configLocation" value="classpath:config/mybatis.cfg.xml" --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 3.自动扫描 将Mapper接口生成代理注入到Spring--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" autowire="byName"> <property name="basePackage" value="com.qyuz.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 4.配置事物 (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 5.设置事物的传播行为种类 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 6.定义一个切面,在定义的切面上加入事物 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.qyuz.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
在这个文件中,做了以下几件事:
- ①注册数据源并初始化,配置相关的数据库信息
- ②注册sqlSessionFactory数据库访问会话工厂(类似数据库连接池?),并配置mapper, <property name="mapperLocations" value="classpath:mapper/*.xml" />,也就是dao层的实现
- ③配置dao层接口,通过sqlSessionFactory将dao层注入到spring中
- ④注册事务并初始化
- ⑤和⑥结合起来配置事务,其中⑤是配置事务的传播行为种类,简单地说就是调用这些方法的时候,判断是否存在事务,如何处理,propagation的值表示如何处理;⑥是配置⑤发生的地方。这两个要结合使用。
至此,mybatis相关的配置完成。
- web.xml的其他配置
- 编码过滤器(顾名思义就是对请求进行编码上的过滤),如下:
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- 上下文监听,如下:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
查看下一篇博客,测试配置效果。