ssm整合(ssm整合springboot)

  本篇文章为你整理了ssm整合(ssm整合springboot)的详细内容,包含有ssm整合配置详解 ssm整合springboot ssm整合mybatis ssm整合vue ssm整合,希望能帮助你了解 ssm整合。

   /dependency

   !-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --

   dependency

   groupId org.springframework /groupId

   artifactId spring-webmvc /artifactId

   version 5.3.20 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --

   dependency

   groupId org.springframework /groupId

   artifactId spring-jdbc /artifactId

   version 5.3.20 /version

   /dependency

   !-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --

   dependency

   groupId javax.servlet /groupId

   artifactId javax.servlet-api /artifactId

   version 4.0.1 /version

   scope provided /scope

   /dependency

   !-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --

   dependency

   groupId javax.servlet.jsp /groupId

   artifactId jsp-api /artifactId

   version 2.2 /version

   scope provided /scope

   /dependency

   !-- https://mvnrepository.com/artifact/javax.servlet/jstl --

   dependency

   groupId javax.servlet /groupId

   artifactId jstl /artifactId

   version 1.2 /version

   /dependency

   !-- https://mvnrepository.com/artifact/junit/junit --

   dependency

   groupId junit /groupId

   artifactId junit /artifactId

   version 4.13.2 /version

   scope test /scope

   /dependency

   !-- https://mvnrepository.com/artifact/com.google.code.gson/gson --

   dependency

   groupId com.google.code.gson /groupId

   artifactId gson /artifactId

   version 2.9.0 /version

   /dependency

   !-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --

   dependency

   groupId cn.hutool /groupId

   artifactId hutool-all /artifactId

   version 5.8.1 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --

   dependency

   groupId org.apache.logging.log4j /groupId

   artifactId log4j-core /artifactId

   version 2.17.2 /version

   /dependency

   !-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --

   dependency

   groupId org.apache.logging.log4j /groupId

   artifactId log4j-api /artifactId

   version 2.17.2 /version

   /dependency

  

 

 

  1.2 、静态资源导出问题

  

 !--静态资源导出-- 

 

   build

   resources

   resource

   directory src/main/resources /directory

   includes

   include **/*.properties /include

   include **/*.xml /include

   /includes

   filtering true /filtering

   /resource

   resource

   directory src/main/java /directory

   includes

   include **/*.properties /include

   include **/*.xml /include

   /includes

   filtering true /filtering

   /resource

   /resources

   /build

  

 

  1.3、 准备数据

  

CREATE DATABASE ssmbuild;

 

  USE ssmbuild;

  CREATE TABLE `books`(

  `bookId` INT NOT NULL AUTO_INCREMENT COMMENT 书id,

  `bookName` VARCHAR(100) NOT NULL COMMENT 书名,

  `bookCounts` INT NOT NULL COMMENT 数量,

  `detail` VARCHAR(200) NOT NULL COMMENT 描述,

  KEY `bookId`(`bookId`)

  )ENGINE=INNODB DEFAULT CHARSET=utf8;

  INSERT INTO `books`(`bookId`,`bookName`,`bookCounts`,`detail`)VALUES

  (1,Java,1,从入门到放弃),

  (2,MySQL,10,从删库到跑路),

  (3,Linux,5,从进门到进牢)

  

 

  1.4、 数据库链接配置

  database.properties

  

jdbc.driver=com.mysql.cj.jdbc.Driver

 

  #8.0以上需要 serverTimezone=Asia/Shanghai 设置时区

  jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?characterEncoding=utf-8 useSSL=true serverTimezone=Asia/Shanghai

  jdbc.username=qifeng

  jdbc.password=qifeng

  

 

  1.5、 创建mybatis和spring的空配置文件

  


 ?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

   https://www.springframework.org/schema/beans/spring-beans.xsd"

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

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

   configuration

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

   this.bookId = bookId;

   this.bookName = bookName;

   this.bookCounts = bookCounts;

   this.detail = detail;

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

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

   mapper namespace="com.wq.dao.BookMapper"

   insert id="addBook" parameterType="books"

   insert into books (bookname, bookCounts, detail)

   values (#{bookname}, #{bookCounts}, #{detail});

   /insert

   delete id="deleteBookById" parameterType="int"

   delete

   from books

   where bookId = #{bookId};

   /delete

   update id="updateBook" parameterType="books"

   update books

   set bookname = #{bookname},

   bookCounts = #{bookCounts},

   detail = #{detail}

   where bookId = #{bookId};

   /update

   select id="selectBook" resultType="books" parameterType="int"

   select *

   from books

   where bookId = #{bookId};

   /select

   select id="selectAllBook" resultType="books"

   select *

   from books;

   /select

   /mapper

  

 

  编写完Mapper.xml后绑定到配置文件中。使用package标签将dao层全部绑定(mybatis-config.xml)

  

 ?xml version="1.0" encoding="UTF-8" ? 

 

   !DOCTYPE configuration

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

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

   configuration

   !--配置别名,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名--

   typeAliases

   package name="com.wq"/

   /typeAliases

   !-- 将包内的映射器接口实现全部注册为映射器 --

   mappers

   package name="com.wq.dao"/

   /mappers

   /configuration

  

 

  service层

  

package com.wq.service;

 

  import com.wq.pojo.Books;

  import java.util.List;

  public interface BookService {

   int addBook(Books books);

   int deleteBookById(int id);

   int updateBook(Books books);

   Books selectBook(int id);

   List Books selectAllBook();

  

 

  

package com.wq.service;

 

  import com.wq.dao.BookMapper;

  import com.wq.pojo.Books;

  import org.apache.ibatis.annotations.Param;

  import java.util.List;

  public class BookServiceImpl implements BookService{

   //业务层调dao层,组合Dao

   private BookMapper bookMapper;

   public BookServiceImpl(BookMapper bookMapper) {

   this.bookMapper = bookMapper;

   public BookServiceImpl() {

   public void setBookMapper(BookMapper bookMapper) {

   this.bookMapper = bookMapper;

   @Override

   public int addBook(Books books) {

   return bookMapper.addBook(books);

   @Override

   public int deleteBookById(int id) {

   return bookMapper.deleteBookById(id);

   @Override

   public int updateBook(Books books) {

   return bookMapper.updateBook(books);

   @Override

   public Books selectBook(int id) {

   return bookMapper.selectBook(id);

   @Override

   public List Books selectAllBook() {

   return bookMapper.selectAllBook();

  

 

  2. spring层

  2.1、整合dao层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

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

   !--关联数据库配置文件--

   context:property-placeholder location="classpath:database.properties"/

   !--连接池--

   bean id="dataSource"

   property name="driverClass" value="${jdbc.driver}"/

   property name="jdbcUrl" value="${jdbc.url}"/

   property name="user" value="${jdbc.username}"/

   property name="password" value="${jdbc.password}"/

   !--c3p0私有属性--

   property name="maxPoolSize" value="30"/

   property name="minPoolSize" value="10"/

   !--关闭链接后不自动commit--

   property name="autoCommitOnClose" value="false"/

   !--获取链接超时时间--

   property name="checkoutTimeout" value="10000"/

   !--当获取链接失败重试次数--

   property name="acquireRetryAttempts" value="2"/

   /bean

   !--sqlSessionFactory--

   bean id="sqlSessionFactory"

   property name="dataSource" ref="dataSource"/

   !--绑定mybatis配置--

   property name="configLocation" value="classpath:mybatis-config.xml"/

   /bean

   !--配置dao接口扫描包,动态实现Dao接口注入--

   bean

   !--注入sqlSessionFactory--

   property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/

   !--要扫描的dao包--

   property name="basePackage" value="com.wq.dao"/

   /bean

   /beans

  

 

  2.2、service层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"

   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"

   !--扫描service包--

   context:component-scan base-package="com.wq.service"/

   bean id="bookServiceImpl"

   property name="bookMapper" ref="bookMapper"/

   /bean

   !--声明式事务--

   bean id="transactionManager"

   !--注入数据源--

   property name="dataSource" ref="dataSource"/

   /bean

   /beans

  

 

  3. springmvc层

  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"

   !--DispatcherServlet--

   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

   !--乱码过滤--

   filter

   filter-name encoding /filter-name

   filter-class org.springframework.web.filter.CharacterEncodingFilter /filter-class

   init-param

   param-name encoding /param-name

   param-value UTF-8 /param-value

   /init-param

   init-param

   param-name forceEncoding /param-name

   param-value true /param-value

   /init-param

   /filter

   filter-mapping

   filter-name encoding /filter-name

   url-pattern /* /url-pattern

   /filter-mapping

   !--session设置15分钟--

   session-config

   session-timeout 15 /session-timeout

   /session-config

   /web-app

  

 

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

   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/mvc

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

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

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

   !--注解驱动--

   mvc:annotation-driven/

   !--静态资源过滤--

   mvc:default-servlet-handler/

   !--扫描包:controller--

   context:component-scan base-package="com.wq.controller"/

   !--视图解析器--

   bean

   property name="prefix" value="/WEB-INF/jsp/"/

   property name="suffix" value=".jsp"/

   /bean

   !--解决json 乱码配置--

   mvc:annotation-driven

   mvc:message-converters

   bean

   constructor-arg value="UTF-8"/

   /bean

   /mvc:message-converters

   /mvc:annotation-driven

   /beans

  

 

  整合到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

   https://www.springframework.org/schema/beans/spring-beans.xsd"

   import resource="spring-dao.xml"/

   import resource="spring-service.xml"/

   import resource="spring-mvc.xml"/

   /beans

  

 

  项目结构图
 

  以上就是ssm整合(ssm整合springboot)的详细内容,想要了解更多 ssm整合的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: