IDEA使用Maven搭建SSM框架
搭建环境:intellij idea 2017
jdk 1.8
tomcat 8.5
mysql 5.7
spring 4.x
mybatis 3.x
这个过程确实太麻烦了,我用了两个小时
所以建议用springboot,简单很多很多
第一步:创建项目
创建一个空项目
第二步:创建父工程
file-->new-->module
选中maven,不勾选create from archetype,直接next
输入组id:通常是com.xxx.项目名
输入artifactid:common-parent(父工程)
点击next,这时候刚才写的-被去掉了,可以自己加上
finish完成
接下来会跳出来一个对话框:non project file protection
点击ok
pom.xml必须要加入打包方式:pom
<?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.0</modelversion> <groupid>org.dreamtech.edu</groupid> <artifactid>common-parent</artifactid> <version>1.0-snapshot</version> <!--父工程--> <packaging>pom</packaging> </project>
由于父工程不是用来写代码的,把src目录删除了
第三步:配置idea的maven
file-->setttings
配置自己的maven目录和配置文件
当然,也可以采用bundle的maven,但是必须要修改配置文件,因为默认目录没有settings.xml
第四步:创建子工程
选中common-parent父工程,右键new-->mudule
首先创建的是model模块:
具体的选项我截图了:
点击next:
输入artifacid:xxx-model
next-->next
注意要把子工程放在父工程的下面,要不然无法成功创建
点击finish即可
然后maven会自动下载一些东西,我们等待它完成,最多三分钟时间
打开子工程,我们在src-->main-->java目录下写java代码
我简单地创建一个user:
package org.dreamtech.edu.model; public class user { private int id; private string username; private string password; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } @override public string tostring() { return "user{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
同目录下有一个app.java文件是自动生产的,没有什么用,删除了就行
然后用同样的方式创建子工程:service层dao层
命名方式通常是:xxx-service、xxx-dao
接下来创建web层,需要选择的就不是quickstart了
然后创建api层,也就是后台提供的接口:和上边web层一样
第五步:具体配置
子工程已经创建完成了,下面是具体的配置
首先,model层、service层、dao层需要在pom.xml配置packging
<packaging>jar</packaging>
然后在web层和api层需要配置成war,但是已经默认配置好了,所以不需要自行配置
在父工程中配置公共的依赖:
删除在每一个子工程中创建的依赖,找到每个pom.xml,剪切这一部分,复制到父工程的pom.xml中:
<dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
在父工程的pom.xml中可以进行版本号的配置:
<!--公共版本号管理--> <properties> <junit.version>4.11</junit.version> <spring.version>4.3.14.release</spring.version> <mybatis.version>3.4.1</mybatis.version> <mybatis-spring.version>1.3.1</mybatis-spring.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.12</slf4j.version> <jstl.version>1.2</jstl.version> <taglibs.version>1.1.2</taglibs.version> <c3p0.version>0.9.5.2</c3p0.version> <mysql.version>5.1.35</mysql.version> </properties> <!--公共依赖--> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies>
web层的配置:
pom.xml进行springmvc的配置
<!--添加依赖--> <dependencies> <!--springmvc--> <!--aop--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>${spring.version}</version> </dependency> <!--aspects--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>${spring.version}</version> </dependency> <!--beans--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <!--context--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <!--core--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <!--expression--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-expression</artifactid> <version>${spring.version}</version> </dependency> <!--jdbc--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <!--orm--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>${spring.version}</version> </dependency> <!--tx--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <!--test--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency> <!--web--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <!--webmvc--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> </dependencies>
注意:idea右下角有一个提示,点击允许maven自动导包
配置tomcat:
右上角启动箭头左边有个下拉框,点击编辑配置:
先导入需要运行的工程,点击绿色加号部署
这里先部署web层的war exploded,因为api部分还没有完成
然后配置热更新:
然后就可以点击绿色的开始箭头开启tomcat了
如果出现了下面这种情况,说明,之前的配置全都正确了!
springmvc的配置文件:
说到配置文件,那么它们应该放在哪里呢?
打开web层子工程,然后在main下新建java目录,右键mark dir as-->sources root
java文件夹就是我们放java源码的地方
再新建一个目录,resources作为存放配置文件的地方
右键resources,mark dir as-->resources root
如果都配置正确,目录结构应该是下面这样:
然后我们看看具体的springmvc配置文件:
写一个最简单的controller(在刚才新建的java文件夹中):
package org.dreamtech.edu.web.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller @requestmapping("user") public class usercontroller { @requestmapping("login") public string login(){ system.out.println("hello"); return null; } }
配置web.xml:这里需要修改约束,自动生成的版本太低
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>archetype created web application</display-name> <!-- 配置springmvc--> <servlet> <servlet-name>dispatcherservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherservlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
然后配置springmvc:
<?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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 1.注解扫描位置--> <context:component-scan base-package="org.dreamtech.edu.web.controller"/> <!-- 2.配置映射处理和适配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter"/> <!-- 3.视图的解析器--> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/modules/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
跑起来项目:
访问http://localhost:8080/user/login.do
发现在控制台打印了hello,配置成功
其实springmvc的配置还没有结束
但我们先来看dao层的配置
dao层的配置:
pom.xml配置
首先需要依赖model层
<dependencies> <dependency> <groupid>org.dreamtech.edu</groupid> <artifactid>edu-model</artifactid> <version>1.0-snapshot</version> </dependency> </dependencies>
依赖mybatis包:
<!-- 依赖mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>${mybatis.version}</version> </dependency>
写入一些简单的代码:
抽取mapper:
package org.dreamtech.edu.mapper.base; public interface basemapper<t> { public t findbyid(integer id); public t findbyuuid(string uuid); public void deletebyid(integer id); public void deletebyuuid(string uuid); public void update(t t); public void insert(t t); }
继承:
package org.dreamtech.edu.mapper; import org.dreamtech.edu.mapper.base.basemapper; import org.dreamtech.edu.model.user; public interface usermapper extends basemapper<user> { }
usermapper.xml:
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="org.dreamtech.edu.mapper.usermapper"> <select id="findbyid" resulttype="user" parametertype="int"> select * from t_user where id = #{id} </select> </mapper>
dao层就先配到这里
service层的配置:
依赖dao层
<dependencies> <dependency> <artifactid>edu-dao</artifactid> <groupid>org.dreamtech.edu</groupid> <version>1.0-snapshot</version> </dependency> </dependencies>
依赖spring:
<!-- 添加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-tx</artifactid> <version>${spring.version}</version> </dependency>
service层简单代码:
package org.dreamtech.edu.service.base; public interface ibaseservice<t> { public t findbyid(integer id); public t findbyuuid(string uuid); public void deletebyid(integer id); public void deletebyuuid(string uuid); public void update(t t); public void insert(t t); }
package org.dreamtech.edu.service.base; import org.dreamtech.edu.mapper.usermapper; import org.springframework.beans.factory.annotation.autowired; public abstract class baseserviceimpl<t> implements ibaseservice<t> { @autowired protected usermapper usermapper; }
package org.dreamtech.edu.service; import org.dreamtech.edu.model.user; import org.dreamtech.edu.service.base.ibaseservice; public interface iuserservice extends ibaseservice<user>{ public user login(string username,string password); }
package org.dreamtech.edu.service.impl; import org.dreamtech.edu.model.user; import org.dreamtech.edu.service.iuserservice; import org.dreamtech.edu.service.base.baseserviceimpl; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; @service @transactional public class userserviceimpl extends baseserviceimpl<user> implements iuserservice{ @override public user login(string username, string password) { return null; } @override public user findbyid(integer id) { return usermapper.findbyid(id); } @override public user findbyuuid(string uuid) { return null; } @override public void deletebyid(integer id) { } @override public void deletebyuuid(string uuid) { } @override public void update(user user) { } @override public void insert(user user) { } }
这么多行的代码,其实只实现了一个功能,就是根据id查找用户
继续配置web层:
添加service依赖:
<dependency> <artifactid>edu-service</artifactid> <groupid>org.dreamtech.edu</groupid> <version>1.0-snapshot</version> </dependency>
导入需要的其他依赖:
<!--mybatis--> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>${mybatis-spring.version}</version> </dependency> <!--数据库驱动--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>${mysql.version}</version> </dependency> <!--数据连接池--> <dependency> <groupid>com.mchange</groupid> <artifactid>c3p0</artifactid> <version>${c3p0.version}</version> </dependency> <!--其他--> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>${jstl.version}</version> </dependency> <dependency> <groupid>taglibs</groupid> <artifactid>standard</artifactid> <version>${taglibs.version}</version> </dependency> <!--日志--> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>${log4j.version}</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>${slf4j.version}</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>${slf4j.version}</version> </dependency> <!--导入servlet-api/jsp--> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>javax.servlet.jsp-api</artifactid> <version>2.3.1</version> <scope>provided</scope> </dependency>
数据库建一张简单的表,在后面测试:
create table `t_user` ( `id` int(32) not null, `username` varchar(32) not null, `password` varchar(32) not null, primary key (`id`) ) engine=innodb default charset=utf8;
添加其他的配置文件,放在resources文件夹
spring配置文件:applicationcontext.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: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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源 --> <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.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxpoolsize" value="30"/> <property name="minpoolsize" value="2"/> </bean> <!-- 配置sessionfactory --> <bean class="org.mybatis.spring.sqlsessionfactorybean" id="sqlsessionfactorybean"> <property name="datasource" ref="datasource"/> <!--配置别名--> <property name="typealiasespackage" value="org.dreamtech.edu.model"/> <!--配置加载映射文件 usermapper.xml--> <property name="mapperlocations" value="classpath:org/dreamtech/edu/mapper/*mapper.xml"/> </bean> <!-- 自动生成dao,mapper--> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="org.dreamtech.edu.mapper"/> <property name="sqlsessionfactorybeanname" value="sqlsessionfactorybean"/> </bean> <!--自动扫描service--> <context:component-scan base-package="org.dreamtech.edu"/> <!-- 配置事务--> <!-- 5.配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> <!-- 6.开启事务注解--> <tx:annotation-driven/> </beans>
properties:
jdbc.driver=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/edu jdbc.username=root jdbc.password=xuyiqing
# global logging configuration log4j.rootlogger=debug, stdout # console output... log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%5p [%t] - %m%n
web.xml加入spring配置:
<!-- 配置spring--> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>
usercontroller:
package org.dreamtech.edu.web.controller; import org.dreamtech.edu.model.user; import org.dreamtech.edu.service.iuserservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller @requestmapping("user") public class usercontroller { @autowired private iuserservice userservice; @requestmapping("login") public string login(){ system.out.println("hello"); return null; } @requestmapping("find") public string find(integer id){ user user = userservice.findbyid(id); return null; } }
现在就可以启动,访问/user/login.do是没问题的
但是如果访问/user/find会报错
这是因为在dao层:usermapper.xml在打包的时候不存在
解决:在dao层的pom.xml的build下面配置:
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
运行!
http://localhost:8080/user/find.do?id=1
不报错,然后我们在数据库随便添加一条信息,在controller层输出
到这里,其实搭建就结束了!
但是还有一步:
页面整合:
页面应该放在哪里呢?
放在web-inf目录中:
js、css、图片等静态资源放在web-inf同级下,页面放在web-inf中
可以新建一个目录,放入页面,再把文件夹放到web-inf
其实之前springmvc就是这么配的
<bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/modules/"/> <property name="suffix" value=".jsp"/> </bean>
在controller中只需要返回jsp的名称字符串就可以了:
@requestmapping("login") public string login(){ system.out.println("hello"); return "default"; }
上一篇: C#工具:CSV文件转换帮助类
下一篇: java中简单内存计算