选择本地安装的maven和对应的setting.xml。
如果没有配置archetypeCatalog这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。
org.springframework
spring-webmvc
5.0.8.RELEASE
org.springframework
spring-jdbc
5.0.8.RELEASE
org.aspectj
aspectjrt
1.8.12
org.aspectj
aspectjweaver
1.8.12
mysql
mysql-connector-java
5.1.27
com.alibaba
druid
1.1.10
将java文件夹设置为Source Root
将resources文件夹设置为Resources Root
同上创建spring-servlet.xml
接下来配置web.xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
springmvc
/
package com.cxb.ssm.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String hello(){
return "hello springMvc!";
}
}
package com.cxb.ssm.controller;
import com.cxb.ssm.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(){
return helloService.hello();
}
}
启动成功会有下面的页面出现,这就是index.jsp
测试访问helloController http://localhost:8081/ssm_war/hello
自此spring、springmvc配置完成。
**********************************************************************************************************************************
**********************************************************************************************************************************
javax.servlet
javax.servlet-api
4.0.1
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
jstl
jstl
1.2
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEcoding
true
forceResponseEconding
true
encodingFilter
/*
package com.cxb.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/helloJsp")
public String index(){
return "hello";
}
}
Hello JSP!
http://localhost:8081/ssm_war/helloJsp
JSP整合成功!
*****************************************************************************************************
*****************************************************************************************************
org.mybatis
mybatis
3.4.6
org.mybatis
mybatis-spring
1.3.2
url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver
filters=stat
maxActive=20
initialSize=1
maxWait=60000
minIdle=10
maxIdle=15
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
maxOpenPreparedStatements=20
removeAbandoned=true
removeAbandonedTimeout=1800
logAbandoned=true
classpath:db.properties
classpath*:/mybaties/*/*.xml
-- --------------------------------------------------------
-- 主机: 127.0.0.1
-- 服务器版本: 5.6.40 - MySQL Community Server (GPL)
-- 服务器操作系统: Win64
-- HeidiSQL 版本: 8.2.0.4675
-- --------------------------------------------------------
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-- 导出 ssm 的数据库结构
CREATE DATABASE IF NOT EXISTS `ssm` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `ssm`;
-- 导出 表 ssm.book 结构
CREATE TABLE IF NOT EXISTS `book` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '图书ID',
`name` varchar(100) NOT NULL COMMENT '图书名称',
`stock` int(11) NOT NULL COMMENT '馆藏数量',
`remark` varchar(50) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1004 DEFAULT CHARSET=utf8 COMMENT='图书表';
-- 正在导出表 ssm.book 的数据:~1 rows (大约)
DELETE FROM `book`;
/*!40000 ALTER TABLE `book` DISABLE KEYS */;
INSERT INTO `book` (`id`, `name`, `stock`, `remark`) VALUES
(1001, '数据库设计', 100, '人民出版社'),
(1002, '面向对象程序设计', 260, '重庆出版社'),
(1003, '高并发的处理', 500, '忠县出版社');
/*!40000 ALTER TABLE `book` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
Book:
package com.cxb.ssm.vo;
public class Book {
private Integer id;
private String name;
private Integer stock;
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", stock=" + stock +
", remark='" + remark + '\'' +
'}';
}
}
BookMapper:
package com.cxb.ssm.dao;
import com.cxb.ssm.vo.Book;
import java.util.List;
public interface BookMapper {
List getAllBook();
}
BookService:
package com.cxb.ssm.service;
import com.cxb.ssm.vo.Book;
import java.util.List;
public interface BookService {
List getAllBook();
}
BookServiceImpl:
package com.cxb.ssm.service.impl;
import com.cxb.ssm.dao.BookMapper;
import com.cxb.ssm.service.BookService;
import com.cxb.ssm.vo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List getAllBook() {
return bookMapper.getAllBook();
}
}
BookController:
package com.cxb.ssm.controller;
import com.cxb.ssm.service.BookService;
import com.cxb.ssm.vo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/getAllBook")
public String getAllBook(Model mod){
List books = bookService.getAllBook();
mod.addAttribute("books",books);
return "book";
}
}
bookMapping.xml:
book.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
Book List
库存
名称
备注
${book.stock}
${book.name}
${book.remark}
http://localhost:8081/ssm_war/getAllBook
数据查询成功,自此spring、springmvc、mybatis、jsp整合完成!
项目下载:https://github.com/ChenXbFrank/ssm-demo