java整合SSM框架的图文教程
使用myeclipse搭建maven项目
准备工作
安装maven
官网下载安装()
配置环境变量
配置完后,使用命令行输入mvn -version查看是否配置成功,出现以下界面表示配置成功。
2. 在myeclipse中配置maven
打开myeclipse2015————点击菜单栏window———选项栏preference ,搜索maven,进入installations,点击add引入安装的maven。
进入user setting,设置global setting,选中安装的maven的settings.xml配置文件(以上步骤已经完成maven配置)
3. 创建项目(检测是否配置成功,这里创建的是web项目)
点击项目右键->run as->maven install将依赖install至本地maven库,有可能会报:
-dmaven.multimoduleprojectdirectorysystem propery is not set. check $m2_home environment variable and mvn scriptmatch.,
表示jdk版本和maven有冲突,更改一下jdk版本,并且加上-dmaven.multimoduleprojectdirectory=$m2_home即可;
如运行过程中控制台如下图所示,表示成功。
在maven项目中搭建spring+springmvc+mybatis
修改pom.xml添加对应的包依赖
<!-- servlet --> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.0.1</version> <scope>compile</scope> </dependency> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> <version>0.9.1</version> </dependency> <!-- 阿里巴巴druid连接池 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.0.25</version> <exclusions> <exclusion> <groupid>com.alibaba</groupid> <artifactid>jconsole</artifactid> </exclusion> <exclusion> <groupid>com.alibaba</groupid> <artifactid>tools</artifactid> </exclusion> </exclusions> </dependency> <!-- 1、先添加spring相关的包 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>4.1.7.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>4.1.7.release</version> </dependency> <!--aspectjweaver包:面向切面要用到的包 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>1.8.5</version> </dependency> <!--4、单元测试相关的包 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> </dependency> <!-- 2、mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.1</version> </dependency> <!-- 日志 --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <!-- 整合插件包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.0</version> </dependency> <!-- mysql驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.38</version> </dependency> <dependency> <groupid>com.mchange</groupid> <artifactid>mchange-commons-java</artifactid> <version>0.2.10</version> </dependency> <!-- 工具包 --> <dependency> <groupid>commons-codec</groupid> <artifactid>commons-codec</artifactid> <version>1.10</version> </dependency> <!-- xml文件解析 --> <dependency> <groupid>dom4j</groupid> <artifactid>dom4j</artifactid> <version>1.6.1</version> </dependency> <dependency> <groupid>com.thoughtworks.xstream</groupid> <artifactid>xstream</artifactid> <version>1.3.1</version> </dependency> <!-- json-lib --> <dependency> <groupid>net.sf.json-lib</groupid> <artifactid>json-lib</artifactid> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupid>xom</groupid> <artifactid>xom</artifactid> <version>1.2.5</version> </dependency> <!-- httpclient --> <dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.2.5</version> </dependency> <!-- 分页组件 --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>4.0.0</version> </dependency> <!-- 通用mapper --> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version>3.3.8</version> </dependency> <dependency> <groupid>commons-httpclient</groupid> <artifactid>commons-httpclient</artifactid> <version>3.1</version> </dependency> <dependency> <groupid>org.jdom</groupid> <artifactid>jdom</artifactid> <version>1.1.3</version> </dependency>
修改web.xml,配置驱动及过滤器和加载spring配置文件
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 加入spring --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext*.xml</param-value> </context-param> <!-- 配置dispatchcerservlet --> <servlet> <servlet-name>springdispatcherservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- 配置spring mvc下的配置文件的位置和名称 --> <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>springdispatcherservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
创建并配置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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 扫描 --> <context:component-scan base-package="cn.ft.mapper,cn.ft.service.impl,cn.ft.service.rest.impl"></context:component-scan> <!-- 导入db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 先配置数据源:c3p0 --> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> <property name="driverclass" value="${driverclassname}"></property> <property name="jdbcurl" value="${jdbc_url}"></property> <property name="user" value="${jdbc_username}"></property> <property name="password" value="${jdbc_password}"></property> <property name="testconnectiononcheckin" value="false"/> <property name="testconnectiononcheckout" value="true"/> </bean> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource"></property> <!-- 加载mybatis.cfg.xml文件 --> <property name="configlocation" value="classpath:mybatis-config.xml"></property> <!-- 自动扫描需要定义类别名的包,将包内的java类的类名作为类别名 --> <property name="typealiasespackage" value="cn.ft.model"></property> </bean> <!-- 自动扫描所有的mapper接口与文件 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="cn.ft.mapper"></property> </bean> <!-- 事务的配置 --> <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionadvice" transaction-manager="txmanager"> <tx:attributes> <!-- 以如下关键字开头的方法使用事物 --> <tx:method name="add*" 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="buyfood" propagation="required" /> <tx:method name="order" propagation="required" /> <!-- 以如下关键字开头的方法不使用事物 --> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="query*" read-only="true" /> <!-- 其他方法不使用事物 --> <tx:method name="*" propagation="supports" /> </tx:attributes> </tx:advice> <!-- 切面,将事物用在哪些对象上 --> <aop:config> <aop:pointcut id="transactionpointcut" expression="execution(* cn.ft.service.impl.*impl.*(..))" /> <aop:advisor pointcut-ref="transactionpointcut" advice-ref="transactionadvice" /> </aop:config> </beans>
创建并配置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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 扫描 --> <context:component-scan base-package="cn.ft.controller"></context:component-scan> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean class="org.springframework.web.servlet.handler.simplemappingexceptionresolver"> <property name="exceptionmappings"> <props> <prop key="org.apache.shiro.authz.unauthorizedexception"> /refuse </prop> </props> </property> </bean> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.stringhttpmessageconverter"> <property name="supportedmediatypes"> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- ajax中文乱码暂时无效<bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <bean class="org.springframework.http.converter.stringhttpmessageconverter"> <property name="supportedmediatypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> </list> </property> </bean> --> </beans>
配置数据源db.properties
driverclassname=com.mysql.jdbc.driver validationquery=select 1 jdbc_url=jdbc\:mysql\://localhost\:3306/ft?useunicode\=true&characterencoding\=utf-8&zerodatetimebehavior\=converttonull jdbc_username=root jdbc_password=8888
配置mybatis-config.xm
<?xml version="1.0" encoding="utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheenabled" value="true"/> </settings> <typealiases> <!-- <typealias type="cn.hd.model.userbean" alias="userbean"/> --> <!-- 批量设置别名:别名就是类名(把第一个字母改小写) --> <package name="cn.ft.model"/> </typealiases> </configuration>
配置log4j.properties
log4j.rootcategory=debug, stdout , r log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=[qc] %p [%t] %c.%m(%l) | %m%n log4j.appender.r=org.apache.log4j.dailyrollingfileappender log4j.appender.r.file=d\:\\tomcat 5.5\\logs\\qc.log log4j.appender.r.layout=org.apache.log4j.patternlayout log4j.appender.r.layout.conversionpattern=%d-[ts] %p %t %c - %m%n
以上这篇java整合ssm框架的图文教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。