工具:
1. IntelliJ IDEA Ultimate 2019.2.4
2. apache-tomcat-9.0.17
3. JDK 1.8.0_221
4. MySQL数据库
5. apache-maven-3.6.3
一、创建项目结构:
二、开始搭建框架
1.导入依赖(引入了lombok 依赖,减少 getter、setter 等方法)
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
org.projectlombok
lombok
1.18.16
2.创建总的配置文件: applicationContext.xml
(由于spring的配置过多,我们在这里分三层,分别是spring-dao.xml、spring-service.xml、spring-mvc.xml)
3.在资源文件resources中创建:spring-dao.xml
(1).关联数据库文件
(2).配置数据库连接池,以及连接池属性
(3).配置配置c3p0
(4).配置SqlSessionFactory对象(mybatis)
(5).配置扫描Dao接口包,动态实现Dao接口注入到spring容器中
4.在资源文件resources中创建:spring-service.xml
(1).扫描service相关的bean
(2).将service层的实现类注入到IOC容器中
(3).配置事务管理器,把事务管理交由spring来完成
(4).注入数据库连接池
5.接着在资源文件中创建:spring-mvc.xml
(1).开启SpringMVC注解驱动,如:@Qualifier,@RequestMapping,@Data … …
(2).对静态资源处理,如 js,css,
(3).配置jsp显示ViewResolver视图解析器,如:在controller层中某个方法返回一个string类型的"queryAll",实际上会返 回"/WEB-INF/queryAll.jsp"
(4).扫描web层 @Controller
6.创建一个 database.properties 存放4个最常见的数据库连接属性
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dbname?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=x5
7.开始配置mybatis核心文件:mybatis-config.xml
8.resources下新建一个名字叫mappper的文件夹,并在mapper文件夹下新建 StudentDao.xml 配置文件
(对数据库进行增、删、改、查 等操作)
9.添加实体类 Student.java
(这里引入lombok,用@Data注解简化了代码)
package com.zdq.pojo;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String gender;
private String address;
}
10.创建数据库dbname,表名为student
-- --------------------------------------------------------
-- 主机 :127.0.0.1
-- 服务器版本 :5.0.67-community-log - MySQL Community Edition (GPL)
-- 服务器操作系统 :Win32
-- HeidiSQL 版本 :7.0.0.4278
-- 创建 :2021-04-04 13:42:37
-- --------------------------------------------------------
/*!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' */;
-- 导出 dbname 的数据库结构
DROP DATABASE IF EXISTS `dbname`;
CREATE DATABASE IF NOT EXISTS `dbname` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `dbname`;
-- 导出 表 dbname.student 结构
DROP TABLE IF EXISTS `student`;
CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(10) default NULL,
`gender` varchar(5) default NULL,
`address` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;
-- 正在导出表 dbname.student 的数据: ~2 rows ((大约))
DELETE FROM `student`;
/*!40000 ALTER TABLE `student` DISABLE KEYS */;
INSERT INTO `student` (`id`, `name`, `gender`, `address`) VALUES
(1001, '李四', '男', '北京大兴区'),
(1002, 'zhangsan', '男', '武汉市');
/*!40000 ALTER TABLE `student` 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 */;
11.在com.zdq.controller包下创建controller控制器:StudentController.java
package com.zdq.controller;
import com.zdq.pojo.Student;
import com.zdq.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
@Qualifier("StudentServiceImpl")
private StudentService studentService;
@RequestMapping("/quaryall")
public String list(Model model){
List list = studentService.queryAll();
model.addAttribute("list",list);
return "queryAll";
}
}
12.创建业务逻辑层接口:StudentService.java
package com.zdq.service;
import com.zdq.pojo.Student;
import java.util.List;
public interface StudentService {
//查询全部,返回list集合
public List queryAll();
}
13.书写业务逻辑层实现类:StudentServiceImpl.java
package com.zdq.service.impl;
import com.zdq.dao.StudentDao;
import com.zdq.pojo.Student;
import com.zdq.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public List queryAll() {
return studentDao.queryAll();
}
}
14.创建数据访问层接口:StudentDao.java
package com.zdq.dao;
import com.zdq.pojo.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentDao {
// @Select("select * from student")
public List queryAll();
}
15.修改web.xml文件,加载总配置文件 applicationContext.xml
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
DispatcherServlet
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
20
16.添加:index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
Hello World!
查询
17.添加查询全部页面:queryAll.jsp
(注意:在WEB-INF目录下新建一个show文件夹)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
学号
名字
性别
家庭住址
${student.getId()}
${student.getName()}
${student.getGender()}
${student.getAddress()}
18.到目前为止,所有的配置文件和java代码就写完了。