SSM搭建
SSM搭建
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。.
SpringIoc
·
SpringMVC
·
Mybatis
Table of contents
环境
- jdk8
- tomcat8
- maven
- IDEA
- win7
搭建
导入web工程依赖
将基本的web工程的依赖导入
导入spring工程依赖
将基本的spring工程所需要的依赖导入
springmvc依赖
基本包结构
先搭建基本包的基本结构
一般来说是在src/main/java包下新建出一个能代表你和当前项目的包的名字,比如说可是是com.selton.hellossm,
然后在这个包下面,
新建controller包,
controller包用来直接对接前端,
新建dao包,
dao包用来从数据库获取数据,
新建service包,
主要的业务逻辑需要在这里体现,
service包会调用dao层,然后提供给controller使用,
新建entities,
用来存放数据库的实体,
新建util包,
用来存放工具类,
新建constant包
用来存放一般常量
配置文件
接下来就是配置resource里的配置文件
首先是数据源连接池的配置
1.c3p0数据源连接池配置
mysql5
导入mysql5依赖
(后面不提,都是在resources下)新建文件
导入依赖
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
导入依赖
新建spring-mybatis.xml
新建springmvc-config.xml
<context:component-scan base-package="com.selton.hellossm.controller"></context:component-scan>
</mvc:annotation-driven>
使用
这时完成了后台的配置,让我们实现一个简单地登录系统
用基本maven项目搭建出来的工程骨架里没有webapp
我们需要在src/main下新建文件夹webapp
当然不需要手动创建
idea有自动化的功能 ---链接
在webapp下新建loginfailed.html
login failed
新建loginsuccess.html
login success
新建index.html
在web.xml中写入
完成所有的配置工作后
开始写后台代码
在数据库中新建
/*
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
浙公网安备 33010602011771号