或许:懂一丢丢java可能都能根据这个步骤搭出来ssm-demo
一、环境
intellij+spring+springmvc+mybatis
intellij:2017.2
spring:4.2.5.RELEASE
mybatis:3.4.5
mysql、以及数据库管理工具
二、搭建环境
前置条件
1、首先java环境是配置好的,我是jdk1.8
2、maven也下载好了。我的maven是3.5.0(如果没有配置环境变量或下载,某度经验前几个都是,按照百度来就没问题。配好M2_HOME,M2等变量)
3、tomcat也配置好了。我的tomcat是8.5.20(同上,如果没有配置环境变量或下载,搜一下就好。按照经验配置来。基本没差。)
正式开始步骤。
1、file->new->project
2、勾选对勾,然后选择maven-archetype-webapp。sdk选择自己电脑的环境,我的是1.8。然后next
3、这个按照自己的来就好了,自己命名的。然后next
4、第1布,选择自己下载好的maven路径。第2打上对勾,可以使用自己配置好的setting,这个目录下没有的话,在下载的maven的conf下应该有(配置主要是改了下阿里的源,下载可能会快)。
5、选择项目名?设置路径。这个也是按照自己的想法来就好了。然后finish。
6、建立好项目之后,菜单结构如下。
右键main菜单,new->Directory.。
7、命名为java。然后右键java文件夹。Mark Directory as->Sources Root.
8、这是最后的目录结构,按照结构创立对应文件、文件夹。(如果包属性不对,可以先配置pom.xml 并且保存改动后,再按照这一步进行目录结构设置)
9、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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssm</groupId>
<artifactId>com.ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>com.ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<mybatis.version>3.4.5</mybatis.version>
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!--表示开发的时候引入,发布的时候不会加载此包-->
<scope>test</scope>
</dependency>
<!--spring 核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis/spring包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--导入java ee jar包-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--JSTL标签类-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<!-- log start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</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>
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>com.ssm</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
10、我是使用navicat操作数据库的。建立表,字段,然后随意填充几条数据。
11、applicationContext.xml 配置。基本copy下来就可以
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ssm.service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<!--此处ssm为你的数据库的名,如上一步图中所示。-->
<property name="url">
<value>jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<!--此处value内改为你的数据库密码。-->
<property name="password">
<value>123456</value>
</property>
</bean>
<!--主要改动下面几个路径。如果按照我的包名来生成的,估计都不用改。-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSession" />
</bean>
</beans>
12、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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.ssm.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
13、web.xml配置。copy就好,应该不用改动。
<?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">
<!-- 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>
<!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</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>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
14、配置tomcat。1、点这个edit。2、点加号,如图tom2来。3、看图中红框内容,挨着来。4、选择任意一个都可以应该,我选的第二个,ok。5、然后点这个跑起来服务。
如果你报错了,就按照错误日志去排查。
15、上一个跑起来的图。
16、现在上各个类的详细信息。
listPerson.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
PersonService :
public interface PersonService {
List<Person> list();
}
PersonServiceImpl :
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
PersonMapper personMapper;
public List<Person> list() {
return personMapper.list();
}
}
Person :
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PersonMapper :
public interface PersonMapper {
public void add(Person Person);
public void delete(int id);
public Person get(int id);
public void update(Person Person);
public List<Person> list();
public int count();
}
Person.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="com.ssm.mapper.PersonMapper">
<select id="list" resultType="com.ssm.pojo.Person">
select * from person
</select>
</mapper>
PersonController :
@Controller
@RequestMapping("")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("listPerson")
public ModelAndView listCategory(){
ModelAndView mav = new ModelAndView();
List<Person> cs= personService.list();
// 放入转发参数
mav.addObject("cs", cs);
// 放入jsp路径
mav.setViewName("listPerson");
return mav;
}
}
以上为此demo全部代码。github地址放在最后。如有需要请自取。
三、跳坑
搭建过程中一直没有跑成功。
1、报错:Exception:Request processing failed; nested exception is org.apache.ibatis.binding.BindingException
从异常信息上看,是找不到mapper对应的xml文件,于是我到target目录去查找,是否存在mapper对应的xml文件,发现xml 文件并不在target目录下
解决办法:在pom.xml 下 build节点内添加 如"pom.xml" 文件示例所示
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
感谢:http://blog.csdn.net/evan_leung/article/details/50670780
2、报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
感谢:http://www.cnblogs.com/beppezhang/p/6118661.html
四、结尾
ssm框架学习:早先时候看过一点spring,一点springmvc。最近刚好想看看ssm。学习下,就看了mybatis之后,直接尝试进行一个demo项目测试。此项目是根据
how2java网站学习的:http://how2j.cn/
在此特别感谢。不过how2j上都是eclipse版本的。也不是maven的。没有idea的。直接把代码copy下来也跑不来。
所以看着那个教程代码,又根据一些前辈们的文章,搭出来这个demo了。
ps:初次写文,如有侵权请通知,会尽快删除相关内容。
ps2:github: https://github.com/hanswook/ssm-demo
ps3:如果该文章对您有所帮助,希望您可以点下喜欢~给作者一点鼓励!