准备工作
Tomcat 7.0.9
开始步骤
一、创建一个项目,添加Web支持
点击菜单:File->NEW->Project
选择左侧的Maven项目,这里的 Create from archetype先不要选择,然后点击Next
项目建好之后,目录结构如下:
在项目上右键单击,弹出菜单,选择 Add Framework Support
弹出如下界面,勾选左侧的Web Application(4.0),点击OK
点击OK之后,可以看到项目的目录结构有web文件夹了
二、项目搭建
1.数据库
新建数据库,创建一个student表,并插入几条数据
create table test.student
(
id integer auto_increment primary key ,
name varchar(50),
age int,
detail varchar(200)
)
insert into test.student(name,age,detail) values
('Tony1',18,'Tony1 is handsome');
insert into test.student(name,age,detail) values
('Tony2',19,'Tony2 is more handsome');
insert into test.student(name,age,detail) values
('Tony2',20,'Tony3 is most handsome');
2.项目目录
在项目结构的/src/main/java文件夹下创建一个包,并添加dao,service,entities,controller这四个文件夹,在/web/WEB-INF目录下添加jsp文件夹:
3.配置文件
本项目总共有7个配置文件:
web.xml:项目的配置文件
applicationContext.xml:spring总的配置文件,会引用controller/service/dao的配置文件
spring-controller.xml:controller层的配置文件
spring-service.xml:service层的配置文件
spring-dao.xml:dao层的配置文件,同时配置,mybatis的配置扫描
db.properties:数据库配置文件,被dao引用
StudentDao.xml:mybatis实体类映射文件
4.maven配置
引入springmvc ,mybatis所需的包,配置如下:
junit
junit
4.12
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.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
org.springframework
spring-context
5.1.9.RELEASE
org.projectlombok
lombok
1.18.10
解决资源文件的依赖问题:
src/main/java
**/*.properties
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
false
三、代码编写
1.在entities包中添加Student类:
package com.Tony.entities;
public class Student {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
private String name;
private int age;
private String detail;
}
2.在dao包中添加StudentDao接口:
package com.Tony.dao;
import com.Tony.entities.Student;
import java.util.List;
public interface StudentDao {
Student findStudentById(int id);
List findAllStudent();
int deleteStudent(int id);
int updateStudent(Student student);
int addStudent(Student student);
}
3.在dao中添加StudentDao.xml 映射文件
delete from test.student where id=#{id}
update test.student set name=#{name},age=#{age},detail=#{detail} where id=#{id}
insert into test.student(name,age,detail) values
(#{name},#{age},#{detail})
4.在service包中添加StudentService接口和其实现类StudentServiceImpl:
StudentService:
package com.Tony.service;
import com.Tony.entities.Student;
import java.util.List;
public interface StudentService {
Student findStudentById(int id);
List findAllStudent();
int deleteStudent(int id);
int updateStudent(Student student);
int addStudent(Student student);
}
StudentServiceImpl:
package com.Tony.service;
import com.Tony.dao.StudentDao;
import com.Tony.entities.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
public Student findStudentById(int id) {
return this.studentDao.findStudentById(id);
}
public List findAllStudent() {
return this.studentDao.findAllStudent();
}
public int deleteStudent(int id) {
return this.studentDao.deleteStudent(id);
}
public int updateStudent(Student student) {
return this.studentDao.updateStudent(student);
}
public int addStudent(Student student) {
return this.studentDao.addStudent(student);
}
}
5.在controller包中添加StudentController,并添加showAllStudent接口:
package com.Tony.controller;
import com.Tony.entities.Student;
import com.Tony.service.StudentService;
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
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/showAllStudent")
public String showAllStudent(Model model)
{
List list=studentService.findAllStudent();
model.addAttribute("list",list);
return "allStudent";
}
}
6.在/web/WEB-INF/jsp/文件夹中添加allStudent.jsp页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
显示所有的学生
学生ID
学生姓名
学生年龄
学生明细
${student.id}
${student.name}
${student.age}
${student.detail}
四、配置文件
1.db.properties
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
2.spring-dao.xml
3.spring-service.xml
4.spring-controller.xml
5.applicationContext.xml
最后,整个项目的目录结构如下:
五、配置Tomcat
点击上方的AddConfiguration按钮
弹出如下界面,点击+号,选择Tomcat Server->Local
弹出如下界面,点击fix:
配置后,这里就看得到刚配置的Tomcat服务器名了:
六、配置打包的Artifacts:
点击菜单File->Project Structure:
弹出如下界面,选择左侧的Artifacts:
在Output Layout的WEB-INF下新建一个lib文件夹(注意此处必须是lib,全部是小写,写错了会导致出各种错误):
选中lib文件夹,右键单击,弹出菜单,选择Add Copy of->Library Files:
七、运行项目:
点击如下的播放按钮运行项目,运行起来之后,IDEA会自动打开浏览器
打开浏览器之后,默认是如下的网址:
我们需要加上显示所有学生的网址,然后按回车键,就可以显示所有的学生了:
八、各种问题排查
1.不支持发行版本5:
解决办法:
点击菜单:File->Setting,弹出如下界面,选择左边的Build,Execution,Deployment->Compiler->Java Compiler,
将项目的target bytecode version从1.5改为9