第一步:创建maven项目并完善项目结构
第二步:相关配置
pom.xml
引入相关jar包
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <maven.compiler.source>1.7</maven.compiler.source> 4 <maven.compiler.target>1.7</maven.compiler.target> 5 <spring.version>5.0.2.RELEASE</spring.version> 6 <slf4j.version>1.6.6</slf4j.version> 7 <log4j.version>1.2.12</log4j.version> 8 <mysql.version>5.1.6</mysql.version> 9 <mybatis.version>3.4.5</mybatis.version> 10 </properties> 11 12 <dependencies> 13 <!-- spring --> 14 <dependency> 15 <groupId>org.aspectj</groupId> 16 <artifactId>aspectjweaver</artifactId> 17 <version>1.6.8</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-aop</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-web</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-webmvc</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-test</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-tx</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-jdbc</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>junit</groupId> 56 <artifactId>junit</artifactId> 57 <version>4.12</version><scope>compile</scope> 58 </dependency> 59 <dependency> 60 <groupId>mysql</groupId> 61 <artifactId>mysql-connector-java</artifactId> 62 <version>${mysql.version}</version> 63 </dependency> 64 <dependency> 65 <groupId>javax.servlet</groupId> 66 <artifactId>servlet-api</artifactId> 67 <version>2.5</version> 68 <scope>provided</scope> 69 </dependency> 70 <dependency> 71 <groupId>javax.servlet.jsp</groupId> 72 <artifactId>jsp-api</artifactId> 73 <version>2.0</version> 74 <scope>provided</scope> 75 </dependency> 76 <dependency> 77 <groupId>jstl</groupId> 78 <artifactId>jstl</artifactId> 79 <version>1.2</version> 80 </dependency> 81 <!-- log start --> 82 <dependency> 83 <groupId>log4j</groupId> 84 <artifactId>log4j</artifactId> 85 <version>${log4j.version}</version> 86 </dependency> 87 <dependency> 88 <groupId>org.slf4j</groupId> 89 <artifactId>slf4j-api</artifactId> 90 <version>${slf4j.version}</version> 91 </dependency> 92 <dependency> 93 <groupId>org.slf4j</groupId> 94 <artifactId>slf4j-log4j12</artifactId> 95 <version>${slf4j.version}</version> 96 </dependency> 97 <!-- log end --> 98 <dependency> 99 <groupId>org.mybatis</groupId> 100 <artifactId>mybatis</artifactId> 101 <version>${mybatis.version}</version> 102 </dependency><dependency> 103 <groupId>org.mybatis</groupId> 104 <artifactId>mybatis-spring</artifactId> 105 <version>1.3.0</version> 106 </dependency> 107 <!--这里选择哪种链接池自己决定 我是用的durid--> 108 <dependency> 109 <groupId>com.alibaba</groupId> 110 <artifactId>druid</artifactId> 111 <version>1.0.9</version> 112 </dependency> 113 </dependencies> 114 配置编译java路径下的xml文件(当mybatis采用mapper.xml形式编写sql语句的时候,放在dao层下的xml文件maven默认是不编译的) 115 在build标签里加入以下代码 116 117 <resources> 118 <!--编译src/main/java目录下的xml文件--> 119 <resource> 120 <directory>src/main/java</directory> 121 <includes> 122 <include>**/*.xml</include> 123 </includes> 124 <filtering>true</filtering> 125 </resource> 126 </resources>
到此为止pom.xml就配置好了
-----------------------------------------------------------------------------------------------------------------------------------------
接下来我们配置mybatis的配置文件
SqlMapConfig.xml(因为我们后面要整合Spring所以很多配置都放在了Spring配置文件里面 这里我们只配置一下别名方便以后mapper.xml的编写)
<?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>
<!--配置别名-->
<typeAliases>
<!--domain包路径>
<package name="cn.jydm.domain"></package>
</typeAliases>
</configuration>
jdbc.properties(这里主要填写一下数据库的相关数据 我只填写了必要的数据 链接限制等自己根据情况填写)
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8
user=root (这里说明一下之所以没有填username 是因为在Spring配置文件里面编写${username} 会获取到主机名字 导致连接数据库失败)
password=密码
到这数据库的相关东西都编写好了
-------------------------------------------------------------------------------------------------------------------------------------------
SpringMVC的配置
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包 -->
<context:component-scan base-package="cn.jydm.contraller" />
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
---------------------------------------------------------------------------------------------------------------------------------------
接下来我们配置一下spring的配置文件(因为无论是SpringMVC还是Mybatis都是与Spring整合所以我们最后配置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.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">
<!--引入jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="cn.jydm" >
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--spring整合mybatis框架-->
<!-- 配置Durid的连接池对象 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="driverClassName" value="${driverClass}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean>
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--指定mapper的配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!--指定mapper文件的存放路径-->
<property name="mapperLocations" value="classpath:cn/jydm/dao/*.xml"/>
</bean>
<!-- 配置扫描dao的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.jydm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
这样所有的配置文件就都写好了SSM框架也就整合完毕了
最后还有一个日志文件
log4j.properties
# 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
在整合过程中出现了tomcat启动失败的问题,后来发现是配置别名的时候包路径没有写对 导致mapper.xml里的返回值类型出现了找不到的问题。
SSM框架整合(Spring+SpringMVC+Mybatis)
原文:https://www.cnblogs.com/nmybk/p/14741457.html