第一次配置ssm框架,记录期间遇到的坑和相关的解决方案
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190627220503255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4MjM5ODE3,size_16,color_FFFFFF,t_70
File–>New–>project–>Maven–>勾选Create from archetype–>选择maven-archetype-webapp(注意选对)
再点next–>finish创建即可
2.在新建的项目中添加所需要的文件和文件夹(可以看前面的目录结构去新建即可,这里主要提出额外需要设置的目录)
在main文件夹下面新建java(注意要设为根目录)
设置java为根目录
在java下新建com包,在com包下新建zhongruan (这里可以根据你的意愿修改包名),在zhongruan包下新建四个包,分别命名为bean,service,dao,controller(如果出现包重叠,可以将设置按钮中的compact Middle Packages的√去掉即可)
在com.zhongruan.bean下面新建UserInfo类
UserInfo类
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
在com.zhongruan.controller下新建UserController类
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll()
{
List all =userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("userinfos",all);
mv.setViewName("allUser");
return mv;
}
}
在com.zhongruan.dao下面新建IUserDao接口类(注意是接口类)
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
List findAll();
}
在com.zhongruan.service下面新建impl包,在impl包下新建UserService类
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List findAll() {
return userDao.findAll();
}
}
在com.zhongruan.service下新建IUserService接口类
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List findAll();
}
在main下面新建resources包(注意这里需要设置下resources包)
在resources包下新建applicationContext.xml,db.properties,log4j.properties,spring-mvc.xml文件
applicationContext.xml
db.properties(记得这里数据库需要改为自己的数据库和姓名密码)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/idea?useSSL=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=bookjiang
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring-mvc.xml
在resources包下面新建mapper包,在mapper包中新建UserMapper.xml文件
执行sql语句并返回值
在webapp下面新建jsp包,并在其中建立allUser.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
user列表
在WEB-INF文件夹下修改web.xml文件(覆盖即可)
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
修改WEB-INF下index.jsp文件
Hello World!
查询
修改pom.xml文件
4.0.0
com.zhongraun
ssm062701
1.0-SNAPSHOT
war
ssm062701 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
5.0.2.RELEASE
3.2.6
1.7.7
1.2.17
0.9.5.2
1.1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.mchange
c3p0
${c3p0.version}
taglibs
standard
${taglibs.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
ssm062701
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
3.项目搭建好后,进行测试
1.tomcat启动出现缺少jsp的某个包,更换jdk得以解决
2.待更新。。。