SSM框架整合——環境搭建+整合應用測試

1.文件組織結構html

 

  

2.所需jar包:前端

連接:https://share.weiyun.com/51uCJBcjava

 

3.建立數據庫mybatis:mysql

create database mybatis;
use mybatis;
create table t_customer
(
id int(32) primary key auto_increment,
username varchar(50),
jobs varchar(50),
phone varchar(16)
);
insert into t_customer(username, jobs, phone)
values ('June', 'student', '1862223890*');

3.配置文件web

  3.1數據庫常量配置文件db.properties:spring

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

  3.2Spring配置文件applicationContext-db.xml:sql

  首先定義了讀取db.properties文件的配置和數據源配置,而後配置了事務管理器並開啓了事務註解。接下來配置了用於整合MyBatis框架的MyBatis工廠信息,最後定義了mapper掃描器DAO層以及Service層的配置。數據庫

<?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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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.sky.dao"/>
</bean>
<context:component-scan base-package="com.sky.service"/>
</beans>

  3.3MyBatis配置文件mybatis-config.xml:apache

  因爲在Spring中已經配置了數據源信息以及mapper接口文件掃描器,因此在MyBatis的配置文件中只須要根據POJO類路徑進行別名配置便可。瀏覽器

<?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.sky.po"/>
</typeAliases>
</configuration>

3.4Spring配置文件springmvc-config.xml:

  主要配置了用於掃描@Controller註解的包掃描器、註解驅動器以及視圖解析器。

<?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.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">
<context:component-scan base-package="com.sky.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>

  3.5web.xml:

  配置Spring的文件監聽器、編碼過濾器以及Spring MVC的前端控制等信息。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<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>
</web-app>

4.整合應用測試

  4.1建立持久化類Customer:com.starrysky.po:Customer.java

package com.sky.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;
}
}

  4.2建立接口文件CustomerDao:com.starrysky.dao:CustomerDao.java

package com.sky.dao;

import com.sky.po.Customer;

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

  4.3根據接口文件的方法編寫執行語句信息:com.starrysky.dao:Customer.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.sky.dao.CustomerDao">
<select id="findCustomerById" parameterType="Integer" resultType="Customer">
select * from t_customer where id = #{id}
</select>
</mapper>

  4.4建立接口文件CustomerService:com.starrysky.service:CustomerService.java

package com.sky.service;

import com.sky.po.Customer;

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

  4.5建立CustomerService接口實現類CustomerServiceImpl:com.starrysky.service.impl:CustomerServiceImpl.java

package com.sky.service.impl;

import com.sky.dao.CustomerDao;
import com.sky.po.Customer;
import com.sky.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;

@Override
public Customer findCustomerById(Integer id) {
return this.customerDao.findCustomerById(id);
}
}

  4.6建立用於處理頁面請求的控制器類CustomerController:com.starrysky.controller:CustomerController.java

package com.sky.controller;

import com.sky.po.Customer;
import com.sky.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;

@RequestMapping("/findCustomerById")
public String findCustomerById(Integer id, Model model) {
Customer customer = customerService.findCustomerById(id);
model.addAttribute("customer", customer);
return "customer";
}
}

  4.7建立用於展現客戶詳情的頁面文件:WEB-INF/jsp/customer.jsp

<%--
Created by IntelliJ IDEA.
User: Starry-Sky
Date: 2019/10/9
Time: 23:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>客戶信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</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>

 5.發佈項目到tomcat服務器並啓動,在瀏覽器訪問地址:http://localhost:8080/ssm/findCustomerById?id=1

相關文章
相關標籤/搜索