SSM-Maven项目实例

SSM-Maven项目实例

web.xml文件调用Spring文件和SpringMvc文件,整个项目不需要
使用new关键字,全部采用Ioc和DI

环境

maven,tomcat,Spring,SpringMvc,Mybatis

数据库

SSM-Maven项目实例_第1张图片

Maven pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>ssmgroupId>
  <artifactId>SSM-Test2artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>

  <name>SSM-Test2name>
  <url>http://maven.apache.orgurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>
	
  <dependencies>
  		 
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.18.RELEASEversion>
        dependency>
 
		<dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>
		<dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>
 
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.17version>
        dependency>
 
        
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.2.3version>
        dependency>
 
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
 
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.3version>
        dependency>
 
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.6version>
        dependency>
 
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>
        
	    <dependency>
	      <groupId>junitgroupId>
	      <artifactId>junitartifactId>
	      <version>3.8.1version>
	      <scope>testscope>
	    dependency>
	    
	    <dependency>
            <groupId>javax.servlet.jsp.jstlgroupId>
            <artifactId>jstl-apiartifactId>
            <version>1.2version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servletgroupId>
                    <artifactId>servlet-apiartifactId>
                exclusion>
                <exclusion>
                    <groupId>javax.servlet.jspgroupId>
                    <artifactId>jsp-apiartifactId>
                exclusion>
            exclusions>
        dependency>
 
        <dependency>
            <groupId>org.glassfish.webgroupId>
            <artifactId>jstl-implartifactId>
            <version>1.2version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servletgroupId>
                    <artifactId>servlet-apiartifactId>
                exclusion>
                <exclusion>
                    <groupId>javax.servlet.jspgroupId>
                    <artifactId>jsp-apiartifactId>
                exclusion>
                <exclusion>
                    <groupId>javax.servlet.jsp.jstlgroupId>
                    <artifactId>jstl-apiartifactId>
                exclusion>
            exclusions>
        dependency>
	  dependencies>
project>

数据库连接文件

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

实体类pojo

package ssm.SSM_Test2.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
				+ "]";
	}
	
}

Spring配置文件


<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
                           
     <context:component-scan base-package="ssm.SSM_Test2.service"/>
    <context:component-scan base-package="ssm.SSM_Test2.controller" />
 
 
    <context:property-placeholder location="classpath:jdbc.properties"/>


    <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>
  
   
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  		 
        <property name="dataSource" ref="dataSource"/>
		<property name="typeAliasesPackage" value="ssm.SSM_Test2">property>
        <property name="configurationProperties">
            <props>
                <prop key="cacheEnabled">trueprop> 
            props>
        property>
        
        <property name="mapperLocations" value="classpath:ssm/SSM_Test2/dao/*Mapper.xml" />
    bean>
    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="ssm.SSM_Test2.dao"/>
    bean>
    

beans>

SpringMvc配置文件

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

    
    <context:component-scan base-package="ssm.SSM_Test2.controller" />

      
    <mvc:annotation-driven />  

      
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
		  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
       
        
        <property name="suffix" value=".jsp"/>  
	bean>  
	
	
	<mvc:resources location="/WebContent/" mapping="/WebContent/**"/>  
beans>  

web.xml


<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>SSM2display-name>
	
	<context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    <listener>      
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>


	
	<servlet>
		<servlet-name>springmvcservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:spring-mvc.xmlparam-value>
		init-param>
	servlet>
	<servlet-mapping>
		<servlet-name>springmvcservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	<jsp-config>
		<jsp-property-group>
			<url-pattern>*.jspurl-pattern>
			<trim-directive-whitespaces>truetrim-directive-whitespaces>
		jsp-property-group>
	jsp-config>
web-app>

dao持久层

<?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="ssm.SSM_Test2.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> 
package ssm.SSM_Test2.dao;

import java.util.List;

import ssm.SSM_Test2.pojo.Student;


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

service 业务层

package ssm.SSM_Test2.service;

import java.util.List;

import ssm.SSM_Test2.pojo.Student;

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

package ssm.SSM_Test2.service.Impl;

import java.util.List;

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

import ssm.SSM_Test2.dao.StudentMapper;
import ssm.SSM_Test2.pojo.Student;
import ssm.SSM_Test2.service.StudentService;

@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 ssm.SSM_Test2.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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

import ssm.SSM_Test2.pojo.Student;
import ssm.SSM_Test2.service.StudentService;

@Controller
@RequestMapping(value="/Student",produces="text/html;charset=UTF-8")
public class StudentController {
     
	@Autowired
	private StudentService studentService;
		
	@RequestMapping(method=RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public String get(HttpServletRequest request){
       
		List<Student> students = studentService.select();
		String s = request.getParameter("test");
		System.out.println("S="+s);
		request.setAttribute("Student", students);
		return "StudentAll"; 
	}
}

页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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">
			.f{
     
				margin-top:100 px;
				font-size:100 px;
				text-align: center;
			}
		
		</style>
	
	
	</head>
	<body>
	
		<form action="Student" method="get" class="f">
			<input type="text" name="test"><br>
			<input type="submit" value="提交">
		</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>Student</title>
		<style type="text/css">
		.div{
     
			margin-top:50px;
			text-align: center;
			font-size: 20px;
		}
		</style>
	</head>
	<body>

		<div class="div">
		 <c:forEach items="${Student}" 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>
		</div>
	</body>
</html>

测试

SSM-Maven项目实例_第2张图片
SSM-Maven项目实例_第3张图片
SSM-Maven项目实例_第4张图片

你可能感兴趣的:(SSM,spring,mybatis,java)