Source --- ssm integration framework

      One: the integration of environmental structures

 

1. To achieve the integration of SSM framework, we must first prepare the framework of the three JAR package, as well as the integration of other required JAR. In Eclipse, create a Web project called chapter17 will integrate the required JAR package is added to the lib directory of the project, and published to the classpath.

2. In chapter17 project, create a file called config in the source folder (Source Folder), create a database constants in the configuration file folder db.properties respectively, Spring configuration file applicationContext.xml, and MyBatis configuration file mybatis-config .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>

         Since Spring has been configured data source information, and an interface mapper document scanner, it is only necessary to configure the alias MyBatis profile according POJO class path.

   3. In the config folder, create a Spring MVC configuration file 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>  

In web.xml configuration file listener Spring, Spring MVC encoding filter and a front end controller information.

<?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: Application Integration Testing

In the src directory, create com.itheima.po package and create a persistent class Customer in the package:

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;
	}  

}

In src directory, create a com.itheima.dao package, and creates a corresponding interface file and CustomerDao CustomerDao.xml mapping file in the package, are edited as follows:

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>

In the src directory, create com.itheima.service package, and then create an interface file CustomerService in the package, and the method by query id defined CustomerService customers in:

package com.itheima.service;

import com.itheima.po.Customer;

public interface CustomerService {
        public Customer findCustomerById(Integer id);
}

In the src directory, create a com.itheima.service.impl package, and create a class that implements the interface CustomerServiceImpl CustomerService in the package:

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);
	}
}

In src directory, create a com.itheima.controller package, and creates the package for controlling the page request process based 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";
	
}
}

In the WEB-INF directory, create a jsp folder, create a page file for displaying customer details customer.jsp in this folder:

<%@ 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>

Publish projects to the server and start Tomcat, visit http address in your browser: // localhost:? 8081 / chapter17 / findCustomerById id = 1, the display effect is as follows

Below:

Published 376 original articles · won praise 172 · views 90000 +

Guess you like

Origin blog.csdn.net/Eider1998/article/details/104335265