ext springmvc mysql_maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral

@_@ 写在最前

之前分享过下面这几篇:

这一篇,在这些练习的基础上,将它们整合在一起!

搭建步骤如下

一、新建maven项目,配置环境,测试是否配置成功

二、整合spring+mybatis,添加ApplicationContext.xml文件,修改pom.xml,,并连接mysql,测试是否搭建成功

三、整合springmvc,添加springmvc-servlet.xml文件,修改调整,测试是否搭建成功

附:第一,二步详细操作步骤可参考

当然下面也会附上详细的搭建过程!

项目结构一览

1c0ff273cf8e645acdea4236891ac0f4.png

现在开始体验!!!

一、新建maven项目

1、新建项目,然后修改几个配置,修改完如下:

b98434550d879e8e5ce786c5036c98f5.png

62c9b46a1b4b35de8accac402b4bf06b.png

2、添加Dynamic Web Module

ade1eafb37d6a7a4c710430dd30de9c3.png

Apply后,查看项目结构可以看到多了个WebContent

f62e3832ef57f007c775a85d4982d794.png

3、将WebContent下的两个文件复制到src/main/webapp下,然后将WebContent整个删掉

6f3df7cf35aafd1e4c2bf0f1516e4f29.png

4、修改Deployment Assembly,只留下下面几个

980e7eb782da0697c4597899367714c6.png

新增/src/main/webapp,步骤Add->Floder->src->main->webapp->finish

169cb17cc4dfd5676f259e0a48645c5b.png

5、测试,在src/main/webapp下,新建一个index.jsp文件

48304ba5e6f9fe08f3fa1abda7d326ab.png

pageEncoding="UTF-8"%>

Insert title here

48304ba5e6f9fe08f3fa1abda7d326ab.png

修改web.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

index.jsp

48304ba5e6f9fe08f3fa1abda7d326ab.png

运行tomcat,访问:http://localhost:8888/SSM01/,页面显示“hello”

549e6958583fe574c1948da2a7c738c7.png

到此,第一步骤完成!!!

二、整合Spring+MyBatis

1、修改pom.xml,加载项目需要的jar

961ddebeb323a10fe0623af514929fc1.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.lmei.ssm

SSM01

0.0.1-SNAPSHOT

war

UTF-8

4.3.0.RELEASE

mysql

mysql-connector-java

5.1.38

org.apache.logging.log4j

log4j-core

2.6.1

org.mybatis

mybatis

3.4.1

junit

junit

4.10

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

org.springframework

spring-tx

4.1.4.RELEASE

org.springframework

spring-jdbc

4.1.4.RELEASE

org.springframework

spring-test

4.1.4.RELEASE

org.springframework

spring-web

4.1.4.RELEASE

org.springframework

spring-webmvc

4.1.4.RELEASE

org.aspectj

aspectjweaver

1.8.5

org.mybatis

mybatis-spring

1.3.0

javax.servlet

jstl

1.2

com.fasterxml.jackson.core

jackson-core

2.5.2

com.fasterxml.jackson.core

jackson-databind

2.5.2

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、数据库准备

新建数据表tb_book,添加测试数据

dd82fd0fc6e87419813feb983409db41.png

3、新建包和类

1de8d3333603c1685ae00f945edfa3c1.png

(1)添加实体类Book

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.entity;

public class Book {

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 this.getId()+"\t"+this.getBookName();

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

(2)添加Dao,BookDao

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.dao;

import java.util.List;

import com.lmei.ssm.entity.Book;

public interface BookDao {

public List getAllBook();

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

(3)添加service,BookService

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.service;

import java.util.List;

import com.lmei.ssm.entity.Book;

public interface BookService {

public List getAllBook();

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

(4)添加impl,BookServiceImpl实现BookService

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lmei.ssm.dao.BookDao;

import com.lmei.ssm.entity.Book;

import com.lmei.ssm.service.BookService;

@Service

public class BookServiceImpl implements BookService{

@Resource

BookDao bookDao;

public List getAllBook() {

return bookDao.getAllBook();

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

(5)添加BookMapper

48304ba5e6f9fe08f3fa1abda7d326ab.png

select * from tb_book

48304ba5e6f9fe08f3fa1abda7d326ab.png

4、添加ApplicationContext.xm和db.propertiesl文件,并注册映射

b05d0ccc2e22dbf49e7fd55ed7299683.png

ApplicationContext.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" 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

http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

value="classpath:com/lmei/ssm/mapper/*Mapper.xml">

48304ba5e6f9fe08f3fa1abda7d326ab.png

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

jdbc.username=root

jdbc.password=123456

5、添加测试类

e187ee5577d82db96896575acb73c858.png

TestMyBatisSpring

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.test;

import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lmei.ssm.dao.BookDao;

import com.lmei.ssm.entity.Book;

public class TestMyBatisSpring {

@Test

public void test() {

//初始化容器

ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");

//获得bean

BookDao bookDao = ctx.getBean(BookDao.class);

//访问数据库

List books = bookDao.getAllBook();

for (Book book : books) {

System.out.println(book.getBookName());

}

assertNotNull(books);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

Run As->JUnit Test

5439b87cf80d80b1895225f0499667f0.png

到此,第二步完成Spring+MyBatis+Mysql!!!

三、整合springmvc

1、修改实体类Book

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.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() ;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、在WEB-INF下新建一个文件夹view,然后在里面新建hello.jsp文件,用来测试springmvc是否搭建成功

路径webapp/WEB-INF/view/hello.jsp

48304ba5e6f9fe08f3fa1abda7d326ab.png

pageEncoding="UTF-8"%>

--%>

Insert title here

${message}

48304ba5e6f9fe08f3fa1abda7d326ab.png

3、新建一个controller,HelloWorld.java

9811c0c8156e26cf0834867d55823682.png

HelloWorld.java代码:

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

//@Controller是为了让Spring IOC容器初始化时自动扫描到;

//@RequestMapping是为了映射请求路径

@Controller

@RequestMapping("/Hello")

public class HelloWorld {

@RequestMapping("/SayHello")

public String SayHello(Model model) {

model.addAttribute("message", "Hello Spring MVC!");

System.out.println(model.containsAttribute("message")); //是否成功赋值

return "hello";

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

4、添加springmvc-servlet.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

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-4.3.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver"

id="internalResourceViewResolver">

48304ba5e6f9fe08f3fa1abda7d326ab.png

5、修改web.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath*:ApplicationContext.xml

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:springmvc-servlet.xml

1

springmvc

/

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

characterEncodingFilter

/*

48304ba5e6f9fe08f3fa1abda7d326ab.png

之前在测试过程中,不小心把/ 写错成 /*,导致springmvc把*.jsp,*.sql,*.txt都当做txt处理,访问页面时,直接在浏览器加载了jsp源码。

导致测试页面hello.jsp中的${message}一直取不到Controller传过来的值。

springmvc

/

6、测试,项目右键->Run As->Run On Server->Tomcat...

运行成功后,访问:http://localhost:8888/SSM01/Hello/SayHello

控制台输出:输出true,表示${message}赋值成功

d747eb0c74b9479ce8138025a6d7810d.png

浏览器输出:

45ee4fe7a025dbe3aa5e40643e719ae3.png

到此,SpringMVC搭建成功 !!!

继续整合!!!

7、在controller包下,新建一个BookController

48304ba5e6f9fe08f3fa1abda7d326ab.png

package com.lmei.ssm.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import com.lmei.ssm.entity.Book;

import com.lmei.ssm.service.BookService;

@Controller

@RequestMapping("/Book")

public class BookController {

@Resource

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 "Book/index";

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

8、在WEB-INFO/view下新建一个文件夹Book,然后在文件夹内添加index.jsp

cae57c5c93d4c2bba735bb2cc65c3abf.png

Book/index.jsp代码:

48304ba5e6f9fe08f3fa1abda7d326ab.png

图书管理

图书管理

编号书名
${book.id}${book.bookName}

48304ba5e6f9fe08f3fa1abda7d326ab.png

9、测试

运行服务器,在浏览器访问:http://localhost:8888/SSM01/Book/getAllBook

68ed96c7429b14ec440aad00982a2cd7.png

到此,完成SpringMVC+Spring+MyBatis的整合!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM是指Spring+SpringMVC+MyBatis的集成开发环境。MySQL是一个关系型数据库管理系统,用于存储和管理数据。Maven是一个项目管理和构建工具,可以自动下载所需的类库和插件,并管理项目的依赖关系。Idea是一个Java集成开发环境(IDE),提供了开发、调试和部署Java代码的工具。 在SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境下,我们可以通过Maven构建项目,引入相应的依赖库。Idea提供了可视化的界面,方便我们进行开发和调试工作。 首先,我们可以使用Maven来管理项目的依赖。在pom.xml文件中添加相应的依赖,Maven会自动下载并引入到项目中。 其次,我们可以使用Idea创建Spring项目,并配置相关的配置文件。在Idea的配置界面中,我们可以设置项目的数据库连接信息和配置MyBatis的相关内容。 然后,我们可以使用MyBatis来操作MySQL数据库。在MyBatis的mapper文件中编写SQL语句,并在Spring中配置相应的bean,使其可以与数据库进行交互。 此外,我们还可以使用SpringMVC来开发Web应用。在SpringMVC中,我们可以通过配置相应的请求映射和控制器来处理请求,并返回相应的结果。 最后,通过整合SpringSpringMVCMyBatis,我们可以实现业务逻辑与数据库的交互,并通过Maven进行项目构建和管理。这样,我们就可以在SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境中进行基于这些框架和工具的开发工作了。 总之,掌握SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境,意味着我们可以利用这些强大工具和框架来进行Java开发,并能够高效地开发出优质的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值