第一步: 创建maven项目

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml

   输入项目名和工程id

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_02

   选择maven

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_spring_03

   默认就可以了

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_04

  刚开始时间比较长,可以看到,用了有7分多钟, 还是很快的

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_spring_05

  maven包下载好后,项目结构如下

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_06

 

第二步: 创建java包

  可以看到, 缺少java包,在main上右键新建包

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_07

  建完java后的目录

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_08

 

第三步:创建层级结构

  到这里先不要修改java包的状态, 接着创建其他目录结构,这么做的主要目的是方便创建分级结构。

  当改变java包的状态后,service.impl会变成一个包的名字,这时候需要Rename以下这个包,先变成service,然后创建PersonService.java, 在创建impl包, 就好了

  如果觉得麻烦可以在创建目录的时候先不创建impl包

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_09

  然后修改java包的状态,右键java包

 

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_10

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_11

 

第四步:创建需要的文件

  接下来创建一系列的文件, 然后在进行配置,先把骨架搭起来

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_12

  以上就是所有用到的文件,现在开始一个一个文件填充, 现在的目的是搭建起来能运次的项目,如果想知道配置原理请自行查阅资料

 

第五步:填充文件

   被标称黄色的部分是需要注意的, 如果更改了需要做相应的改动, 如果项目目录跟这个一样,就不需要改动


PersonController.java

package com.ssm.demo.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssm.demo.entity.Person;
import com.ssm.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
</span><span style="color:#0000ff;">private</span><span style="color:#000000;"> PersonService personService;
@RequestMapping(</span>"/selectPerson"<span style="color:#000000;">)
</span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> selectPerson(HttpServletRequest request, HttpServletResponse response) <span style="color:#0000ff;">throws</span><span style="color:#000000;"> IOException {

request.setCharacterEncoding(</span>"utf-8"<span style="color:#000000;">);
response.setCharacterEncoding(</span>"utf-8"<span style="color:#000000;">);

</span><span style="color:#0000ff;">long</span> personId = Long.parseLong(request.getParameter("id"<span style="color:#000000;">));
Person person </span>=<span style="color:#000000;">personService.findPersonById(personId);

ObjectMapper mapper </span>= <span style="color:#0000ff;">new</span><span style="color:#000000;"> ObjectMapper();

response.getWriter().write(mapper.writeValueAsString(person));
response.getWriter().close();
}

}

PersonMapperDao.java

package com.ssm.demo.dao;
import com.ssm.demo.entity.Person;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonMapperDao {
Person findPersonById(long id);
}
Person.java

package com.ssm.demo.entity;

public class Person {
private long id;
private String name;
private String email;
private int status;
//getter setter

@Override
public String toString(){
return this.getId()+"—"+this.getName()+"—"+
this.getEmail()+"—"+this.getStatus();
}
}
PersonService.java

package com.ssm.demo.service;
import com.ssm.demo.entity.Person;

public interface PersonService {
Person findPersonById(long id);
}
PersonServiceImpl.java

package com.ssm.demo.service.impl;
import com.ssm.demo.dao.PersonMapperDao;
import com.ssm.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapperDao personMapperDao;
public Person findPersonById(long id) {
return personMapperDao.findPersonById(id);
}
}spring-mvc.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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 扫描web相关的bean </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">context:component-scan </span><span style="color:#ff0000;">base-package</span><span style="color:#0000ff;">="<span style="color:#ffff00;">com.ssm.demo</span>"</span><span style="color:#0000ff;">/></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 开启SpringMVC注解模式 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">mvc:annotation-driven</span><span style="color:#0000ff;">/></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 静态资源默认servlet配置 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">mvc:default-servlet-handler</span><span style="color:#0000ff;">/></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置jsp 显示ViewResolver </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">bean </span><span style="color:#ff0000;">class</span><span style="color:#0000ff;">="org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="color:#0000ff;">></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="viewClass"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="org.springframework.web.servlet.view.JstlView"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="prefix"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="/WEB-INF/views/"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="suffix"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">=".html"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"></</span><span style="color:#800000;">bean</span><span style="color:#0000ff;">></span>

</beans>
spring-mybatis.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: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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.ssm.demo"/>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置数据库相关参数properties的属性:${url} </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">context:property-placeholder </span><span style="color:#ff0000;">location</span><span style="color:#0000ff;">="<span style="color:#ffff00;">classpath:jdbc.properties</span>"</span><span style="color:#0000ff;">/></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 数据库连接池 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">bean </span><span style="color:#ff0000;">id</span><span style="color:#0000ff;">="dataSource"</span><span style="color:#ff0000;"> class</span><span style="color:#0000ff;">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><span style="color:#0000ff;">></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="driverClass"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${jdbc.driver}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="jdbcUrl"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${jdbc.url}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="user"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${jdbc.username}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="password"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${jdbc.password}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="maxPoolSize"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${c3p0.maxPoolSize}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="minPoolSize"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${c3p0.minPoolSize}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="autoCommitOnClose"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${c3p0.autoCommitOnClose}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="checkoutTimeout"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${c3p0.checkoutTimeout}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="acquireRetryAttempts"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="${c3p0.acquireRetryAttempts}"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"></</span><span style="color:#800000;">bean</span><span style="color:#0000ff;">></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置SqlSessionFactory对象 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">bean </span><span style="color:#ff0000;">id</span><span style="color:#0000ff;">="sqlSessionFactory"</span><span style="color:#ff0000;"> class</span><span style="color:#0000ff;">="org.mybatis.spring.SqlSessionFactoryBean"</span><span style="color:#0000ff;">></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 注入数据库连接池 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="dataSource"</span><span style="color:#ff0000;"> ref</span><span style="color:#0000ff;">="dataSource"</span><span style="color:#0000ff;">/></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 扫描model包 使用别名 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="typeAliasesPackage"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="<span style="color:#ffff00;">com.ssm.demo.entity</span>"</span><span style="color:#0000ff;">/></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 扫描sql配置文件:mapper需要的xml文件 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="mapperLocations"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="<span style="color:#ffff00;">classpath:mapper/*.xml</span>"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"></</span><span style="color:#800000;">bean</span><span style="color:#0000ff;">></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">bean </span><span style="color:#ff0000;">class</span><span style="color:#0000ff;">="org.mybatis.spring.mapper.MapperScannerConfigurer"</span><span style="color:#0000ff;">></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 注入sqlSessionFactory </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="sqlSessionFactoryBeanName"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="sqlSessionFactory"</span><span style="color:#0000ff;">/></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 给出需要扫描Dao接口包 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="basePackage"</span><span style="color:#ff0000;"> value</span><span style="color:#0000ff;">="<span style="color:#ffff00;">com.ssm.demo.dao</span>"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"></</span><span style="color:#800000;">bean</span><span style="color:#0000ff;">></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置事务管理器 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">bean </span><span style="color:#ff0000;">id</span><span style="color:#0000ff;">="transactionManager"</span><span style="color:#ff0000;"> class</span><span style="color:#0000ff;">="org.springframework.jdbc.datasource.DataSourceTransactionManager"</span><span style="color:#0000ff;">></span>
<span style="color:#008000;"><!--</span><span style="color:#008000;"> 注入数据库连接池 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">property </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="dataSource"</span><span style="color:#ff0000;"> ref</span><span style="color:#0000ff;">="dataSource"</span><span style="color:#0000ff;">/></span>
<span style="color:#0000ff;"></</span><span style="color:#800000;">bean</span><span style="color:#0000ff;">></span>

<span style="color:#008000;"><!--</span><span style="color:#008000;"> 配置基于注解的声明式事务 </span><span style="color:#008000;">--></span>
<span style="color:#0000ff;"><</span><span style="color:#800000;">tx:annotation-driven </span><span style="color:#ff0000;">transaction-manager</span><span style="color:#0000ff;">="transactionManager"</span><span style="color:#0000ff;">/></span>
</beans>

PersonMapper.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">
<!– 设置为IUserDao接口方法提供sql语句配置 –>
<mapper namespace=“com.ssm.demo.dao.PersonMapperDao”>
<span style="color:#0000ff;"><</span><span style="color:#800000;">select </span><span style="color:#ff0000;">id</span><span style="color:#0000ff;">="findPersonById"</span><span style="color:#ff0000;"> resultType</span><span style="color:#0000ff;">="Person"</span><span style="color:#ff0000;"> parameterType</span><span style="color:#0000ff;">="long"</span><span style="color:#0000ff;">></span><span style="color:#000000;">
SELECT * FROM person WHERE id = #{id}
</span><span style="color:#0000ff;"></</span><span style="color:#800000;">select</span><span style="color:#0000ff;">></span>
</mapper>
jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=数据库账号
#密码
jdbc.password=数据库密码
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2


web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!– 编码过滤器 –>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><!– 配置DispatcherServlet –>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!– 配置springMVC需要加载的配置文件–>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!– 匹配所有请求,此处也可以配置成 *.do 形式 –>
<url-pattern>/</url-pattern>
</servlet-mapping><welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list></web-app>

 

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<script>
function selectUser() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("test").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "person/selectPerson", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("id=1");
}
</script>
<body>
<p id="test">Hello World!</p>
<button type="button" onclick="selectUser()">onclick test</button>
</body>
</html>



第六步: 配置tomcat

   先clean一下, 然后package一下

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_13

  打包成功后,控制台显示

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_spring_14

   会多一个包,和一个war文件

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_15

  配置tomcat


 


  选择deployment, 点击右边加号

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_bc_16

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_17

     输入请求的跟地址, 项目名


  还有一种是点击右边加号,可以添加artifact的, 没弄明白怎么整的, 求链接. 第七步: 数据库创建, 表的建立, 插入数据


navicat 图形化界面创建数据库,然后修改jdbc.properties文件的数据库名称为自己创建的数据库,然后创建表

CREATE TABLE person (
id int(11) NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
name varchar(255) NOT NULL,
status int(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;INSERT INTO person VALUES (‘1’, ‘123456qq.com’, ‘zzz’, ‘0’);
SET FOREIGN_KEY_CHECKS=1;

  

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_18

 

第八步: 启动tomcat, 弹出页面点击按钮, 会出现查询内容

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_19

IntelliJ IDEA 创建Spring+SpringMVC+mybatis+maven项目_xml_20

 

以上是idea创建maven管理的ssm项目搭建, 本文作为学习经验,方便以后查看, 如有错误请指出,万望包含