我的项目一共会分为4个模块:entity、dao、service和web
一、创建父模块
填写GroupId与ArtifactId
填写项目名称和项目保存路径
因为是父模块所以src包可以删除
二、创建entity子模块
右键项目选择Module
和父模块一样点击下一步
填写ArtifactId
点击Finish子模块创建完成
结构如下
dao、service的步骤和entity子模块步骤一样,创建完成后结构如下
三、创建web子模块
点击Module
勾选上选择webapp点击下一步
填写ArtifactId点击下一步
点击下一步
点击finish多模块创建完成
多模块结构如下
可以看到idea创建时web模块的main包下的结构和其他模块不同,可以手动修改,修改完成后点击ok
正确的结构如下
至此,我们的多模块创建完成,接下来整合ssm
四、添加模块之间的相关依赖
这是preant模块的pom.xml文件如下
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.chaoqigroupId> <artifactId>chaoqi_preantartifactId> <packaging>pompackaging> <version>1.0-SNAPSHOTversion> <modules> <module>chaoqi_entitymodule> <module>chaoqi_daomodule> <module>chaoqi_servicemodule> <module>chaoqi_webmodule> modules> <properties> <junit.version>4.12junit.version> <spring.version>4.1.3.RELEASEspring.version> <mybatis.version>3.2.8mybatis.version> <mybatis.spring.version>1.2.2mybatis.spring.version> <mybatis.paginator.version>1.2.15mybatis.paginator.version> <mysql.version>5.1.32mysql.version> <slf4j.version>1.6.4slf4j.version> <jackson.version>2.4.2jackson.version> <druid.version>1.0.9druid.version> <httpclient.version>4.3.5httpclient.version> <jstl.version>1.2jstl.version> <servlet-api.version>2.5servlet-api.version> <jsp-api.version>2.0jsp-api.version> <joda-time.version>2.5joda-time.version> <commons-lang3.version>3.3.2commons-lang3.version> <commons-io.version>1.3.2commons-io.version> <commons-net.version>3.3commons-net.version> <pagehelper.version>5.0.0pagehelper.version> <jsqlparser.version>0.9.1jsqlparser.version> <commons-fileupload.version>1.3.1commons-fileupload.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>joda-timegroupId> <artifactId>joda-timeartifactId> <version>${joda-time.version}version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>${commons-lang3.version}version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-ioartifactId> <version>${commons-io.version}version> dependency> <dependency> <groupId>commons-netgroupId> <artifactId>commons-netartifactId> <version>${commons-net.version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>${jackson.version}version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclientartifactId> <version>${httpclient.version}version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>${junit.version}version> <scope>testscope> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>${mybatis.spring.version}version> dependency> <dependency> <groupId>com.github.miemiedevgroupId> <artifactId>mybatis-paginatorartifactId> <version>${mybatis.paginator.version}version> dependency> <dependency> <groupId>com.github.pagehelpergroupId> <artifactId>pagehelperartifactId> <version>${pagehelper.version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql.version}version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>${druid.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>${jstl.version}version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>servlet-apiartifactId> <version>${servlet-api.version}version> <scope>providedscope> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jsp-apiartifactId> <version>${jsp-api.version}version> <scope>providedscope> dependency> project>
entity模块的pom.xml
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>chaoqi_preantartifactId> <groupId>com.chaoqigroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>chaoqi_entityartifactId> project>
dao模块的pom.xml
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>chaoqi_parentartifactId> <groupId>com.chaoqigroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>chaoqi_daoartifactId> <dependencies> <dependency> <groupId>com.chaoqigroupId> <artifactId>chaoqi_entityartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> dependency> <dependency> <groupId>com.github.miemiedevgroupId> <artifactId>mybatis-paginatorartifactId> dependency> <dependency> <groupId>com.github.pagehelpergroupId> <artifactId>pagehelperartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.8.4version> dependency> dependencies> <build> <resources> <resource> <directory>src/main/javadirectory> <includes> <include>**/*.propertiesinclude> <include>**/*.xmlinclude> includes> <filtering>falsefiltering> resource> resources> build> project>
service模块的pom.xml
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>chaoqi_parentartifactId> <groupId>com.chaoqigroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>chaoqi_serviceartifactId> <dependencies> <dependency> <groupId>com.chaoqigroupId> <artifactId>chaoqi_daoartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> dependency> dependencies> project>
web模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>chaoqi_parentartifactId> <groupId>com.chaoqigroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>chaoqi_webartifactId> <packaging>warpackaging> <name>chaoqi_web Maven Webappname> <url>http://maven.apache.orgurl> <dependencies> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>servlet-apiartifactId> <scope>providedscope> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jsp-apiartifactId> <scope>providedscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> dependency> <dependency> <groupId>com.chaoqigroupId> <artifactId>chaoqi_serviceartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-annotationsartifactId> <version>2.5.0version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>2.5.0version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> dependency> <dependency> <groupId>com.fasterxml.jackson.jrgroupId> <artifactId>jackson-jr-allartifactId> <version>2.5.0version> dependency> dependencies> <build> <finalName>chaoqi_webfinalName> build> project>
五、spring整合springMVC
创建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:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.chaoqi.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>
编写测试类controller
package com.chaoqi.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class TestController { private static final String Hello = "hello"; @RequestMapping("/test") public String test() { return Hello; } }
创建jsp页面
配置web-xml文件
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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_2_5.xsd" version="2.5"> CharacterEncodingFilter class>org.springframework.web.filter.CharacterEncodingFilter class>encoding UTF-8 CharacterEncodingFilter /* chaoqi-web org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/springmvc.xml 1 chaoqi-web /
接下来就是运行项目打,首先打包项目
配置完后点击OK
build完成后,结构如下
配置tomcat
点击运行
结果如下
六、spring整合mybatis
创建数据库 数据库采用的是mysql数据库
--学生登陆表 CREATE TABLE `user_login` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键(自增长)', `user_name` varchar(30) DEFAULT NULL COMMENT '用户名', `pass_word` varchar(30) DEFAULT NULL COMMENT '密码', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
添加两条数据
insert into user_login (user_name,pass_word) VALUES ('猪点点','12345') insert into user_login (user_name,pass_word) VALUES ('会飞的大象','12345')
创建实体类
创建UserLoginMapper 接口和xml
创建service接口和serviceImpl实现类
创建db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.host=localhost
jdbc.database=test
jdbc.userName=root
jdbc.passWord=123456
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=1000
创建applicatiionController-service.xml和applicatiionController-dao.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:component-scan base-package="com.chaoqi.service">context:component-scan> beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:properties/*.properties" /> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="jdbc:mysql://${jdbc.host}:3306/${jdbc.database}?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull" /> <property name="username" value="${jdbc.userName}" /> <property name="password" value="${jdbc.passWord}" /> <property name="initialSize" value="${jdbc.initialSize}">property> <property name="maxActive" value="${jdbc.maxActive}">property> <property name="maxIdle" value="${jdbc.maxIdle}">property> <property name="minIdle" value="${jdbc.minIdle}">property> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mappings/*.xml">property> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.chaoqi.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> tx:attributes> tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.chaoqi.service.*.*(..))" /> aop:config> beans>
编辑web-xml
xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring/applicationContext-*.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet> <servlet-name>chaoqi-webservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring/springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>chaoqi-webservlet-name> <url-pattern>/url-pattern> servlet-mapping> web-app>
编写controller
package com.chaoqi.controller; import com.chaoqi.entity.UserLogin; import com.chaoqi.service.UserLoginService; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class TestController { private static final String HELLO = "hello"; @Autowired private UserLoginService userLoginService; @RequestMapping("/test") public String test() { return HELLO; } @ResponseBody @RequestMapping("/getUserLogin") public ListgetViideType() { List userLogin = userLoginService.getVideoInfo(null); for (UserLogin userLogins : userLogin) { } return userLogin; } }
重新编译项目,运行页面如下
整合成功!由于第一次写文章,若有不正确的地方请大家多多指教,谢谢!(源码下载地址:https://github.com/caicahoqi/ChaoqiIsPrivateLibrary)