IDEA SSM框架整合配置及步骤详解

网友投稿 254 2023-01-25

IDEA SSM框架整合配置及步骤详解

参考

狂神说SpringMVC05:整合SSM框架

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247484004&idx=1&sn=cef9d881d0a8d7db7e8ddc6a380a9a76&scene=19#wechat_redirect

前言

根据自己的环境参考狂神的视频进行了SSM框架整合,用于备忘

SSM框架整合步骤

1. 创建数据库

2. IDEA创建maven项目.在pom.xml中设设置java conpiler版本(jdk13)

3. 导入依赖 jnuit 数据库驱动 数据库连接 Severlet jsp

4. maven 资源过滤

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

4.0.0

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

org.example

ssm

1.0-SNAPSHOT

13

13

junit

junit

4.12

mysql

mysql-connector-java

5.1.47

com.mchange

c3p0

0.9.5.2

javax.servlet

servlet-api

2.5

CVhKzUzHI

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.2

org.mybatis

mybatis-spring

2.0.6

org.springframework

spring-webmvc

5.3.4

org.springframework

spring-jdbc

5.3.4

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

4.0.0

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

org.example

ssm

1.0-SNAPSHOT

13

13

junit

junit

4.12

mysql

mysql-connector-java

5.1.47

com.mchange

c3p0

0.9.5.2

javax.servlet

servlet-api

2.5

CVhKzUzHI

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.2

org.mybatis

mybatis-spring

2.0.6

org.springframework

spring-webmvc

5.3.4

org.springframework

spring-jdbc

5.3.4

5. 创建两个项目的目录结构以及Spring 和 Mybatis的配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

Mybatis

6. 配置数据库属性

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

jdbc.username=root

jdbc.password=root

7. 创建Book实体类(使用lombok插件),编写Dao层mapper接口,在mybatis配置中注册mapper

package com.projectname.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

@Data

@AllArgsConstructor

@NoArgsConstructor

public class Book {

private int bookID;

private String bookName;

private int bookCounts;

private String detail;

}

package com.projectname.dao;

import com.projectname.pojo.Book;

import java.util.List;

public interface DaoBook {

//增加一个Book

int addBook(Book book);

//根据id删除一个Book

int deleteBookById(int id);

//更新Book

int updateBook(Book books);

//根据id查询,返回一个Book

Book queryBookById(int id);

//查询全部Book,返回list集合

List queryAllBook();

}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into ssmbuild.books(bookName,bookCounts,detail)

values (#{bookName}, #{bookCounts}, #{detail})

delete from ssmbuild.books where bookID=#{bookID}

update ssmbuild.books

set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}

where bookID = #{bookID}

select * from ssmbuild.books

where bookID = #{bookID}

SELECT * from ssmbuild.books

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

8. 编写service层的接口和实现类代码

package com.projectname.service;

import com.projectname.pojo.Book;

import java.util.List;

public interface BookService {

//增加一个Book

int addBook(Book book);

//根据id删除一个Book

int deleteBookById(int id);

//更新Book

int updateBook(Book books);

//根据id查询,返回一个Book

Book queryBookById(int id);

//查询全部Book,返回list集合

List queryAllBook();

}

package com.projectname.service;

import com.projectname.dao.BookMapper;

import com.projectname.pojo.Book;

import java.util.List;

public class BookServiceImpl implements BookService {

//调用dao层的操作,设置一个set接口,方便Spring管理

private BookMapper bookMapper;

public void setBookMapper(BookMapper bookMapper) {

this.bookMapper = bookMapper;

}

public int addBook(Book book) {

return bookMapper.addBook(book);

}

public int deleteBookById(int id) {

return bookMapper.deleteBookById(id);

}

public int updateBook(Book books) {

return bookMapper.updateBook(books);

}

public Book queryBookById(int id) {

return bookMapper.queryBookById(id);

}

public List queryAllBook() {

return bookMapper.queryAllBook();

}

}

Spring

9. 使用Spring编写Mybatis配置文件:spring-dao.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

10. Spring 整合service层

spring-service

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

Spring MVC层

11. configuate the web.xml

xmlns:xsi="http://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">

DispatcherServlet</servlet-name>

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

DispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

xmlns:xsi="http://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">

DispatcherServlet</servlet-name>

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

DispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

12. spring-mvc.xml

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

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

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

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

13. configuate applicationContext.xml

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

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

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

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

一些杂七杂八的配置

14. 添加日志功能

15. add lib

File -> Project Structure->Artifacts

Type -> Bundle

new directory lib

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 数据库连接(JDBC)的相关总结
下一篇:电影api资源站(电影API)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~