javaee实验,SSM整合开发综合实例

由于不能使用maven管理,只能导入jar包做实验,最下面有截图展示所用到的jar包,可以自己搜索文档maven导入依赖;

SSM整合开发综合实例

实验目的:
(1)掌握SSM项目整合的原则;
(2)掌握SSM框架的整合步骤;
(3)掌握综合开发实例的实现;

实验要求:
(1) 完成SSM框架的整合搭建;
(2) 完成SSM项目的配置;
(3) 完成相关代码的编写;
(4) 完成jsp页面的编码;

实验内容:
根据给定的数据库,完成一个SSM框架的项目搭建,要求最终实现在一个页面中输入学校名称和学校人数,点击按钮,将数据插入到数据库中,要求在过程中使用SSM 框架完成。

由于配置和代码中含有路径的原因,先展示一下目录:
在这里插入图片描述
1.配置文件代码:
application.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: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.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
	<!-- 注册数据源 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/javaeetest7?useSSL=false" />
		<property name="user" value="root" />
		<property name="password" value="123456" />
	</bean>
	 <!-- 配置Session工厂对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置扫描的包-->
    <bean id="mapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ldu.dao"></property>
    </bean>
	<!-- 扫描注解的类-->
    <context:component-scan base-package="com.ldu.service"></context:component-scan>
</beans>

springmvc.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.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.ldu"/>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<mvc:annotation-driven/>
	<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>javaEETest6.0</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
	<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.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>

2.业务逻辑功能代码
实体类School.java:

package com.ldu.pojo;

public class School {
	private Integer id;
	private String name;
	private int persons;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPersons() {
		return persons;
	}
	public void setPersons(int persons) {
		this.persons = persons;
	}
	@Override
	public String toString() {
		return "school [id=" + id + ", name=" + name + ", persons=" + persons + "]";
	}
}

Dao层 SchoolDao.java:

package com.ldu.dao;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Repository;

@Repository
public interface SchoolDao {
	
	@Insert("insert into t_school(name, persons) values(#{param1},#{param2})")
	int InsertSchool(String name, Integer persons);
}

Service层 SchoolService.java:

package com.ldu.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ldu.dao.SchoolDao;

@Service
public class SchoolService {
	
	@Resource
	private SchoolDao schoolDao;
	
	public int insertSchool(String name, Integer persons) {
		return schoolDao.InsertSchool(name, persons);
	}	
}

Controller层 SchoolController.java:

package com.ldu.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ldu.service.SchoolService;
import com.ldu.pojo.School;

@Controller
@RequestMapping("/school")
public class SchoolController {
	
	@Resource
	private SchoolService schoolService;
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String add(School school,Model model) {
		if(schoolService.insertSchool(school.getName(), school.getPersons())>0) {
			model.addAttribute("msg", "添加成功!");
		}
		else {
			model.addAttribute("msg", "添加失败!");
		}
		return "msg";
	}

}

3.Jsp页面的代码:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML">
<html>
<head>
  <title>新增学校</title>
</head>
  
 <body>
	<form action="${pageContext.request.contextPath }/school/add" method="post">
		校名:<input name="name"><br/>
		人数:<input name="persons"><br/>
		<input type="submit" value="新增学校">
	</form>
 </body>
</html>

msg.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML">
<html>
<head>
  <title>反馈信息</title>
</head>
  
 <body>
	<h3>${msg}</h3>
 </body>
</html>

jar包
在这里插入图片描述

结果图:在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. 报错:Error creating bean with name ‘ ’ 原因是未开启注解扫描、未设置注解扫描的包路径。
  2. 要注意检查DAO、Controller、Service层中相应的注解是否加上。

小知识:
实体类中用Integer而不是int的原因是:Integer是int包装后的对象,默认值是null,而int默认值是0。如果用int,在未赋值的情况下传入数据库,数据库接收到的是0而不是null;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代码不会敲

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

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

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

打赏作者

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

抵扣说明:

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

余额充值