今天来搭建一个SSM项目的小例子简单练一练,那项目模板还是我们那个模板,就是我们在JavaWeb最后的小例子,那到SSM中我们如何实现,后面我们再看看springboot中如何实现
javaweb中项目例子:https://blog.csdn.net/hello_list/article/details/124734814
首先我们来搭建一个SSM项目,同时也是SSM项目整合哦!
环境:idea2019 、maven、jdk1.8
创建一个maven项目,直接next
现在还不是一个web项目
项目右键,选择这个
我们把web勾上
这样就是一个标准的web项目了,比起maven自建的我觉得好看,普普通通的maven项目看起来最顺眼
项目就就创建好了,我们运行下,运行之前我们放个login.jsp页面,随便在网上找了一个页面:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logintitle>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<style>
.loginForm {
/*边框高度*/
height: 350px;
/*边框宽度*/
width: 500px;
/*边框颜色*/
border: #4d4d4d solid 1px;
/*边框圆角*/
border-radius: 4px;
/*阴影 水平方向,竖直方向,模糊距离*/
box-shadow: 5px 5px 5px #4d4d4d;
/*上边界距离*/
margin-top: 300px;
/*左边界距离:自动*/
margin-left: auto;
/*右边界距离:自动*/
margin-right: auto;
/*用户名、密码间距*/
padding: 20px 40px;
}
/*将用户登录置于中间*/
.loginForm h2 {
text-align: center;
}
/*修改button属性*/
.button {
text-align: center;
vertical-align: middle;
}
style>
head>
<body>
<div class="loginForm">
<h2>用户登录h2>
<form>
<div class="form-group">
<label for="exampleInputEmail1">用户名label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="请输入用户名">
div>
<div class="form-group">
<label for="exampleInputPassword1">密码label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入密码">
div>
<div class="checkbox">
<label>
<input type="checkbox"> 同意
<a href="#"> xxx安全协议a> 与 <a href="#"> xxx隐私协议a>
label>
div>
<div class="button">
<input type="submit" class="btn btn-primary" value="登 录"/>
div>
form>
div>
body>
html>
打包运行项目,手把手教你,
如果没有打包,自己打个包,我们看到没有lib目录,后面我们项目的依赖不一定会加进来,现在没有依赖,后面可能我们会需要自己建一个,可能你idea不一样,视情况而变
把我们的项目加进来
OK,启动成功
下面我们开始SSM整合,然后开始写例子
万事开头加入依赖:
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
dependencies>
一定要检查自己的依赖都加进来了
添加一个maven的过滤
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
首先我们搭建spring框架:
总配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/huanying?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=
mybatis的配置文件:mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.xxx.pojo"/>
typeAliases>
<mappers>
<package name="com.xxx.dao.mapper"/>
mappers>
configuration>
然后通过spring去接管一下:spring-dao
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
bean>
<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="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.xuerxiriji.dao"/>
bean>
beans>
我们再直接把service层搞定下:spring-service.xml
<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"
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">
<context:component-scan base-package="com.xuexiriji.service" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
OK,接下来我们整合springMVC
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
filter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<session-config>
<session-timeout>15session-timeout>
session-config>
web-app>
<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
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
bean>
<context:component-scan base-package="com.xuexiriji.controller" />
beans>
最后都放到我们applicationContext.xml总配置中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
beans>
我们可以看到,配置好的文件idea会自动识别spring小叶子
写了这么多配置文件,项目算是搭好了,但是还没有写代码,下面我们对一个表试试CRUD
表结构,没错啦就是我们在javaweb中的Good货物表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for good
-- ----------------------------
DROP TABLE IF EXISTS `good`;
CREATE TABLE `good` (
`goodid` int(20) NOT NULL AUTO_INCREMENT,
`goodname` varchar(20) NOT NULL,
`surplus` int(20) NOT NULL,
`goodprice` int(20) NOT NULL,
PRIMARY KEY (`goodid`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of good
-- ----------------------------
INSERT INTO `good` VALUES ('1', '黑色森林', '2', '15');
INSERT INTO `good` VALUES ('2', '奶油蛋糕', '3', '13');
INSERT INTO `good` VALUES ('3', '水果蛋糕', '2', '11');
INSERT INTO `good` VALUES ('4', '冰淇凌蛋糕', '5', '13');
INSERT INTO `good` VALUES ('9', '牛奶蛋糕', '34', '9');
INSERT INTO `good` VALUES ('11', '肉松蛋糕', '13', '13');
INSERT INTO `good` VALUES ('12', '草莓蛋糕', '99', '23');
INSERT INTO `good` VALUES ('13', '苹果蛋糕', '32', '12');
INSERT INTO `good` VALUES ('14', '香蕉蛋糕', '32', '12');
INSERT INTO `good` VALUES ('15', '火龙果蛋糕', '43', '43');
INSERT INTO `good` VALUES ('16', '香橙蛋糕', '65', '31');
INSERT INTO `good` VALUES ('17', '苹果', '43', '54');
INSERT INTO `good` VALUES ('18', '菠萝', '32', '32');
INSERT INTO `good` VALUES ('19', '橙子', '435', '32');
INSERT INTO `good` VALUES ('20', '花椒', '43', '65');
INSERT INTO `good` VALUES ('21', '大蒜', '23', '54');
INSERT INTO `good` VALUES ('22', '鸡蛋', '32', '43');
INSERT INTO `good` VALUES ('23', '西红柿', '32', '43');
还有就是javaweb中的展示页面,因为都是jsp所以都一样,所以不改了
我们把index.jsp换成我们的这个
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
学习日记
一步步来,开始Good.java
package com.xuexiriji.pojo;
public class Good {
private int goodId;
private String goodName;
private int surplus;
private int goodPrice;
public int getGoodId() {
return goodId;
}
public void setGoodId(int goodId) {
this.goodId = goodId;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public int getSurplus() {
return surplus;
}
public void setSurplus(int surplus) {
this.surplus = surplus;
}
public int getGoodPrice() {
return goodPrice;
}
public void setGoodPrice(int goodPrice) {
this.goodPrice = goodPrice;
}
public Good(int goodId, String goodName, int surplus, int goodPrice) {
super();
this.goodId = goodId;
this.goodName = goodName;
this.surplus = surplus;
this.goodPrice = goodPrice;
}
public Good() {
super();
}
@Override
public String toString() {
return "Good [goodId=" + goodId + ", goodName=" + goodName + ", surplus=" + surplus + ", goodPrice=" + goodPrice
+ "]";
}
}
mapper接口:goodDao.java
package com.xuexiriji.dao;
import com.xuexiriji.pojo.Good;
import java.util.List;
public interface GoodDao {
public List<Good> selectAllGood();
}
GoodMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xuexiriji.dao.GoodDao">
<select id="selectAllGood" resultType="Good">
select * from good
select>
mapper>
service这里我就不面向接口了:GoodService
@Service
public class GoodService {
@Autowired
GoodDao GoodDao;
public List<Good> selectAllGood(){
return GoodDao.selectAllGood();
}
}
这里我们看好,只要是被spring接管的都是有小叶子的,而且点击都可以跳过去,如果你没有跳过去,那就是没接管,项目都不用运行,可以去找找错误了
GoodController
@Controller
public class GoodController {
@Autowired
GoodService goodService;
@RequestMapping("/selectAllGood")
public String selectAllGood(Model model){
List<Good> goods = goodService.selectAllGood();
model.addAttribute("goods",goods);
return "index";
}
}
启动我们的项目,一下去写这么多中间都没有测试,害,希望不要报错一大堆吧,没有报错启动成功:
果然不出我所料必须报错,说没有这个类,其实看到我就想到,还记得之前启动web项目时候说的吗,lib依赖可能不被加进去,所以我们还得重新打下包
这样这些包就有了
重新启动,欧克,成功启动
点击登录,报错:
这里肯定就是我们的mapper和接口有问题,看下我们的配置,应该是这样的,因为这次我们类名没有起的一直,那就不改了,添加一个mapper.xml记得来这里注册就行
Ok,测试搞定
还可以哈,我差不多,这是我第三次吧,整合这个,其实第一整合完,第二次也就是复制粘贴,这次差不多也是复制的第一次的,所以你自己整合好的一个项目,留好,可能这是你后面项目的基底。
这里打算给大家整合做一个小例子的,今天就先整合到这里吧,等后面再给大家做出来,看到这里了,还不点个赞吗,留个关注也行呀,bye~