整体环境搭建

准备所需的JAR包
整合SSM框架
编写配置文件
db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/jwdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
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>

mybaits-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>

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

<?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>

整体应用测试

Customer.java

package com.itheima.po;

public class Customer {
private Integer id;
private Integer c_sn;
private String c_name;
private String c_gender;
private String c_class;
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public Integer getC_sn() {
	return c_sn;
}
public void setC_sn(Integer c_sn) {
	this.c_sn = c_sn;
}
public String getC_name() {
	return c_name;
}
public void setC_name(String c_name) {
	this.c_name = c_name;
}
public String getC_gender() {
	return c_gender;
}
public void setC_gender(String c_gender) {
	this.c_gender = c_gender;
}
public String getC_class() {
	return c_class;
}
public void setC_class(String c_class) {
	this.c_class = c_class;
}

}

CustomerDao.java

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_student where id=#{id}
</select>
</mapper>

CustomeService.java

package com.itheima.service;

import com.itheima.po.Customer;

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

CustomeServicelmpl.java

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

CustomerController.java

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

customer.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>编号</td>
   <td>学号</td>
   <td>姓名</td>
   <td>性别</td>
   <td>班级</td>
   </tr>   
    <tr>
   <td>${customer.id}</td>
   <td>${customer.c_sn}</td>
   <td>${customer.c_name}</td>
   <td>${customer.c_gender}</td>
   <td>${customer.c_class}</td>
   </tr>
  </table>
</body>
</html>

实验结果

整合SSM框架

相关文章: