写这篇文章的目的是为了新手学习如何快速搭建ssm框架,用于刚进入公司快速搭建和平时的学习。
1 创建一个ssm的工程
2 导入pom坐标:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jijia</groupId>
<artifactId>ssm_test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>ssm_common</module>
<module>ssm_dao</module>
<module>ssm_domain</module>
<module>ssm_system_service</module>
<module>ssm_mannge_web</module>
</modules>
<!-- 集中定义依赖版本号 -->
<properties>
<!--测试包--><junit.version>4.12</junit.version>
<!--spring的包--><spring.version>5.0.2.RELEASE</spring.version>
<!--spring分页包--><pagehelper.version>5.1.2</pagehelper.version>
<mybatis.version>3.4.5</mybatis.version>
<!--mybatis的包--><mybatis.spring.version>1.3.0</mybatis.spring.version>
<!--数据库的包--><mysql.version>5.1.32</mysql.version>
<!--连接池的包--><druid.version>1.0.9</druid.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-webmvc</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
3 建立各个工程的模块:
4 建立模块的关系:
依次是:web依赖service依赖dao依赖domian依赖common
5 插入测试company实体类:
package com.jijia.domain.system;
import java.io.Serializable;
import java.util.Date;
/**
* 这是一个测试实体类
* @author 马石林
* @Company http://www.马石林.com
*/
public class Company implements Serializable {
private String id;
private String name;
private Date expirationDate;
private String address;
private String licenseId;
private String representative;
private String phone;
private String companySize;
private String industry;
private String remarks;
private Integer state;
private Double balance;
private String city;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLicenseId() {
return licenseId;
}
public void setLicenseId(String licenseId) {
this.licenseId = licenseId;
}
public String getRepresentative() {
return representative;
}
public void setRepresentative(String representative) {
this.representative = representative;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCompanySize() {
return companySize;
}
public void setCompanySize(String companySize) {
this.companySize = companySize;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Company{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", expirationDate=" + expirationDate +
", address='" + address + '\'' +
", licenseId='" + licenseId + '\'' +
", representative='" + representative + '\'' +
", phone='" + phone + '\'' +
", companySize='" + companySize + '\'' +
", industry='" + industry + '\'' +
", remarks='" + remarks + '\'' +
", state=" + state +
", balance=" + balance +
", city='" + city + '\'' +
'}';
}
}
6 编写dao层的配置文件和接口:
dao的接口(CompanyDao):
package com.jijia.system.dao;
import com.jijia.domain.system.Company;
import java.util.List;
/**
* dao的测试接口
* @author 马石林
* @Company http://www.马石林.com
*/
public interface CompanyDao {
List<Company> findAll();
}
properties连接(db.properties)(自己电脑的数据库):
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas-export-ee100?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=1234
spring的整合mybatis(applicationContext-dao.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--创建容器的包扫描-->
<context:component-scan base-package="com.itheima">
<!--Controller注解不需要spring扫描,由springmvc扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!--指定连接数据库的properties文件的位置-->
<context:property-placeholder location="classpath*:properties/db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.itheima.domain"></property>
</bean>
<!--配置dao接口创建代理实现类的扫描器-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao接口所在的包-->
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
</beans>
同目录下的xml配置文件(com/jijia/system/dao/CompanyDao.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.jijia.dao.system.CompanyDao">
<!--建立实体类属性和数据库表中列名的对应关系-->
<resultMap id="BaseResultMap" type="company">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="expiration_date" property="expirationDate"></result>
<result column="address" property="address"></result>
<result column="license_id" property="licenseId"></result>
<result column="representative" property="representative"></result>
<result column="phone" property="phone"></result>
<result column="company_size" property="companySize"></result>
<result column="industry" property="industry"></result>
<result column="remarks" property="remarks"></result>
<result column="state" property="state"></result>
<result column="balance" property="balance"></result>
<result column="city" property="city"></result>
</resultMap>
<!--配置查询所有-->
<select id="findAll" resultMap="BaseResultMap">
select * from ss_company;
</select>
</mapper>
7.service整合spring配置文件(applicationContext-dao.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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配置事务的属性-->
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--配置aop 包括配置切入点表达式,事务通知和切入点表达式的关联-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution(* com.jijia.service.*.impl.*.*(..))"></aop:pointcut>
<!--建立切入点表达式和事务的关联-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
编写service和service实现类测试类:
CompanyService
package com.jijia.service;
import com.jijia.domain.system.Company;
import java.util.List;
public interface CompanyService {
/**
* 查询所有企业
* @return
*/
List<Company> findAll();
}
CompanyServiceImpl:
package com.jijia.service.impl;
import com.jijia.domain.system.Company;
import com.jijia.service.CompanyService;
import com.jijia.system.dao.CompanyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CompanyServiceImpl implements CompanyService {
@Autowired
private CompanyDao companyDao;
public List<Company> findAll() {
return companyDao.findAll();
}
}
8 编写Controller层测试类和配置文件:
spring整合springmvc(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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定springmvc创建容器时要扫描的包-->
<context:component-scan base-package="com.jijia.web"></context:component-scan>
<!--配置视图解析器-->
<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>
<!--开启注解的支持-->
<mvc:annotation-driven/>
</beans>
测试Controller:
package com.jijia.web.controller;
import com.jijia.domain.system.Company;
import com.jijia.service.system.CompanyService;
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 java.util.List;
/**
* 企业的控制器
* @author 黑马程序员
* @Company http://www.jijia.com
*/
@Controller
@RequestMapping("/company")
public class CompanyController {
@Autowired
private CompanyService companyService;
/**
* 获取企业列表
* @return
*/
@RequestMapping(value="/list",name = "查询企业列表")
public String list(HttpServletRequest request){
//1.查询企业列表
List<Company> companyList = companyService.findAll();
//2.存入请求域中
request.setAttribute("companyList",companyList);
//3.前往列表页面
return "company/company-list";
}
}
编写测试页面:
web.xml:
<?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"
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*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Shiro Security filter filter-name这个名字的值将来还会在spring中用到
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
</web-app>
index.jsp:
<%@page contentType="text/html;UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>临时主页</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/company/list.do">
<h2>企业列表</h2>
</a>
</body>
</html>
company-list.jsp:
<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>临时企业列表</title>
</head>
<body>
<table>
<tr>
<th class="">序号</th>
<th class="sorting">企业名称</th>
<th class="sorting">所在地</th>
<th class="sorting">地址</th>
<th class="sorting">企业法人</th>
<th class="sorting">联系方式</th>
<th class="sorting">所属行业</th>
<th class="sorting">状态</th>
<th class="sorting">余额</th>
<th class="text-center">操作</th>
</tr>
<!--遍历集合,显示企业列表内容-->
<c:forEach items="${companyList}" var="item" varStatus="vs">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"> ${vs.count}</td>
<td>
${item.name}
</td>
<td>${item.city}</td>
<td>${item.address}</td>
<td>${item.representative}</td>
<td>${item.phone}</td>
<td>${item.industry}</td>
<td>${item.state ==0?'未审核':'已审核'}</td>
<td class="text-center">${item.balance}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" onclick='location.href="${pageContext.request.contextPath}/company/toUpdate.do?id=${item.id}"'>编辑</button>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
测试成功。
注意事项:数据库账号和密码是对的,表名是对的,dao和dao.xml包结构相对应。