SSM三大框架整合-分模块版本
0.创建数据库和表结构
create database ssm; use ssm; create table account( id int primary key auto_increment, name varchar(20), money double(20,2) );
1.创建父工程 oa(pom文件中加上 <packaging>pom</packaging>,删除src文件,因为它仅仅作为一个父工程,不编写代码)
在 pom 文件中定义全局版本
<properties> <spring.version>5.0.3.RELEASE</spring.version> </properties>
2.在父工程上右键创建子模块
oa_dao:
在 pom 文件添加依赖
<dependencies> <!-- 配置实体类依赖 --> <dependency> <groupId>com.fgy</groupId> <artifactId>oa_domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- mybatis和spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
oa_service:
在 pom 文件添加依赖
<dependencies> <!-- 配置实体类依赖 --> <dependency> <groupId>com.fgy</groupId> <artifactId>oa_domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 配置持久层依赖 --> <dependency> <groupId>com.fgy</groupId> <artifactId>oa_dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
oa_domain:可以不划分此模块,在用到此模块时要记得在对应的 pom 文件中导入此依赖
oa_utils:可以不划分此模块,在用到此模块时要记得在对应的 pom 文件中导入此依赖
oa_web:使用 webapp 骨架创建,pom文件中加上 <packaging>war</packaging>
在 pom 文件添加依赖
<dependencies> <!-- 配置业务层依赖 --> <dependency> <groupId>com.fgy</groupId> <artifactId>oa_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
<finalName>oa_web</finalName> <plugins> <!-- 设置编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version > <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- 设置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <port>8080</port> </configuration> </plugin> </plugins>
3.在 oa_dao 的 resources 目录下创建 applicationContext-dao.xml 文件
<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"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.fgy.oa.dao"/> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 给实体类起别名 --> <property name="typeAliasesPackage" value="com.fgy.oa.domain"/> </bean> <!-- mapper 扫描器 :用来产生代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.fgy.oa.dao"/> </bean> </beans>
在 oa_dao 中创建持久层接口:
/** * 账户持久层接口 */ @Repository("accountDao") public interface AccountDao { /** * 根据id查询账户信息 * @param id * @return */ @Select("select * from account where id = #{id}") Account findById(Integer id); }
4.在 oa_domain 中创建实体类
/** * 账户实体类 */ public class Account { private Integer id; private String name; private Double money; // get 和 set 方法 }
5.测试持久层接口
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-dao.xml") public class AccountDaoTest { @Autowired private AccountDao accountDao; @Test public void testFindById() { Account account = accountDao.findById(1); System.out.println(account); } }
6.在 oa_service 的 resources 目录下创建 applicationContext-service.xml 文件
<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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 测试service层时需要导入使用,整合开发统一导入web层即可 --> <!--<import resource="applicationContext-dao.xml"/>--> <!-- 开启注解扫描 --> <context:component-scan base-package="com.fgy.oa.service.impl"/> <!-- 事务配置 --> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="*" read-only="false" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置 aop --> <aop:config> <!-- 配置切入点表达式 --> <aop:pointcut id="pt" expression="execution(* com.fgy.oa.service.impl.*.*(..))"/> <!-- 配置切入点表达式和事务通知的对应关系 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor> </aop:config> </beans>
dao 新增的方法:
/** * 根据名称查询账户信息 * @param name * @return */ @Select("select * from account where name = #{name}") Account findByName(String name); /** * 更新账户信息 * @param account1 */ @Select("update account set name = #{name}, money = #{money} where id = #{id}") void update(Account account1);
service 代码:
/** * 账户业务层接口 */ public interface AccountService { /** * 根据id查询账户信息 * @param id * @return */ Account findById(Integer id); /** * 用户1向用户2转账 * @param user1 * @param user2 * @param money */ void transfer(String user1, String user2, Double money); }
/** * 账户业务层实现类 */ @Service("accountService") public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; /** * 根据id查询账户信息 * @param id * @return */ @Override public Account findById(Integer id) { return accountDao.findById(id); } /** * 用户1向用户2转账 * @param user1 * @param user2 * @param money */ @Override public void transfer(String user1, String user2, Double money) { // 查询用户1 Account account1 = accountDao.findByName(user1); // 查询用户2 Account account2 = accountDao.findByName(user2); // 转账 account1.setMoney(account1.getMoney() - money); account2.setMoney(account2.getMoney() + money); // 更新用户1 accountDao.update(account1); // int i = 1/0; // 更新用户2 accountDao.update(account2); } }
7.测试业务层
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-service.xml") public class AccountServiceTest { @Autowired private AccountService accountService; @Test public void testTransfer() { accountService.transfer("张三", "李四", 100.0); } }
8.在 oa_web 的 resources 目录下创建 applicationContext.xml 和 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <import resource="applicationContext-dao.xml"/> <import resource="applicationContext-service.xml"/> </beans>
<?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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.fgy.oa.controller"/> <!-- 处理器映射器、处理器适配器 默认注册处理请求,参数和返回值的类。主要实现了两个接口:HandlerMapping与HandlerAdapter。 --> <mvc:annotation-driven/> <!-- 静态资源不拦截 --> <mvc:default-servlet-handler/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
controller 代码:
/** * 账户表现层 */ @Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; /** * 查询账户详情 * @param model * @param id * @return */ @RequestMapping("/findDetail/{id}") public String findDetail(Model model, @PathVariable(name = "id") Integer id) { Account account = accountService.findById(id); model.addAttribute("account", account); return "detail"; } /** * 测试转账 * @return */ @RequestMapping("/transfer") public String transfer() { accountService.transfer("张三", "李四", 100.0); return "success"; } }
index.jsp 页面和 detail.jsp页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>index</title> </head> <body> <a href="${pageContext.request.contextPath}/account/findDetail/1">查询详情</a> <br/> <a href="${pageContext.request.contextPath}/account/transfer">测试事务是否正常</a> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>detail</title> </head> <body> id:${account.id} <br/> name:${account.name} <br/> money:${account.money} <br/> <img src="${pageContext.request.contextPath}/images/test.jpg">测试静态文件是否放行 </body> </html>
9.配置 web.xml 文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 配置过滤器,解决中文乱码的问题 --> <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> <!-- forceEncoding=true意思是指无论客户端请求是否包含了编码,都用过滤器里的编码来解析请求 --> <init-param> <param-name>forceEncoding</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 spring 核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ContextLoaderListener 默认去WEB-INF下加载名称为applicationContext.xml文件 重新指定配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置 SpringMVC的核心控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Servlet的初始化参数,读取spring-web的配置文件,创建spring容器 如果不指定,默认读取 /WEB-INF/dispatcherServlet-servlet.xml文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置servlet启动时加载对象,servlet默认发起请求时才创建,这里是加载配置文件,启动时就需要加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
10.启动 Tomcat 测试:
..........................