网上有很多ssm框架的教程,特此总结。
idea 2017.1
jdk1.8
Maven 3.3.9
一、首先使用idea新建一个Maven webapp项目
关于maven,我有一篇初识maven的博客,可以参考:
https://blog.csdn.net/jjs15259655776/article/details/77096083
二、搭建目录结构
我这里列出的是搭建完了之后所有的目录和文件,诸位先把目录文件建起来,然后我在给出文件内容
这里的目录建好之后还需要设置一下,让idea识别目录作用,选择File-Project Structure
设置完成后ok
三、配置文件内容
pom.xml文件内容:这是maven管理依赖的地方,也是maven被认可的最大原因。
4.0.0
com.chatRobot
ChatRobot
war
1.0-SNAPSHOT
ChatRobot Maven Webapp
http://maven.apache.org
UTF-8
UTF-8
4.3.5.RELEASE
3.4.1
javax
javaee-api
7.0
junit
junit
4.12
ch.qos.logback
logback-classic
1.2.2
com.fasterxml.jackson.core
jackson-databind
2.8.7
mysql
mysql-connector-java
5.1.41
runtime
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
ChatRobot
org.apache.maven.plugins
maven-compiler-plugin
1.8
web.xml:这个配置总不能少吧。
ChatRobot
ChatRobot_Alpha_0.0.1
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-*.xml
1
true
SpringMVC
/
index.html
logback.xml:用于记录
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
jdbc.properties:数据库的配置信息,这个并不难,根据自己的情况修改一下username和password吧少年,另外记得建数据库,然后把名字填上。
jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=xxxx
#密码
jdbc.password=xxxxx
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2
spring-mybatis.xml:只是把mybatis整合到spring的配置。
spring-mvc.xml:springMVC的配置,说实话,这个我也不是很熟。缝(网)缝(上)补(搜)补(搜),还有很多关于springMVC的tips,需要你在使用中,慢慢的接受。
但是其实,不过不测试,会发现很多问题,在测试过程中,慢慢调整,就更能够熟练掌握ssm
四、测试
控制器:UserController
package com.chatRobot.controller;
import javax.servlet.http.HttpServletRequest;
import com.chatRobot.model.User;
import com.chatRobot.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/showUser.do")
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
long userId = Long.parseLong(request.getParameter("id"));
User user = this.userService.selectUser(userId);
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(user));
response.getWriter().close();
}
}
Dao层:UserDao
package com.chatRobot.dao;
import com.chatRobot.model.User;
public interface UserDao {
User selectUser(long id);
}
Model层:User
package com.chatRobot.model;
import java.util.Date;
public class User {
private long id;
private String email;
private String password;
private String username;
private String role;
private int status;
private Date regTime;
private String regIp;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public String getRegIp() {
return regIp;
}
public void setRegIp(String regIp) {
this.regIp = regIp;
}
}
Service层:UserService
package com.chatRobot.service;
import com.chatRobot.model.User;
public interface UserService {
public User selectUser(long userId);
}
实现层:UserServiceImpl 这里是实现逻辑的地方。
package com.chatRobot.service.impl;
import com.chatRobot.dao.UserDao;
import com.chatRobot.model.User;
import com.chatRobot.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
public User selectUser(long userId) {
return this.userDao.selectUser(userId);
}
}
Mybaticd的配置文件:UserDao.xml
继续新建个页面来测试springmvc和mybatis
index.html
test
Hello World!
ps:如果不知道怎么运行的骚年,可以搜一搜网上很多滴~~, 我就在这里提一嘴:仔细看 IDEA 下面的提示信息,然后点 fix it
fix it fix it 没错IDEA就是这么强。