3.1 需求描述
本案例基于maven构建 SSM(Spring+SpringMVC+Mybatis)工程,通过maven坐标进行依赖管理。最终实现根据 id 查询商品信息的功能。
3.2 构建maven工程
1.数据库环境搭建
①创建数据库ssmtest
②创建商品表item
[AppleScript] 纯文本查看 复制代码
1 2 3 4 5 6 7 8 |
|
2.maven项目构建
①创建maven web项目
②配置pom.xml文件
③实现spring+mybatis整合
创建POJO类
[AppleScript] 纯文本查看 复制代码
1 2 3 4 5 6 7 8 |
|
持久层DAO接口编写
[AppleScript] 纯文本查看 复制代码
1 2 3 |
|
Mapper映射文件编写
[AppleScript] 纯文本查看 复制代码
1 2 3 4 5 6 7 |
|
业务层Service编写
[AppleScript] 纯文本查看 复制代码
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
springmvc.xml文件编写
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.itheima.ssm.controller"/>
<!-- 配置视图解析器的前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix“ value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
jsp页面编写
配置web.xml文件
4.分模块构建maven工程
4.1 分模块构建maven工程分析
在现实生活中,汽车厂家进行汽车生产时,由于整个生产过程非常复杂和繁琐,工作量非常大,所以车场都会将整个汽车的部件分开生产,最终再将生产好的部件进行组装,形成一台完整的汽车。
4.2 maven工程的继承
在Java语言中,类之间是可以继承的,通过继承,子类就可以引用父类中非private的属性和方法。同样,在maven工程之间也可以继承,子工程继承父工程后,就可以使用在父工程中引入的依赖。继承的目的是为了消除重复代码。
4.3 maven工程的聚合
在maven工程的pom.xml文件中可以使用<modules>标签将其他maven工程聚合到一起,聚合的目的是为了进行统一操作。
例如拆分后的maven工程有多个,如果要进行打包,就需要针对每个工程分别执行打包命令,操作起来非常繁琐。这时就可以使用<modules>标签将这些工程统一聚合到maven工程中,需要打包的时候,只需要在此工程中执行一次打包命令,其下被聚合的工程就都会被打包了。