SSM搭建
SSM搭建
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。.
<br>
<a href="">SpringIoc</a>
·
<a href="">SpringMVC</a>
·
<a href="">Mybatis</a>
Table of contents
环境
- jdk8
- tomcat8
- maven
- IDEA
- win7
搭建
导入web工程依赖
将基本的web工程的依赖导入
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!--没有这个依赖会报错-->
<!--java.lang.NoClassDefFoundError:org/springframework/dao/support/DaoSupport-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
导入spring工程依赖
将基本的spring工程所需要的依赖导入
springmvc依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
基本包结构
先搭建基本包的基本结构
一般来说是在src/main/java包下新建出一个能代表你和当前项目的包的名字,比如说可是是com.selton.hellossm,
然后在这个包下面,
新建controller包,
controller包用来直接对接前端,
新建dao包,
dao包用来从数据库获取数据,
新建service包,
主要的业务逻辑需要在这里体现,
service包会调用dao层,然后提供给controller使用,
新建entities,
用来存放数据库的实体,
新建util包,
用来存放工具类,
新建constant包
用来存放一般常量
配置文件
接下来就是配置resource里的配置文件
首先是数据源连接池的配置
1.c3p0数据源连接池配置
mysql5
导入mysql5依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!--没有这个依赖会报错-->
<!--PropertyAccessException1:org.springframework.beans.MethodInvocationException:-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
(后面不提,都是在resources下)新建文件
导入依赖
com.mchange
c3p0
0.9.5.2
c3p0-config.properties
c3p0.driverClassName=com.mysql.jdbc.Driver
c3p0.url=jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=UTF-8
c3p0.username=root
c3p0.password=123456
c3p0.maxActive=50
c3p0.maxIdle=10
c3p0.minIdle=5
c3p0.initialSize=10
c3p0.maxWait=5000
c3p0.minPoolSize=10
接着将数据源连接池注入给mybatis
导入依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
新建spring-mybatis.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:task="http://www.springframework.org/schema/task"
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-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--构建一个C3P0数据源连接池对象-->
<bean id="id_ds_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${c3p0.driverClassName}"></property>
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="user" value="${c3p0.username}"></property>
<property name="password" value="${c3p0.password}"></property>
<property name="maxPoolSize" value="${c3p0.maxActive}"></property>
<property name="initialPoolSize" value="${c3p0.initialSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
</bean>
<!--配置SqlSessionFactory-->
<bean name="sqlSessionFactory" id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="id_ds_c3p0"></property>
<!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
</bean>
<!--有了这个配置,我们就指明了我们的Mapper们,即Dao们,都在哪个包
也能使用注解了
同时不用写Dao的实现类了
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage的值如果没有对呀好包,会报错-->
<!--PropertyAccessException 1: org.springframework.beans.MethodInvocationException:-->
<property name="basePackage" value="com.selton.hellossm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
新建springmvc-config.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">
<!--springmvc 只管扫描 controller包-->
<context:component-scan base-package="com.selton.hellossm.controller"></context:component-scan>
<!-- 让spring-mvc支持注解 -->
<mvc:annotation-driven>
</mvc:annotation-driven>
</beans>
使用
这时完成了后台的配置,让我们实现一个简单地登录系统
用基本maven项目搭建出来的工程骨架里没有webapp
我们需要在src/main下新建文件夹webapp
当然不需要手动创建
idea有自动化的功能 ---链接
在webapp下新建loginfailed.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
login failed
</body>
</html>
新建loginsuccess.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
login success
</body>
</html>
新建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="get">
name: <input type="text" name="name">
<br>
password: <input type="password" name="password">
<br>
<input type="submit" value="登录">
</form>
</body>
</html>
在web.xml中写入
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
version="3.1">
<!-- 这个全局参数的作用是:加载Spring-config的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<!--Spring的一个核心监听器,对Spring容器进行初始化动作
即我们以前的ApplicationContext context=new ClassPathXml.....
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的核心Servlet-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
<!-- 处理乱码-->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
完成所有的配置工作后
开始写后台代码
在数据库中新建
/*
Navicat Premium Data Transfer
Source Server : link1
Source Server Type : MySQL
Source Server Version : 50622
Source Host : localhost:3306
Source Schema : db_test1
Target Server Type : MySQL
Target Server Version : 50622
File Encoding : 65001
Date: 10/07/2018 11:22:41
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
加入lombok依赖----链接
不使用lombok的话,去掉@Data
然后自己添上无参构造,等各种get,set
在entities中新建实体类User
@Data
public class User {
private int id;
private String name;
private String password;
private int age;
}
在dao中新建UserDao
@Repository
public interface UserDao {
@Select("SELECT password FROM user WHERE name = #{name}")
String getUserByNameAndPassword(@Param("name") String name, @Param("password") String password);
}
service包中用来存放接口
在service下新建包serviceimpl,该包下存放service包中接口的实现类
service中新建接口类UserService
public interface UserService {
boolean loginUserStatus(String name,String password);
}
在serviceimpl包下新建UserServiceImpl类实现UserService接口
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public boolean loginUserStatus(String name, String password) {
if(name == null || "".equals(name)){
return false;
}
if(password == null || "".equals(password)){
return false;
}
String passwordByName = userDao.getPasswordByName(name);
System.out.println("passwordByName = " + passwordByName);
if (password == null){
return false;
}
if (password.equals(passwordByName)) {
return true;
}
return false;
}
}
controller下新建类UserController
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("userLogin")
public String userLogin(String name,String password){
if (userService.loginUserStatus(name,password)) {
return "/loginsuccess.html";
}
return "/loginfailed.html";
}
}
在index.html的form表单的action中写入
/userLogin/userLogin.php
写入form表单中的数据将会提交到这个controller中
运行,tomcat自行配置,记得将war配置到tomcat中,以及pom中packing war
SSM搭建的更多相关文章
- idea+maven+ssm搭建boot_crm项目遇到的问题
使用idea+maven+ssm搭建一个boot_crm项目,遇到的问题如下: 1.环境搭建好了,相关配置文件都配置好了,也部署到了tomcat,但是无法启动首页. 通过百度,google找到了,原因 ...
- SSM搭建Spring单元测试环境
原文链接:https://jingyan.baidu.com/article/93f9803f5a97a4e0e46f55c8.html SSM搭建Spring单元测试环境
- Maven+SSM搭建总结(非教程)
记录我用Maven搭建Spring+SpringMVC+Mybatis项目的过程. 网上关于这个的详细教程有很多,但是优质而适合自己看的需要自己筛选以下我看过的几篇认为讲的比较详细的资源(照着做吧,做 ...
- ssm搭建报错
在搭建ssm框架时候踩得坑:1.对于拦截器url-parttern的设置:第一次设置的是/** 本以为这个是表示拦截所有,没想到这是错误的写法,正确的写法是/ 启动项目不会报错,但是会出现404 ...
- SSM 搭建精美实用的管理系统
课程介绍 SSM 框架即 SpringMVC+Spring+Mybatis,相信各位朋友在投递简历时已直观感受到它的重要性,JavaWeb 相关工作的招聘要求中基本都包括了这三项技术能力. 由于其轻量 ...
- ssm搭建相关的问题
在搭建ssm框架时候踩得坑:1.对于拦截器url-parttern的设置:第一次设置的是/** 本以为这个是表示拦截所有,没想到这是错误的写法,正确的写法是/ 启动项目不会报错,但是会出现404 ...
- 使用SSM搭建一个简单的crud项目
使用SSM完成增删查改 前端使用到的技术:ajax,json,bootstrap等 完整项目地址:点这里GitHub 项目地址,可以在线访问 这一章节主要搭建SSM的环境. SpringMVC Spr ...
- eclipse +maven+ssm搭建矿建
记录一下搭建框架的过程1.下载最新的eclipse https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R/ec ...
- SSM搭建一个后台管理系统
看一下效果图: 登陆界面: 图片上传页面: 我也把项目放到服务器上了,可以直接查看项目内容: http://codingcoge.cn/ssm-demo/login.html 1 我也放到github ...
随机推荐
- centos7虚拟机安装elasticsearch6.4.x-遇到的坑
OS:Centos7x虚拟机 1H2Gjdk:1.8elasticsearch:5.6.0 1.下载“elasticsearch-5.6.0.tar.gz”解压到/usr/local/elastics ...
- 06-SSH综合案例:前台首页访问
1.5 编码实现: 1.5.1 首页显示: 复制所有文件到工程下: * css * js * image 复制页面到工程WEB-INF/jsp/ * 将后缀.htm改为jsp 访问一个Actio ...
- unity5-GI是什么?
GI是什么? GI =直接光照+间接光照+环境光+反射光.直接光照先不用说了,间接光照是光线在物体上反射所带来的光照. 核心参数: 每个光源上的Bounce Intensity.环境光可以直接理解为你 ...
- 单词的添加与查找 · Add and Search Word
[抄题]: 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词 ...
- oracle触发器--if else demo
CREATE OR REPLACE Trigger trig_solr_index_el_lesson After Update of lessonid, lessonname, lessongoal ...
- Laravel 多态关联使用的案例
1.实现的功能,:短信发送,需要签名和模板审核,审核结果要插进审核记录表 2,在signature(签名表模型)和 template(模板表模型)添加多态对应关系代码其实一样,代码如下: 审核记录表需 ...
- Apache 调用不同的 PHP 版本
# cd /etc/apache2/mods-enabled 建立 /etc/apache2/mods-available/ 之下的 php7.2.load php7.2.conf 两个文件的符号 ...
- 利用NotePad++ 格式化代码(格式标准化) worldsing
在阅读别人的代码时往往会遇到格式很乱,阅读起来很费劲,如果手动改很容易出错,而且很费时间,这时可以借助一些专业的编辑器来格式化代码,NotePad++是一个轻量级的代码编辑器,占用内存少,运行速度快, ...
- Android登录模块原理及实现
1.需要保持登录状态,在本地保存登录的状态信息. 2.界面布局 3.登录流程 输入信息=>登录成功=>手机号发送验证码=>输入验证码=>登录成功
- nodejs顺序执行shell
最近工作中需要用到nodejs编写脚本来顺序执行自动化测试用例,编写代码如下: var runCommand = function (command){ child_process.exec(comm ...