Java 进阶必备之ssm框架全面整合

网友投稿 268 2022-09-21


Java 进阶必备之ssm框架全面整合

目录1.导入依赖2.创建实体类3.写dao层接口4.写mybatis核心配置文件和接口配置文件5.用spring整合Mybatis层也就是Dao层6.spring整合Service层7.spring整合Conteoller层8.添加web支持9.完整的目录结构

1.导入依赖

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

com.fasterxml.jackson.core

jackson-databind

2.12.1

com.alibaba

druid-spring-boot-starter

1.1.10

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

2.创建实体类

package pojo;

public classhttp:// books {

private int bookId;

private String bookName;

private int bookCounts;

private String detail;

public books(int bookId, String bookName, int bookCounts, String detail) {

this.bookId = bookId;

this.bookName = bookName;

this.bookCounts = bookCounts;

this.detail = detail;

}

public books() {

}

public int getBookId() {

return bookId;

}

public void setBookId(int bookId) {

this.bookId = bookId;

}

public String getBookName() {

return bookName;

}

public void setBookName(String bookName) {

this.bookName = bookName;

}

public int getBookCounts() {

return bookCounts;

}

public void setBookCounts(int bookCounts) {

this.bookCounts = bookCounts;

}

public String getDetail() {

return detail;

}

public void setDetail(String detail) {

this.detail = detail;

}

}

3.写dao层接口

这里暂时只有一个方法

package Dao;

import pojo.books;

import java.util.List;

public interface BooksMapper {

List selectbooks();

}

4.写mybatis核心配置文件和接口配置文件

这个是接口配置文件

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

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

select * from books

mybatis核心配置文件

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

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

5.用spring整合Mybatis层也就是Dao层

这个是spring整合Mybatis的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">

数据库配置文件,这里你们改一下数据库就OK

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild

jdbc.username=root

jdbc.password=root

6.spring整合Service层

写接口和实现类

package Service;

import org.springframework.stereotype.Service;

import pojo.books;

import java.util.List;

public interface BooksService {

List selectbooks();

}

实现类

@Service

public class BooksServicelmpl implements BooksService{

/*这里是把Dao的接口引进来了因为Service层调用Dao层*/

@Autowired

private BooksMapper booksMapper;

public void setBooksMapper(BooksMapper booksMapper) {

this.booksMapper = booksMapper;

}

@Override

public List selectbooks() {

return booksMapper.selectbooks();

}

}

写spring配置文件

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配置文件,我们写个总的配置文件到进去就ok

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">

7.spring整合Conteoller层

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

httpJVrryIus://springframework.org/schema/mvc/spring-mvc.xsd">

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

httpJVrryIus://springframework.org/schema/mvc/spring-mvc.xsd">

写Controller的类

@RestController

public class bookController {

@Autowired

private BooksService booksService;

public void setBooksService(BooksService booksService) {

this.booksService = booksService;

}

@RequestMapping("/books")

public List selectbooks()

{

List list = booksService.selectbooks();

return list;

}

}

把三个配置文件关联

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">

8.添加web支持

写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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

创建一个lib包把依赖导进去

添加工件测试

9.完整的目录结构


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

上一篇:HCIA-vlan间路由技术(hcna路由交换认证)
下一篇:HCIA-二层端口技术(hci接口)
相关文章

 发表评论

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