准备工作

IDEA 2019.3.1

MySql 8.0.17

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所需的包,配置如下:

<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency> <!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency> </dependencies>

解决资源文件的依赖问题:

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

三、代码编写

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<Student> findAllStudent(); int deleteStudent(int id); int updateStudent(Student student); int addStudent(Student student);
}

3.在dao中添加StudentDao.xml 映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Tony.dao.StudentDao">
<select id="findStudentById" parameterType="int" resultType="com.Tony.entities.Student">
select * from test.student where id=#{id}
</select> <select id="findAllStudent" resultType="com.Tony.entities.Student">
select * from test.student
</select> <delete id="deleteStudent" parameterType="int">
delete from test.student where id=#{id}
</delete> <update id="updateStudent" parameterType="com.Tony.entities.Student">
update test.student set name=#{name},age=#{age},detail=#{detail} where id=#{id}
</update> <insert id="addStudent" parameterType="com.Tony.entities.Student">
insert into test.student(name,age,detail) values
(#{name},#{age},#{detail})
</insert>
</mapper>

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<Student> 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<Student> 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<Student> 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" %>
<html>
<head>
<title>显示所有的学生</title>
</head>
<body>
<table>
<thead>
<tr>
<td>学生ID</td>
<td>学生姓名</td>
<td>学生年龄</td>
<td>学生明细</td>
</tr>
</thead>
<tbody>
<c:forEach var="student" items="${requestScope.get('list')}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.detail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>

四、配置文件

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.Tony.dao"></property>
</bean>
</beans>
3.spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.Tony.service"></context:component-scan>
</beans>
4.spring-controller.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描@controller注解-->
<context:component-scan base-package="com.Tony.controller"></context:component-scan> <!--@RequestMapping生效-->
<mvc:annotation-driven></mvc:annotation-driven> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
5.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-controller.xml"></import>
<import resource="classpath:spring-dao.xml"></import>
<import resource="classpath:spring-service.xml"></import>
</beans>
6.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

最后,整个项目的目录结构如下:

五、配置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

IDEA spring mvc整合mybatis的更多相关文章

  1. spring MVC(十)---spring MVC整合mybatis

    spring mvc可以通过整合hibernate来实现与数据库的数据交互,也可以通过mybatis来实现,这篇文章是总结一下怎么在springmvc中整合mybatis. 首先mybatis需要用到 ...

  2. Spring MVC整合Mybatis 入门

    本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spri ...

  3. spring mvc整合mybaitis和log4j

    在上一篇博客中,我介绍了在mac os上用idea搭建spring mvc的maven工程,但是一个完整的项目肯定需要数据库和日志管理,下面我就介绍下spring mvc整合mybatis和log4j ...

  4. MyBatis+Spring+Spring MVC整合开发

    MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...

  5. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  6. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  7. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  8. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  9. Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)

    Spring.Spring MVC.MyBatis整合(未万待续)

随机推荐

  1. R - Fence Repair POJ - 3253

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  2. CocosCreator内存与性能优化

    一.内存优化 因为 iOS小游戏和微信共用同一个进程,而微信在连续两次收到系统内存警告的时候会关闭小游戏并释放小游戏占用的内存.如果你的小游戏有外网用户反馈“闪退”,或者你自己测试的时候频繁出现“该小 ...

  3. SAM(后缀自动机)总结

    “写sam是肯定会去写的,这样才学的了字符串,后缀数组又不会用 >ω<, sam套上数据结构的感觉就像回家一样! 里面又能剖分又能线段树合并,调试又好调,我爱死这种写法了 !qwq” SA ...

  4. 「Luogu P5368 [PKUSC2018]真实排名」

    PKUSC签到题 题目大意 给出一个长度为 \(N\) 的序列,序列中有 \(K\) 个数会乘二,对于每个数计算在乘二后大于等于这个数的个数与乘二前没有发生变化的方案数. 分析 思路很清晰,可以将答案 ...

  5. 「luogu3402」【模板】可持久化并查集

    「luogu3402」[模板]可持久化并查集 传送门 我们可以用一个可持久化数组来存每个节点的父亲. 单点信息更新和查询就用主席树多花 一个 \(\log\) 的代价来搞. 然后考虑如何合并两个点. ...

  6. Python编程使用PyQT制作视频播放器

    最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放 ...

  7. 小白学 Python 爬虫:Selenium 获取某大型电商网站商品信息

    目标 先介绍下我们本篇文章的目标,如图: 本篇文章计划获取商品的一些基本信息,如名称.商店.价格.是否自营.图片路径等等. 准备 首先要确认自己本地已经安装好了 Selenium 包括 Chrome ...

  8. [转]Mysql连表之多对多

    转自 回到顶部 连表多对多 可以理解成一夫多妻和一妻多夫. 男人表: nid name 1 xxx 2 yyy 3 zzz 女人表: nid name 1 aaa 2 bbb 3 ccc 要让两个表建 ...

  9. [转]BeanUtil使用

    BeanUtils的使用 转载自:https://blog.csdn.net/xxf159797/article/details/53645722 1.commons-beanutils的介绍 com ...

  10. 洛谷P1091合唱队形(DP)

    题目描述 NNN位同学站成一排,音乐老师要请其中的(N−KN-KN−K)位同学出列,使得剩下的KKK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2, ...