하나 : 환경 구조의 통합
1. SSM 프레임 워크의 통합을 달성하기 위해, 우리는 먼저 세 개의 JAR 패키지의 프레임 워크뿐만 아니라 다른 필요한 JAR의 통합을 준비해야합니다. 이클립스에서 chapter17 필요한 JAR 패키지는 프로젝트의 lib 디렉토리에 추가하고, 클래스 경로에 게시 통합 할 것이다라는 웹 프로젝트를 만듭니다.
2. chapter17 프로젝트에서의 설정이라는 파일 생성 소스 폴더 각각 Spring 설정 파일 applicationContext.xml 및 MyBatis로 구성 파일 MyBatis로-설정을 구성 파일 폴더 db.properties에서 데이터베이스 상수를 생성 (소스 폴더) .XML.
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxTotal" value="${jdbc.maxTotal}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
<context:component-scan base-package="com.itheima.service" />
</beans>
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.itheima.po" />
</typeAliases>
</configuration>
스프링 데이터 소스 정보, 및 인터페이스 매퍼 문서 스캐너 구성되어 있기 때문에, POJO 클래스 경로있어서 별명의 MyBatis 프로파일을 구성하는 것만이 필요하다.
는 config 폴더에서 3, 스프링 MVC 설정 파일 springmvc-config.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-4.3.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">
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan
base-package="com.itheima.controller" />
<!-- 加载注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
스프링 청취자 web.xml의 구성 파일에서 스프링 MVC는 필터 전단 제어 정보를 인코딩.
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置加载Spring文件的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 编码过滤器 -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端控制核心 -->
<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>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /:拦截所有请求(除了jsp) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
II : 응용 프로그램 통합 테스트
src 디렉토리에서 com.itheima.po 패키지를 만들고 패키지에 영속 클래스 고객을 만들 :
package com.itheima.po;
public class Customer {
private Integer id; // 主键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;
}
}
다음과 같이 SRC 디렉토리에서 편집하는 com.itheima.dao 패키지를 작성하고 패키지에 해당 인터페이스 파일과 CustomerDao CustomerDao.xml 매핑 파일을 생성합니다 :
package com.itheima.dao;
import com.itheima.po.Customer;
/**
* Customer接口文件
* @author Administrator
*
*/
public interface CustomerDao {
/**
* 根据id查询客户信息
*/
public Customer findCustomerById(Integer id);
}
CustomerDao.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.itheima.dao.CustomerDao">
<!-- 根据id查询客户信息 -->
<select id="findCustomerById" parameterType="Integer" resultType="Customer" >
select * from t_customer where id=#{id}
</select>
</mapper>
src 디렉토리에서 com.itheima.service 패키지를 만든 다음 패키지의 인터페이스 파일를 CustomerService를 생성하고 쿼리 ID로 방법에 CustomerService를 고객 정의 :
package com.itheima.service;
import com.itheima.po.Customer;
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
src 디렉토리에서 클래스를 com.itheima.service.impl 패키지를 생성하고, 생성이 구현 패키지의 인터페이스 CustomerServiceImpl CustomerService를 :
package com.itheima.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.dao.CustomerDao;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
//注解注入Customer
@Autowired
private CustomerDao customerDao;
//查询客户
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}
SRC 디렉토리에서, com.itheima.controller 패키지를 작성하고 페이지 요청 프로세스를 기반으로 CustomerController을 제어하기위한 패키지를 만듭니다
package com.itheima.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.itheima.po.Customer;
import com.itheima.service.CustomerService;
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* 根据id查询客户详情
*
*/
@RequestMapping("/info")
public String findCustomerById(Integer id,Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute("customer",customer);
//返回客户信息展示页面
return "customer";
}
}
WEB-INF 디렉토리에, JSP로 폴더를 만들고이 폴더에 customer.jsp 고객의 세부 사항을 표시하는 페이지 파일을 만듭니다
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WINNER</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>
</tr>
</table>
</body>
</html>
서버에 프로젝트를 게시하고 톰캣, 브라우저에서 방문 HTTP 주소를 시작합니다 : // localhost를 :? 8081 / chapter17 /이 ID는 = 1, 디스플레이 효과는 다음과됩니다 findCustomerById
아래 :