SSM框架搭建之传统方式
Jdk安装配置请参照此博客
eclipse配置jdk/tomcat参照此博客。
打开eclipse,点击File--New—Dynamic Web Project
点击Finish即可完成web项目的创建 按照此目录进行文件创建
1.导入如下图所示jar包
只用导入一种数据源即可,这里有c3p0、druid、dbcp三种连接池
2.datasource.properties
mysql8的驱动名称不是com.mysql.jdbc.Driver;格外注意url的写法
datasource.driverClassName=com.mysql.cj.jdbc.Driver
datasource.userName=root
datasource.userPassword=123456
datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
3. log4j.properties
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活的指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n
#mybatis显示SQL语句日志配置 net.cxp.blog.dao是你的dao接口所在的包名
#log4j.logger.org.mybatis=DEBUG
log4j.logger.com.lymn.it.mapper=DEBUG
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File=d:/logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=DEBUG
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n
4. spring-mvc.xml
5.spring-mybatis.xml
6.model--User
package com.lymn.it.model;
public class User {
private Integer userid;
private String username;
private String password;
private String email;
private String phone;
private Integer status;
private String code;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username + ", password=" + password + ", email=" + email
+ ", phone=" + phone + ", status=" + status + ", code=" + code + "]";
}
}
7.mapper-UserMapper.java
package com.lymn.it.mapper;
import java.util.List;
import com.lymn.it.model.User;
public interface UserMapper {
public List getAllUsers();
public User getUserById(int userid);
public boolean deleteUserById(int userid);
public boolean insertUser(User user);
public boolean updateUserById(User user);
}
mapper-UserMapper.java
INSERT INTO s_user(username,password,email,phone)
VALUES(#{username},#{password},#{email},#{phone})
UPDATE user SET username = #{username},
password = #{password},
email = #{email},
phone = #{phone}
WHERE
userid = #{userid}
DELETE FROM user WHERE userid = #{userid}
8.service-UserService.java
package com.lymn.it.service;
import java.util.List;
import com.lymn.it.model.User;
public interface UserService {
public List getAllUsers();
public User getUserById(int userid);
public boolean deleteUserById(int userid);
public boolean insertUser(User user);
public boolean updateUserById(User user);
}
service-UserServiceImpl.java
package com.lymn.it.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lymn.it.mapper.UserMapper;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List getAllUsers() {
return userMapper.getAllUsers();
}
@Override
public User getUserById(int userid) {
return userMapper.getUserById(userid);
}
@Override
public boolean deleteUserById(int userid) {
return userMapper.deleteUserById(userid);
}
@Override
public boolean insertUser(User user) {
return userMapper.insertUser(user);
}
@Override
public boolean updateUserById(User user) {
return userMapper.updateUserById(user);
}
}
9.controller-UserController
package com.lymn.it.controller;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
Logger logger=Logger.getLogger(UserController.class);
@RequestMapping(value="/user")
public String user(Map
10.web.xml添加如下配置
contextConfigLocation
classpath:config/spring-mybatis.xml
org.springframework.web.context.ContextLoaderListener
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
1
spring-mvc
/
org.springframework.web.util.IntrospectorCleanupListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
encodingFilter
/*
11.jsp/user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
User
${requestScope.user}
12.建数据库test,然后在创建表
CREATE TABLE `user` (
`userid` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) DEFAULT NULL,
`password` VARCHAR(255) DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`phone` VARCHAR(255) DEFAULT NULL,
`status` VARCHAR(255) NOT NULL DEFAULT '0',
`code` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=INNODB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;