在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下。
原文在这里:https://my.oschina.net/finchxu/blog/3007984
工作环境:
- Windows 10
- jdk8(1.8)
- IntelliJ IDEA
- spring 4 和 springMVC
- MySQL 5.7
- maven 3.3
- mybatis 3.4
- DBCP
- Tomcat 8.5
项目上传到了Github方便查看:https://github.com/finch-xu/springbook 有用的话欢迎加星。
页面演示:
首先新建项目
如图所示新建maven的webapp
建数据库和数据表
CREATE DATABASE books;
CREATE TABLE `bookadmin` (
`bid` int(11) NOT NULL AUTO_INCREMENT,
`bn` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`press` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`bid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
这里是项目目录结构
springbook
├── src
│ └── main
│ ├── java
│ │ └── cn
│ │ └── book
│ │ ├── controller
│ │ │ └── BooksController.java
│ │ ├── mapper
│ │ │ ├── BooksMapper.java
│ │ │ └── BooksMapper.xml
│ │ ├── pojo
│ │ │ └── Bookadmin.java
│ │ └── service
│ │ ├── BooksServiceImpl.java
│ │ └── BooksService.java
│ ├── resources
│ │ ├── applicationContext-dao.xml
│ │ ├── applicationContext-service.xml
│ │ ├── applicationContext-trans.xml
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── spring-mvc.xml
│ │ └── sqlMapConfig.xml
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ ├── jsp
│ │ ├── listBooks.jsp
│ │ ├── savepage.jsp
│ │ └── updatepage.jsp
│ └── web.xml
pom.xml 找不到的包可以去https://mvnrepository.com/ 这里找,很方便的。
4.0.0
cn.book
spring
1.0-SNAPSHOT
war
spring Maven Webapp
http://www.example.com
UTF-8
UTF-8
4.3.19.RELEASE
5.2.4.Final
1.7.7
3.3.2
2.4
1.9
1.3.1
1.9.1
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.29
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
taglibs
standard
1.1.2
jar
javax.servlet
jstl
1.2
jar
javax.servlet
javax.servlet-api
3.1.0
javax.servlet.jsp
jsp-api
2.1
commons-dbcp
commons-dbcp
1.4
com.esotericsoftware.reflectasm
reflectasm
1.09
org.springframework
spring-web
4.2.6.RELEASE
org.springframework
spring-webmvc
4.2.6.RELEASE
org.springframework
spring-core
${spring.version}
commons-logging
commons-logging
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
commons-logging
commons-logging
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-jdbc
${spring.version}
javax.inject
javax.inject
1
org.aspectj
aspectjrt
1.7.4
org.aspectj
aspectjweaver
1.7.4
cglib
cglib
3.1
junit
junit
4.12
org.springframework
spring-test
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.slf4j
jcl-over-slf4j
${slf4j.version}
org.slf4j
jul-to-slf4j
${slf4j.version}
commons-lang
commons-lang
2.6
commons-io
commons-io
${commons-io.version}
commons-codec
commons-codec
${commons-codec.version}
commons-fileupload
commons-fileupload
${commons-fileupload.version}
commons-beanutils
commons-beanutils
${commons-beanutils.version}
commons-logging
commons-logging
org.projectlombok
lombok
1.16.18
provided
install
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
256M
org.apache.maven.plugins
maven-idea-plugin
2.2
true
true
org.apache.maven.plugins
maven-resources-plugin
2.4.3
org.apache.maven.plugins
maven-clean-plugin
2.4.1
activemq-data
org.apache.maven.plugins
maven-install-plugin
2.3.1
maven-remote-resources-plugin
1.1
org.apache.maven.plugins
maven-jar-plugin
2.2
org.apache.maven.plugins
maven-dependency-plugin
2.1
org.apache.maven.plugins
maven-surefire-plugin
2.5
org.apache.maven.plugins
maven-checkstyle-plugin
2.6
org.codehaus.mojo
exec-maven-plugin
1.1.1
org.apache.maven.plugins
maven-surefire-plugin
false
true
false
**/*Test.java
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
一定要注意这段内容,在maven打包war时扫描这些xml文件
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
resources
applicationContext-dao.xml 用的dbcp连接池。
applicationContext-service.xml
applicationContext-trans.xml
jdbc.properties 这里要注意应定要加上“jdbc.”的前缀。我用的是mysql5.7所以driver直接这么写就行,如果是mysql6及以上的就不一样了,具体百度吧。
如果遇到什么时区问题,记得在url后边再加上时区设置就行了。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/books?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=12345678
jdbc.maxActive=10
jdbc.maxIdle=5
spring-mvc.xml
sqlMapConfig.xml
web.xml 这里的最后一部分(有注释的那里)写的是/而不是/*,因为用了后者就把jsp也当静态文件了,访问页面直接显示源代码而不是解析jsp。
Archetype Created Web Application
index.jsp
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
springmvc-web
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
springmvc-web
/
然后就是代码:
Mapper部分:
BooksMapper.xml
bookadmin
bid,bn,author,press
INSERT INTO
bn,author,
press,
#{bn,jdbcType=VARCHAR},#{author,jdbcType=VARCHAR},
#{press,jdbcType=VARCHAR},
update
set bn = #{bn},author = #{author},press = #{press}
where bid = #{bid}
delete
from
where bid = #{bid}
BooksMapper.java
package cn.book.mapper;
import cn.book.pojo.Bookadmin;
import java.util.List;
public interface BooksMapper{
List list();
int insert(Bookadmin record);
int update(Bookadmin b);
int delete(Bookadmin bid);
Bookadmin getBookByBid(Integer bid);
}
POJO实体
Bookadmin.java
package cn.book.pojo;
public class Bookadmin {
Integer bid;
String bn;
String author;
String press;
public Integer getBid() {
return bid;
}
public void setBid(Integer bid) {
this.bid = bid;
}
public String getBn() {
return bn;
}
public void setBn(String bn) {
this.bn = bn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
}
service部分
BooksService.java
package cn.book.service;
import cn.book.pojo.Bookadmin;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface BooksService {
List list();
int insertBook(Bookadmin bookadmin);
int update(Bookadmin b);
int deleteBookByBid(Bookadmin bid);
Bookadmin getBookByBid(int bid);
}
BooksServiceImpl.java
package cn.book.service;
import cn.book.mapper.BooksMapper;
import cn.book.pojo.Bookadmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BooksServiceImpl implements BooksService {
@Autowired
private BooksMapper booksMapper;
// 列出数据
@Override
public List list(){
List list = this.booksMapper.list();
return list;
}
// 插入数据
@Override
public int insertBook(Bookadmin bookadmin){
return booksMapper.insert(bookadmin);
}
// 更新数据
@Override
public int update(Bookadmin b){
return booksMapper.update(b);
}
// 删除数据
@Override
public int deleteBookByBid(Bookadmin bid){
return booksMapper.delete(bid);
}
@Override
public Bookadmin getBookByBid(int bid){
return booksMapper.getBookByBid(bid);
}
}
Controller部分
BooksController.java
package cn.book.controller;
import cn.book.pojo.Bookadmin;
import cn.book.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/a")
public class BooksController {
@Autowired
BooksService booksService;
//列出数据表格
// 设置listBooks页面(list第一种写法)
@RequestMapping("/listBooks")
public ModelAndView listBooks(){
ModelAndView mav = new ModelAndView();
List bb = booksService.list();
mav.addObject("bb",bb);
mav.setViewName("listBooks");
return mav;
}
// list的第二种写法
// @RequestMapping("/listBooks")
// public String listBooks(Model model){
// List bb = booksService.list();
// model.addAttribute("bb",bb);
// return "listBooks";
// }
// 添加数据(两部分)
// 第一步:跳转到这里并添加图书信息,点击添加按钮就执行下边第二段代码
@RequestMapping("/addBooks0")
public String addBooks0(){
return "savepage";
}
// 第二步:把下边的页面数据返回给后端,再跳转到listBooks页面
@RequestMapping(value = "/addBooks",method = RequestMethod.POST)
public String addBooks(Bookadmin bookadmin){
booksService.insertBook(bookadmin);
return "redirect:listBooks";
}
// 修改数据(两部分)
// 第一步:更新图书,先通过bid找到图书,并列在/updatepage/{bid}页面上,
@RequestMapping("/updatepage/{bid}")
public String updatepage(@PathVariable("bid") int bid,Model model){
model.addAttribute("bookadmin",booksService.getBookByBid(bid));
return "updatepage";
}
// 第二步:然后修改即可,在这里点更新提交数据给后端
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update(Bookadmin b){
booksService.update(b);
return "redirect:listBooks";
}
// 删除图书数据
@RequestMapping("/deleteBooksByBid")
public String deleteBooksByBid(Bookadmin bid){
booksService.deleteBookByBid(bid);
return "redirect:listBooks";
}
}
JSP页面
listBooks.jsp 列出所有的图书信息(并且包含添加、修改和删除的功能按钮)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--^^^^^添加对jstl列表的支持^^^^^--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
listBooks
savepage.jsp 添加图书并返回到上边的列表页面
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加图书
updatepage.jsp 更新和修改图书信息
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
更新&修改 图书信息
编辑图书信息!
然后可以在idea里开一个tomcat来启动项目,实现简单的增删改查功能。
访问http://localhost:8080/springbook_war_exploded/a/listBooks就得到页面:
项目上传到了Github方便查看:https://github.com/finch-xu/springbook 有用的话欢迎加星。
转载需注明本文地址:https://my.oschina.net/finchxu/blog/3007984
感谢:
controller的详细讲解:https://www.cnblogs.com/pophope/p/5619504.html
https://blog.csdn.net/Peng_Hong_fu/article/details/53536862
https://www.javatpoint.com/spring-mvc-crud-example
https://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial