SSM整合

Posted 我永远信仰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。


在完成数据库设计,maven配置依赖后,创建对应实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books 
    private int bookID;
    private String bookName ;
    private int bookCounts;
    private String detail;

项目结构

1、先完成mybatis层

创建mybatis配置文件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>
    <!--给实体类起别名-->
    <typeAliases>
        <package name="com.yong.pojo"/>
    </typeAliases>
</configuration>

创建dao层下的接口和mapper

BookMapper.java

public interface BookMapper 
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById(@Param("bookId") int id);

    //修改一本书
    int updateBook(Books books);

    //查询一本书
    int queryBookById(@Param("bookId") int id);

    //查询全部的书
    List<Books> queryAllBook();

BookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yong.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="queryBookById" resultType="Books">
        select *
        from books where bookID=#bookId;
    </select>

    <select id="queryAllBook" resultType="Books">
        select *
        from books;
    </select>
</mapper>

写好mapper后马上去mybatis-config.xml注册

<mappers>
    <mapper class="com.yong.dao.BookMapper"/>
</mappers>

开始service

创建对应的接口

BookService.java

public interface BookService 
    //增加一本书
    int addBook(Books books);

    //删除一本书
    int deleteBookById( int id);

    //修改一本书
    int updateBook(Books books);

    //查询一本书
    int queryBookById(int id);

    //查询全部的书
    List<Books> queryAllBook();

实现类

BookServiceImpl.java

public class BookServiceImpl implements BookService 

    //service调用dao,组合
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) 
        this.bookMapper = bookMapper;
    

    public int addBook(Books books) 
        return bookMapper.addBook(books);
    

    public int deleteBookById(int id) 
        return bookMapper.deleteBookById(id);
    

    public int updateBook(Books books) 
        return bookMapper.updateBook(books);
    

    public int queryBookById(int id) 
        return bookMapper.queryBookById(id);
    

    public List<Books> queryAllBook() 
        return null;
    

mybatis层完成

2、Spring层

创建数据库配置文件database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSl=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username=root
password=123456

创建Spring的核心配置文件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">

</beans>

创建spring-dao.xml,分层开发,dao层

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

    <!--1、关联数据库文件-->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--2、连接池
        dbcp:半自动化操作,不能自动连接
        c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中)
        druid:
        hikari:
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="$driver"/>
        <property name="jdbcUrl" value="$url"/>
        <property name="user" value="$username"/>
        <property name="password" value="$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>

    <!--3、sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--4、配置dao接口扫描包:动态的实现了Dao接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的dao包-->
        <property name="basePackage" value="com.yong.dao"/>
    </bean>
</beans>

创建spring-service.xml,service层

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

    <!--1、扫描service下的包-->
    <context:component-scan base-package="com.yong.service"/>

    <!--2、将所有的业务类注入到spring,可以通过配置文件或者注解-->
    <bean id="bookServiceImpl" class="com.yong.service.Impl.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--3、声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--4、AOP事务支持-->
    
</beans>

3、SpringMVC层

  • 增加web框架支持

在web.xml中配置dispatchservlet和乱码过滤器

<?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:spring-mvc.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>encodingFilter</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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!--1、注解驱动-->
    <mvc:annotation-driven/>
    <!--2、静态资源过滤-->
    <mvc:default-servlet-handler/>
    <!--3、扫描包-->
    <context:component-scan base-package="com.yong.controller"/>
    <!--4、视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

最后整合这些配置文件,在Spring的核心配置文件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-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

至此,框架整合完成,依靠Spring来整合,Spring本身就是一个大杂烩

4、增加业务

以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架整合

SSM 项目整合详细解读

SSM整合

520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“

shiro整合SSM(spring_springmvc_mybatis)

ssm整合redis