idea搭建ssm项目
搭建环境:
- jdk 1.8
- maven 3.6.1
- Tomcat 9.0
- idea 2019
- mysql 5.7.19
一、创建maven项目
二、项目目录结构
2.1 代码目录结构
创建完工程后,在src-main-java下构建com包,并且在com下创建,Controller,dao,service,pojo,包用于存储代码
2.2配置文件目录结构
在resources下创建 mapper(用于存储mybatis配置文件),spring(用来存放spring配置文件)两个子目录
2.3 web目录
在src-main下创建一个webapp目录,并且在webapp目录下构建一个叫WEB-INF的子目录,并且建立一个web.xml的文件
<?xml version="1.0" encoding="UTF-8" ?>
<!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://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"
version="3.0"
metadata-complete="true">
三、搭建SSM框架
3.1 maven依赖
在pom.xml文件中导入如下依赖:
3.1.1 spring-MVC
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
3.1.2 mybatis和驱动类
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
3.1.3 事务和整合依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
3.1.4 mysql及数据库连接池
<!-- 连接池依赖 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
3.1.4 JSON
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>0.9</version>
</dependency>
3.1.5 构建插件
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
注意:构建如果在工程中所有的配置文件都放在resources下,一般可以不加构建插件,但是为了保证工程打包内容完整,建议加上构建插件以保证内容完整。
3.2 数据库信息
3.2.1 Mysql数据库中创建名字为ssm的数据库
create database ssm
3.2.2 创建user表
CREATE TABLE user(
id int(10) NOT NULL auto_increment,
name varchar(50) default NULL,
pwd varchar(50) default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
3.2.3 向user表添加数据
INSERT INTO user (id, name, pwd) VALUES
(1, '张三', '123456'),
(2, '李四', '123456'),
(3, '王五', '123123');
3.3 创建类和HTML文件
3.3.1 pojo 类
package com.pojo;
public class user {
private Integer id;
private String name;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
3.3.2 dao 接口
package com.dao;
import com.pojo.user;
import java.util.List;
public interface userDao {
public List<user> queryAll();
}
3.3.3 service 接口
package com.service;
import com.pojo.user;
import java.util.List;
public interface userservice {
List<user> queryAll();
}
3.3.4 serviceimpl 类
package com.service.serviceimpl;
import com.dao.userDao;
import com.pojo.user;
import com.service.userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class userserviceImpl implements userservice {
@Autowired
userDao dao;
public List<user> queryAll() {
return dao.queryAll();
}
}
3.3.5 Controller 类
package com.controller;
import com.pojo.user;
import com.service.serviceimpl.userserviceImpl;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class userController {
List<user> list=new ArrayList<user>();
@Autowired
userserviceImpl service;
@ResponseBody
@RequestMapping(value = "queryAll.do",produces = "text/html;charset=utf-8")
public String queryAll(){
list=service.queryAll();
JSONArray jsonArray=JSONArray.fromObject(list);
return jsonArray.toString();
}
}
3.3.6 login.html 页面
因为页面中要使用JQuery,所以我们要引入jquery.js
下面是login.html 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>pwd</th>
</tr>
<tbody id="trs"></tbody>
</table>
</body>
<script src="js/jquery.js" type="text/javascript"></script>
<script>
$(function () {
login();
});
function login() {
$.ajax({
type:"post",
url:"queryAll.do",
dataType:"json",
success:function (r) {
console.log(r);
var trs="";
$.each(r,function(i,v){
trs=trs+"<tr><td>"+v.id+"</td><td>"+v.name+"</td><td>"+v.pwd+"</td></tr>";
});
$("#trs").append(trs);
}
});
}
</script>
</html>
3.4 配置工程支持WEB开发
(1)在File选项卡中找到Project Structure选项进入
(2)在Project Settings里找到Modules,点击左上方"+",找到WEB模块,点击确定加入WEB模块
(3)点击添加的WEB模块位置,将Deployment Descriptors里面的path修改成刚才我们建立的web.xml文件的位置
(4)在Web Resource Directories里面的路径修改成我们的webapp路径
(5)点击Artfacts选项,点击“+”,选择Web Application:Exploaded,选择from modules,找到我们刚才建立的web工程点击确定,这样才能让我们可以在WEBAPP目录下建立JSP文件,并且tomcat服务器找到该WEB项目进行部署和运行
3.5 配置文件
3.5.1 spring 配置文件
(1)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-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!--使用注解驱动-->
<mvc:annotation-driven/>
<context:component-scan base-package="com"/>
</beans>
(2)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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--引入properties文件-->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<!--连接数据库的url -->
<property name="url" value="${url}" />
<!--连接数据库的用户名 -->
<property name="username" value="${username}" />
<!--连接数据库的密码 -->
<property name="password" value="${password}" />
<!--最大连接数 -->
<property name="maxActive" value="${jdbc.maxTotal}" />
<!--最大空闲连接 -->
<property name="maxIdle" value="${maxIdle}" />
<!--初始化连接数 -->
<property name="initialSize" value="${initialSize}" />
</bean>
<!--配置sqlsession工厂-->
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置DAO所在spring会自动查找下面的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3.5.2 web.xml文件
在web.xml文件中加入
<!--请求和应答字符编码过滤器-->
<filter>
<filter-name>encoding-filter</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>encoding-filter</filter-name>
<url-pattern>/* </url-pattern>
<servlet-name>springDispatcherServlet</servlet-name>
</filter-mapping>
<!--启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml</param-value>
</context-param>
<!-- 用前端控制器初始化springmvc容器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.5.3 jdbc.properties文件
在resources下直接建立jdbc.properties文件,存储JDBC配置信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8
#账户
username=root
#密码
password=x5
jdbc.maxTotal=30
maxIdle=10
initialSize=5
3.5.4 mapper映射文件
在resources下的mapper文件夹中建立一个叫emp.xml的映射文件,用于存储mybatis映射配置信息
<?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.dao.userDao">
<select id="queryAll" resultType="com.pojo.user">
select * from user
</select>
</mapper>
四、部署和运行
4.1 部署
4.1.1 Tomcat服务器配置
4.1.2 项目部署
(1)找到运行窗口,选择add configurations,点击左上角“+”,选择tomcat服务器,选择local。在弹出窗口的application server 位置找到配置好的Tomcat服务器
URL:代表访问地址默认为 [HTTP://本机IP:8080]
JRE:为默认JDK版本
VM options:为Tomcat服务器运行的JVM参数
(2)选择deployment选项卡,点击右侧加号选择artifact,刚才建立的好的工程就会加入到Tomcat服务器下,点击apply完成部署
注意:下方的Application Context位置代表访问工程的默认路径,可以只保留“/”,后面的内容都删除,方便访问,只需要输入http://IP:8080即可访问工程,无需如eclipse一样去输入工程名
3.2 访问运行
(1)启动Tomcat服务器
(2)默认打开浏览器以配置好的地址直接进行访问