网易首页 > 网易号 > 正文 申请入驻

java开发框架之SSM整合框架

0
分享至

一、搭建环境
1、创建数据库表和表结构
create table account(
id INT identity(1,1) primary key,
name varchar(20),
[money] DECIMAL
)
2、创建maven的工程SSM,在pom.xml文件引入依赖
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.mingqi
SSM
1.0-SNAPSHOT
war
SSM Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
5.0.2.RELEASE
1.6.6
1.2.12
5.1.6
3.4.5


org.aspectj
aspectjweaver
1.6.8

org.springframework
spring-aop
${spring.version}

org.springframework
spring-context
${spring.version}

org.springframework
spring-web
${spring.version}

org.springframework
spring-webmvc
${spring.version}

org.springframework
spring-test
${spring.version}

org.springframework
spring-tx
${spring.version}

org.springframework
spring-jdbc
${spring.version}

junit
junit
4.12
compile

com.microsoft.sqlserver
sqljdbc4
4.0

javax.servlet
servlet-api
2.5
provided

javax.servlet.jsp
jsp-api
2.0
provided

jstl
jstl
1.2

log4j
log4j
${log4j.version}

org.slf4j
slf4j-api
${slf4j.version}

org.slf4j
slf4j-log4j12
${slf4j.version}

org.mybatis
mybatis
${mybatis.version}

org.mybatis
mybatis-spring
1.3.0

org.springframework
spring-tx
${spring.version}

c3p0
c3p0
0.9.1.2
jar
compile

SSM

maven-clean-plugin
3.1.0

maven-resources-plugin
3.0.2

maven-compiler-plugin
3.8.0

maven-surefire-plugin
2.22.1

maven-war-plugin
3.2.2

maven-install-plugin
2.5.2

maven-deploy-plugin
2.8.2


3、部署ssm_web的项目,只要把ssm_web项目加入到tomcat服务器中即可
4、编写实体类
package com.mingqi.domain;
import java.io.Serializable;
public class Account implements Serializable {
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Double getMoney() {
return money;
}
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
5、编写dao接口
package com.mingqi.dao;
import com.mingqi.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IAccountDao {
@Insert(value="insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
@Select("select * from account")
public List findAll();
}
6、编写service接口和实现类
package com.mingqi.service;
import com.mingqi.domain.Account;
import java.util.List;
public interface IAccountService {
public void saveAccount(Account account);
public List findAll();
}
package com.mingqi.service.impl;
import com.mingqi.dao.IAccountDao;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
@Override
public List findAll() {
System.out.println("业务层:查询所有账户...");
List accounts=accountDao.findAll();
return accounts;
//return null;
}
}
二、Spring框架代码的编写


1. 搭建和测试Spring的开发环境
在webapp项目中创建applicationContext.xml的配置文件,编写具体的配置信息。
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">

expression="org.springframework.stereotype.Controller"/>

class="org.springframework.jdbc.datasource.DriverManagerDataSource">














三、Spring整合SpringMVC框架
1、搭建和测试SpringMVC的开发环境
1. 在web.xml中配置DispatcherServlet前端控制器
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application

org.springframework.web.context.ContextLoaderListener

contextConfigLocation
classpath:applicationContext.xml



dispatcherServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml

1

dispatcherServlet
/

characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8

characterEncodingFilter
/*
2、测试SpringMVC的框架搭建是否成功
1. 编写index.jsp和list.jsp编写,超链接
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

查询所有!
查询所有
测试查询
测试包
姓名:
金额:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
查询所有的帐户
${account.name}
2、创建AccountController类,编写方法,进行测试
package com.mingqi.controller;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
* 查询所有数据
* @return
@RequestMapping("/findAll")
public String findAll(Model model)
System.out.println("表现层查询所有");
accountService.findAll();
List list = accountService.findAll();
model.addAttribute("list",list);
return "list";
@RequestMapping("/save")
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;

配置tomcat,启动项目,进行测试

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
鲍仁君:若森林狼冲出西部则凯尔特人冠军 若掘金冲出西部将夺冠

鲍仁君:若森林狼冲出西部则凯尔特人冠军 若掘金冲出西部将夺冠

直播吧
2024-05-17 21:37:28
《星刃》更换服装似乎也让女主身体改变:更翘更大

《星刃》更换服装似乎也让女主身体改变:更翘更大

3DMGAME官方号
2024-05-17 17:15:07
第一波失业潮开始了!这三大行业或将沦为重灾区

第一波失业潮开始了!这三大行业或将沦为重灾区

诉人世间
2024-05-15 09:00:07
央5直播中加女排大战,张常宁有神奇发球,巴西球迷支持中国女排

央5直播中加女排大战,张常宁有神奇发球,巴西球迷支持中国女排

体育大学僧
2024-05-17 10:37:22
雷军登顶《歌手2024》摇人榜:投票是第二名韩红17倍!马云也杀入前10,Are you OK大战怒放的生命

雷军登顶《歌手2024》摇人榜:投票是第二名韩红17倍!马云也杀入前10,Are you OK大战怒放的生命

和讯网
2024-05-17 17:15:17
腿筋拉伤!缺席G6!场均40分钟,谁能顶得住啊

腿筋拉伤!缺席G6!场均40分钟,谁能顶得住啊

篮球教学论坛
2024-05-17 17:37:41
这个换人的大瓜?背后水很深???

这个换人的大瓜?背后水很深???

刘空青
2024-05-17 12:21:50
大S疑崩溃?台娱曝猛料:汪小菲退出公司股东,大S或一分钱拿不到

大S疑崩溃?台娱曝猛料:汪小菲退出公司股东,大S或一分钱拿不到

暖心说娱乐
2024-05-17 19:02:57
帕森斯:骑士现在最大分歧点是米切尔 他们要在垮台之前把他送走

帕森斯:骑士现在最大分歧点是米切尔 他们要在垮台之前把他送走

直播吧
2024-05-18 02:12:19
你知道哪些创业失败的重灾区?网友:都是忽悠中产阶级的情怀产业

你知道哪些创业失败的重灾区?网友:都是忽悠中产阶级的情怀产业

说故事的阿袭
2024-05-17 21:38:04
格里芬谈空接之城快船:夺冠需要运气 如果KD克莱没伤猛龙赢不了

格里芬谈空接之城快船:夺冠需要运气 如果KD克莱没伤猛龙赢不了

直播吧
2024-05-17 22:08:04
瑞士加速向中国运黄金,英法德日等8国集体抛美债,拜登无能为力

瑞士加速向中国运黄金,英法德日等8国集体抛美债,拜登无能为力

猫咪百万的日常
2024-05-17 13:17:08
百度王云鹏:特斯拉在武汉无人运营水平和百度还差三五年,“如果不信来武汉跑一跑”【附无人驾驶汽车行业现状分析】

百度王云鹏:特斯拉在武汉无人运营水平和百度还差三五年,“如果不信来武汉跑一跑”【附无人驾驶汽车行业现状分析】

前瞻网
2024-05-16 11:24:20
两大硬伤成了邱彪心里的结!新疆男篮真的难以翻盘,休赛期要反思

两大硬伤成了邱彪心里的结!新疆男篮真的难以翻盘,休赛期要反思

小鬼头体育
2024-05-18 01:29:12
十大灵异事件:神秘失踪的北京330路公交车,司机售票员离奇死亡

十大灵异事件:神秘失踪的北京330路公交车,司机售票员离奇死亡

平安是福呀
2023-10-09 10:43:08
逼良为娼,被强迫拍了50部三级片:从车模到女优,她只走错了一步

逼良为娼,被强迫拍了50部三级片:从车模到女优,她只走错了一步

爱过的人去了哪里
2022-06-19 16:40:26
汽车直播间现大尺度画面,多次展示裙底特写,评论区毫不避讳互动

汽车直播间现大尺度画面,多次展示裙底特写,评论区毫不避讳互动

娱乐八卦木木子
2024-05-15 19:59:08
45分钟售罄!上海又现日光盘豪宅!

45分钟售罄!上海又现日光盘豪宅!

中国基金报
2024-05-17 08:07:49
冲上热搜!“单价跌破10元”!价格真的跳水了?

冲上热搜!“单价跌破10元”!价格真的跳水了?

第一财经资讯
2024-05-17 08:46:05
广西民族大学教授夫妻被儿子砍死,遇害前哀求:我是妈妈啊

广西民族大学教授夫妻被儿子砍死,遇害前哀求:我是妈妈啊

纸鸢奇谭
2024-05-17 22:18:39
2024-05-18 07:12:49
IT爱好者小尚
IT爱好者小尚
分享IT教育类信息
630文章数 55关注度
往期回顾 全部

科技要闻

京东拼增长,大力出奇迹

头条要闻

媒体:菲律宾在南海闹事时 美国航母紧急"撤"到新加坡

头条要闻

媒体:菲律宾在南海闹事时 美国航母紧急"撤"到新加坡

体育要闻

中超疯狂星期五!5场28球,单场5球起步

娱乐要闻

《庆余年2》首播口碑出炉!有好有坏

财经要闻

重磅!楼市王炸来了 多部门出手救楼市

汽车要闻

内饰与配置全新升级 全新途观L PRO将于5月30日上市

态度原创

数码
亲子
健康
房产
旅游

数码要闻

驰为 CoreBox 迷你主机发布:i5-13500H、2.5G 网口,1999 元起

亲子要闻

小朋友背着琵琶下课,像极了琵琶放假~

在中国,到底哪些人在吃“伟哥”?

房产要闻

19.1亿,三亚挂出超级教育+宅地!要建国际学校,这个板块价值又要涨!

旅游要闻

火车票改签收手续费了?12306回应

无障碍浏览 进入关怀版