本篇博客参考自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral
首先来了解一下mybatis:
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。(来源百度百科)
首先建一个springmvc项目(建议使用STS,STS使用与eclipse一致)
STS下载
创建springmvc项目步骤
https://jingyan.baidu.com/article/358570f6af5fe0ce4724fca5.html
项目视图
在pom.xml复制下列jar包(这是从maven官网直接下载jar包,无需自己寻找jar包,下载需要一段时间建议到网络好的地方下载)
4.0.0
com.zjq
ssm
ssm
war
1.0.0-BUILD-SNAPSHOT
5.1.5.RELEASE
1.6.6
1.2.12
1.8.1
org.springframework
spring-test
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.quartz-scheduler
quartz
1.8.4
org.mybatis
mybatis
3.3.1
org.mybatis
mybatis-spring
1.2.4
mysql
mysql-connector-java
5.1.35
junit
junit
4.11
test
com.alibaba
druid
1.0.17
org.aspectj
aspectjweaver
1.8.8
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.2
log4j
log4j
1.2.17
com.alibaba
fastjson
1.2.22
org.mybatis.generator
mybatis-generator-core
1.3.2
javax.servlet.jsp
jsp-api
2.1
provided
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
org.codehaus.jackson
jackson-core-lgpl
${jackson.version}
org.codehaus.jackson
jackson-mapper-lgpl
${jackson.version}
jstl
jstl
1.2
taglibs
standard
1.1.2
commons-codec
commons-codec
1.9
com.belerweb
pinyin4j
2.5.0
ssm
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
5.1.35
src/main/resources/generator.xml
true
true
在src/main/java处创建包名
其次在src/main/resources处配置xml文件(注意中间的root-context.xml与servlet-context.xml文件我是从下面拉上来的)
ApplicationContext.xml(此文件里有许多包名,建议使用Ctrl+F一键替换避免遗漏)
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=账号
jdbc.password=密码
servlet-context.xml
HomeController.java
package com.zjq.ssm.controller;
import java.util.List;
import javax.annotation.Resource;
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 com.zjq.ssm.entity.Book;
import com.zjq.ssm.service.BookService;
@Controller
@RequestMapping("/Book")
public class HomeController {
@Resource
@Autowired
BookService bookService;
@RequestMapping("/getAllBook")
public String getAllBook(Model model) {
List books = bookService.getAllBook();
for (Book book : books) {
System.out.println(book.getBookName());
}
model.addAttribute("books", books);
return "success";
}
}
BookDao.java
package com.zjq.ssm.dao;
import java.util.List;
import com.zjq.ssm.entity.Book;
public interface BookDao {
public List getAllBook();
}
Book.java
package com.zjq.ssm.entity;
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String bookName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
@Override
public String toString() {
return "id:" + getId() + ",bookName:" + getBookName() ;
}
}
BookServiceImpl.java
package com.zjq.ssm.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zjq.ssm.dao.BookDao;
import com.zjq.ssm.entity.Book;
import com.zjq.ssm.service.BookService;
@Service
public class BookServiceImpl implements BookService{
@Resource
BookDao bookDao;
@Override
public List getAllBook() {
return bookDao.getAllBook();
}
}
BookMapper.xml
BookService.java
package com.zjq.ssm.service;
import java.util.List;
import com.zjq.ssm.entity.Book;
public interface BookService {
public List getAllBook();
}
web.xml
contextConfigLocation
classpath:root-context.xml,classpath:ApplicationContext.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:servlet-context.xml
1
appServlet
/
此处记得添加
创建数据库(id为递增)
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
跳转
success.xml
<%@ 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" %>
图书管理