SSM-项目搭建简单实例(非maven,调用mybatis配置文件)

SSM-项目搭建简单实例

(1)index页面发起请求,web.xml配置,让controller接受请求
(2)同时,web.xml中配置Spring-mvc.xml配置文件,完成Spring配置
(3)spring-mvc.xml中,完成与mybatis的整合,并将service,maopper等注入
(4)controller接受到请求,做出响应

环境:

tomcat 8.5
jdk1.8
MySQL
druid 阿里巴巴数据库池

所用jar包

在这里插入图片描述

数据库

在这里插入图片描述

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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">
	<display-name>ssm-Test</display-name>
	<!-- 请求都交给DispatcherServlet处理 -->
	<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:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 清除jsp空格 -->
	<jsp-config>
		<jsp-property-group>
			<url-pattern>*.jsp</url-pattern>
			<trim-directive-whitespaces>true</trim-directive-whitespaces>
		</jsp-property-group>
	</jsp-config>
</web-app>

spring-mvc.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:p="http://www.springframework.org/schema/p"
	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-3.0.xsd
						http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

	<!--     1) 读取properties中的内容 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--     2) 直接数据库,采用阿里巴巴的druid数据库池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        	<property name="driverClassName" value="${jdbc.driver}" /> 
			<property name="url" value="${jdbc.url}" /> 
			<property name="username" value="${jdbc.username}" /> 
			<property name="password" value="${jdbc.password}" />
    </bean>
  
   <!--  3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <!--      注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置mybatis全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    
    <!-- 4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!--  注入SqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描的Dao接口 -->
        <property name="basePackage" value="dao"/>
    </bean>

    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
     <context:component-scan base-package="service"/>
    <context:component-scan base-package="controller" />
   
    <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,  
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->  
    <mvc:annotation-driven />  

    <!-- 视图层配置 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
		<!--配置JSTL表达式-->  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <!-- 前缀 -->
        <!-- <property name="prefix" value="/WEB-INF/view/"/>  --> 
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>  
	</bean>  
	
	<!-- 配置静态资源 -->
	<mvc:resources location="/WebContent/" mapping="/WebContent/**"/>  
</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> 
		<typeAlias type="pojo.Student" alias="Student"/> 
	  </typeAliases> 
	  
	  <!-- 映射文件 -->
	  <mappers> 
		<mapper resource="dao/StudentMapper.xml" /> 
	  </mappers> 
	</configuration> 

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/Student?useUnicode=true&characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root

pojo

package pojo;

public class Student {
	private String son;
	private String sname;
	private String ssex;
	private int sage;
	private String sdept;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getSon() {
		return son;
	}
	public void setSon(String son) {
		this.son = son;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}
	public int getSage() {
		return sage;
	}
	public void setSage(int sage) {
		this.sage = sage;
	}
	public String getSdept() {
		return sdept;
	}
	public void setSdept(String sdept) {
		this.sdept = sdept;
	}
	@Override
	public String toString() {
		return "Student [son=" + son + ", sname=" + sname + ", ssex=" + ssex + ", sage=" + sage + ", sdept=" + sdept
				+ "]";
	}
	
}

dao

package dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import pojo.Student;


public interface StudentMapper {
	List<Student> findAllStudents();
}

<?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="dao.StudentMapper"> 
	
	  <resultMap type="Student" id="StudentResult"> 
		<id property="son" column="son" /> 
		<result property="sname" column="sname" /> 
		<result property="ssex" column="ssex" /> 
		<result property="sage" column="sage" /> 
		<result property="sdept" column="sdept" /> 
	  </resultMap> 
	  
	  <select id="findAllStudents" resultMap="StudentResult"> 
			select * from student 
	  </select> 
	  
	</mapper> 

service

package service;

import java.util.List;

import pojo.Student;

public interface StudentService {
	List<Student> select();
}

package service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.StudentMapper;
import pojo.Student;
@Service("StudentService")
public class StudentServiceImpl implements StudentService{
	@Autowired
	private StudentMapper studentMapper;
	
	
	@Override
	public List<Student> select() {
		// TODO Auto-generated method stub
		return studentMapper.findAllStudents();
	}


	
	
}

controller

package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pojo.Student;
import service.StudentService;

@Controller
@RequestMapping(value="/Student",produces="text/html;charset=UTF-8")
public class StudentController {
	// 获得spring容器
	//static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	@Autowired(required = true)
	private StudentService studentService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String get(HttpServletRequest request){  
		
		List<Student> Students = studentService.select();
    
//		System.out.println(studentService);
		request.setAttribute("stu",Students);
		
		return "StudentAll";  //返回指向login.jsp页面
	}
}

页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
		<style type="text/css">
			.asd{
				text-align: center;
			}
		</style>
	</head>
	<body class="asd">
	
		<h1>@CaoPengCheng</h1>
		<form action="Student">
		
			<input  type="submit" name="提交">
		
		</form>
	</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
		<style type="text/css">
		.div{
			text-align: center;
		}
		</style>
	</head>
	<body>

		<div class="div">
			<h1>HelloWorld!!!</h1>
		 <c:forEach items="${stu}" var="stu">
			<tr>
				<td>${stu.son}  </td>
				<td>${stu.sname }  </td>
				<td>${stu.ssex }  </td>
				<td>${stu.sage }  </td>
				<td>${stu.sdept}  </td>
			</tr><br>
		 </c:forEach>
		<h1>@CaoPengCheng</h1>
		</div>
	</body>
</html>

测试

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CaoPengCheng&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值