SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为持久层,DAO层,controller层,service层,
spring MVC负责请求的转发和视图管理
spring负责实现业务对象管理,mybatis作为数据对象的持久化引擎
以查询客户信息为实例
开发工具:eclipse
项目结构
用到的包
配置文件
db.properties:数据库源配置信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
日志文件log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.zzf=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.zzf.service"></context:component-scan>
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}"></property>
<!-- 连接数据库url -->
<property name="url" value="${jdbc.url}"/>
<!-- 连接数据库的用户名密码 -->
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}"></property>
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解,注册事务管理驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置MyBatis工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定核心配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- Mapper代理开发(基于MapperScannerConfigurer) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zzf.dao"></property>
</bean>
</beans>
mybatis框架的mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 别名,配置扫描包形式,别名就是类首字母小写 -->
<package name="com.zzf.po"/>
</typeAliases>
</configuration>
springmvc配置文件springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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 ">
<!-- 指定需要扫描的包 -->
<context:component-scan base-package="com.zzf.controller"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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">
<display-name>SSM</display-name>
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring文件监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化时加载配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</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>
持久层
po类
package com.zzf.po;
/**
*
* 客户持久层
*
*/
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Dao层
Dao接口和对应的mapper.xml文件
package com.zzf.dao;
import com.zzf.po.Customer;
public interface CustomerDao {
/**
* 根据id查询客户信息
*
*/
public Customer findCustomerById(Integer id);
}
<?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代理开发规范
Mapper接口的名称和对应的Mapper.xml映射文件的名称必须一致
Mapper.xml文件的namespace与Mapper接口的类路径相同(即接口文件和映射文件需要放在同一个包中)
Mapper接口中的方法名和Mapper.xml中定义的每个执行语句的id相同
Mapper接口中方法的输入参数类型和Mapper.xml中定义的每个sql的parameterType类型相同
Mapper接口中方法的输入参数类型和Mapper.xml中定义的每个sql的resultType类型相同
-->
<!-- namespace表示命名空间 -->
<mapper namespace="com.zzf.dao.CustomerDao">
<!-- 根据id查询客户信息 -->
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
Service层
接口
package com.zzf.service;
import com.zzf.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
实例化类
package com.zzf.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zzf.dao.CustomerDao;
import com.zzf.po.Customer;
import com.zzf.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
//依赖注入
//注解注入CustomerDao
@Autowired
private CustomerDao CustomerDao;
//查询客户
@Override
public Customer findCustomerById(Integer id) {
// TODO 自动生成的方法存根
return this.CustomerDao.findCustomerById(id);
}
}
Controller控制器层
package com.zzf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zzf.po.Customer;
import com.zzf.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* 根据id查询客户详情
*
*/
@RequestMapping("/findCostomerById")
public String findCostomerById(Integer id,Model model) {
Customer customer=customerService.findCustomerById(id);
model.addAttribute("customer", customer);
return "customer";
}
}
jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户信息</title>
</head>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>名称</td>
<td>职业</td>
<td>电话</td>
</tr>
<tr>
<td>${customer.id }</td>
<td>${customer.username }</td>
<td>${customer.jobs }</td>
<td>${customer.phone }</td>
</table>
</body>
</html>
数据库表
启动服务器浏览器输入(id是传输的参数)
http://localhost:8080/SSM/findCostomerById?id=7