关于SSM的整合
1、导入相关的jar包需要的话可以下载最基础的jar包 提取码iecv
2、导入数据源文件以及mybatis的配置文件
//数据源文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ssm?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
//mybatis配置文件
<?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>
<typeAlias type="com.ssm.bean.Customer" alias="Customer"/>
</typeAliases>
<mappers>
<mapper resource="com/ssm/bean/customerMapper.xml"/>
</mappers>
</configuration>
3、配置spring的xml
//spring的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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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-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">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 开启自动扫描注解 -->
<context:component-scan base-package="com.ssm.service.impl"></context:component-scan>
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:conf.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mapper代理的方式开发方式二,扫描包方式配置代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口 -->
<property name="basePackage" value="com.ssm.dao" />
</bean>
<!--声明式事务管理-->
<bean id="myHibTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="myHibTransactionManager"/>
</bean>
5、spring-mvc的配置
//配置spring-mvc
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!-- 开启控制器包的自动扫描 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 加载注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
</bean>
</beans>
6、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>SSM01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</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> <!--拦截所有的URL请求 -->
</filter-mapping>
</web-app>
7、完成上述步骤之后可以在文件的路径下建立一个简单的例子
bean包下
package com.ssm.bean;
/**
* 客户实体类
* @author ***
*
*/
public class Customer {
private int id;//客户Id
private String userName;//客户名称
private String jobs;//客户职位
private String phone;//客户电话
public int getId() {
return id;
}
public void setId(int 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;
}
}
加入mapper映射文件
//实现简单的查询功能
<?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.ssm.dao.ICustomerDao">
<select id="findCustomerById" parameterType="int" resultType="Customer">
select * from customer where id=#{id}
</select>
</mapper>
8、创建接口与映射文件对应(前面的xml中定义了mapper代理的形式)
dao包下:
package com.ssm.dao;
import com.ssm.bean.Customer;
public interface ICustomerDao {
/**
* 根据客户id查询信息
* @param id
* @return
*/
public Customer findCustomerById(int id);
}
9、创建一个service来调用上面的接口
package com.ssm.service;
import com.ssm.bean.Customer;
public interface ICustomerService {
/**
* 根据客户id查询信息
* @param id
* @return
*/
public Customer findCustomerById(int id);
}
实现接口
package com.ssm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.bean.Customer;
import com.ssm.dao.ICustomerDao;
import com.ssm.service.ICustomerService;
@Service
public class CustomerService implements ICustomerService {
//实例化dao对象
@Autowired
ICustomerDao customerDao;
@Override
public Customer findCustomerById(int id) {
//通过customer对象调用方法进行查询
return customerDao.findCustomerById(id);
}
}
10、创建一个controller来返回视图,以及调用查询方法
//控制器
package com.ssm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ssm.bean.Customer;
import com.ssm.service.ICustomerService;
@Controller
public class CustomerController {
@Autowired
private ICustomerService customerService;
@RequestMapping("/find/{id}")
public String find(@PathVariable int id,Model model) {
Customer customer=customerService.findCustomerById(id);
model.addAttribute("customer", customer);
return "list";
}
}
11、创建一个jsp文件夹并创建视图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
<td>name</td>
<td>jobs</td>
<td>tel</td>
</tr>
<tr>
<td>${customer.id }</td>
<td>${customer.userName}</td>
<td>${customer.jobs }</td>
<td>${customer.phone }</td>
</tr>
</table>
</body>
</html>
12、最终我们就可以测试项目了
在浏览器中输入正确的地址
项目的大致路径图
完