一个简单的SSM框架,实现功能
1.新建web项目
2.servlet.xml中发布项目
在Tomcat中找到conf文件夹,在里面找到servlet.xml文件,打开
在xml文件中找到此标签,将web项目中的WebContent路径复制到标签中
3.导包
3.1Spring相关包
-------context,core,beans,expression,logging
3.2测试相关包
-------test,aop
3.3servlet和jsp相关包
-------jsp,servlet,jstl,standard
3.4数据库相关包
-------mysql,dbcp,pool
3.5Mybatis相关包
3.6Spring和MyBatis相关包
包的整合(lib目录下)
4.创建数据库,设计表
5.创建资源文件夹(resources)
5.1将数据库资源文件jdbc.properties放入此文件夹
#驱动的类路径
jdbc.driver=com.mysql.jdbc.Driver
#连接字符串
jdbc.url=jdbc:mysql://localhost:3306/ssm
#用户名
jdbc.username=root
#密码
jdbc.password=123456
#连接池启动时的初始值
initialSize=1
#连接池的最大值
maxActive=50
#连接池的最大空闲数
maxIdle=20
5.2创建Spring的配置文件applicationContext.xml放入此文件夹,将数据库资源文件交给此spring的配置文件管理
// <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 将属性文件交给spring管理 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- spring管理数据库 -->
<bean id="basicDataSource" class=" org.apache.commons.dbcp.BasicDataSource">
<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>
<!-- spring管理SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源,因为获取连接之后,才能打开会话 -->
<property name="dataSource" ref="basicDataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.domain"></property>
<!-- 加载所有的mapper文件 -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
</bean>
<!-- 将Mapper接口交给Spring去管理,获取的是代理对象 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 注解扫描service层 -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
</beans>
6.创建项目结构
** domain写字段实体类
mapper,service写功能**
7.配置mapper映射文件(.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">
<!--
命名空间: 就是映射接口的全限定名
数据id的名称和接口的方法名要一致
SQL抽取:
<select id="findAll" resultType="cn.itsource.domain.Emp">
select * from emp
<include refid="Emp"></include>
</select>
<sql id="Emp">
被抽取的SQL语句
</sql>
-->
<mapper namespace="cn.itsource.mapper.EmpMapper">
<!-- 写Sql语句 -->
<select id="findAll" resultType="cn.itsource.domain.Emp">
select * from emp
</select>
<select id="findByCondition" resultType="cn.itsource.domain.Emp">
select * from emp
<where>
<if test="deptno!=null">
deptno=#{deptno}
</if>
<!-- .trim()可以去除空字符 -->
<if test="ename!=null and ''!=ename.trim()">
<!-- 模糊查询拼接字符串 -->
and ename like concat('%',trim(#{ename}),'%')
</if>
<if test="address!=null and ''!=address.trim()">
and address like concat('%',trim(#{address}),'%')
</if>
<if test="sal!=null">
<choose>
<when test="sal==3000.0">
and sal < 3000
</when>
<when test="sal==5000.0">
and sal >= 3000 and sal < 5000
</when>
<when test="sal==8000.0">
and sal >= 5000 and sal < 8000
</when>
<when test="sal==8001.0">
and sal >= 8000
</when>
</choose>
</if>
</where>
</select>
<delete id="del">
delete from emp where eid=#{eid}
</delete>
</mapper>
8.配置applicationContext文件
// <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 将属性文件交给spring管理 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- spring管理数据库 -->
<bean id="basicDataSource" class=" org.apache.commons.dbcp.BasicDataSource">
<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>
<!-- spring管理SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源,因为获取连接之后,才能打开会话 -->
<property name="dataSource" ref="basicDataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.domain"></property>
<!-- 加载所有的mapper文件 -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
</bean>
<!-- 将Mapper接口交给Spring去管理,获取的是代理对象 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 注解扫描service层 -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
</beans>
9.配置项目中的web.xml文件
// <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 服务器启动的时候加载sprin的 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置过滤器,解决中文乱码问题 -->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
10.在resources资源文件中新建spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 扫描controller层 -->
<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
<!-- 开启spring对MVC的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
11.编写Controller层
package cn.itsource.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.itsource.domain.Dept;
import cn.itsource.domain.Emp;
import cn.itsource.domain.EmpQuery;
import cn.itsource.service.IEmpService;
@Controller
public class EmpController {
@Autowired
private IEmpService service;
//查询全部功能
@RequestMapping("/findAll")
public String findAll(Map<String, Object> map){
List<Emp> findAll = service.findAll();
map.put("list", findAll);
return "show";
}
//根据条件查询功能
@RequestMapping("/findByCondition")
public String findByCondition(EmpQuery query,Map<String, Object> map){
List<Emp> list = service.findByCondition(query);
map.put("emp", query);
map.put("list", list);
return "show";
}
//删除功能
@RequestMapping("/del")
public String del(Integer eid){
service.del(eid);
return "redirect:/findAll";
}
}
12.编写前台页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>model</title>
<style type="text/css">
body{
background-color: silver;
}
table{
border-collapse: collapse;
width: 100%;
text-align: center;
margin: 20px auto;
font-family: "微软雅黑";
}
td{
border: 1px solid green;
height: 30px;
}
caption {
margin: 5px;
font-size: 30px;
font-weight: bold;
}
</style>
</head>
<body>
<form action="${pageContext.request.contextPath}/findByCondition">
<div>
<h1>员工列表显示</h1>
姓名:<input type="text" name="ename" value="${emp.ename }"/>  
地址:<input type="text" name="address" value="${emp.address }"/>  
部门编号:
<select name="deptno" id="deptno">
<option value="">请选择部门:</option>
<option value="10">技术部【10】</option>
<option value="20">销售部【20】</option>
<option value="30">财务部【30】</option>
<option value="40">人事部【40】</option>
</select>
薪水:
<select name="sal" id="sal">
<option value="">请选择薪水:</option>
<option value="3000.0">3000以下</option>
<option value="5000.0">[3000-5000)</option>
<option value="8000.0">[5000-8000)</option>
<option value="8001.0">8000以上</option>
</select>
<input type="submit" value="搜索"/>
</div>
<script type="text/javascript">
document.getElementById("deptno").value="${emp.deptno}";
document.getElementById("sal").value="${emp.sal}";
</script>
</form>
<table>
<thead>
<tr>
<td>ID</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>电话</td>
<td>地址</td>
<td>薪水</td>
<td>部门编号</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tbody">
<c:forEach items="${list }" var="e">
<tr>
<td>${e.eid}</td>
<td>${e.eno}</td>
<td>${e.ename}</td>
<td>${e.age}</td>
<td>${e.sex}</td>
<td>${e.tel}</td>
<td>${e.address}</td>
<td>${e.sal}</td>
<td>${e.deptno}</td>
<td><a href="/del?eid=${e.eid}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>