第一次的ssm整合


数据库表

 导入依赖


        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            org.junit.jupiter
            junit-jupiter-api
            ${junit.version}
            test
        
        
            org.junit.jupiter
            junit-jupiter-engine
            ${junit.version}
            test
        
        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            com.mchange
            c3p0
            0.9.5.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
        
        
        
            log4j
            log4j
            1.2.17
        
        
            org.projectlombok
            lombok
            1.18.4
            provided
        
        
        
        
            org.springframework
            spring-tx
            5.2.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.13
        
        
        
            org.apache.taglibs
            taglibs-standard-spec
            1.2.5
        
        
            com.github.demidenko05
            taglibs-standard-impl
            1.0.5
        
    

编写mybatis-config.xml配置文件,这里已经绑定了SQL的映射文件,所以在spring的配置中就不要再次引入mapperLocations

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

    
        
        <package name="com.smu.pojo"/>
    
    
      

        
        <package name="com.smu.mapper"/>
    

写jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
#????MySQL8.0+,???????????; &serverTimezone=Asia/Shanghai
#jdbc.url=jdbc:mysql://localhost:3306/testProject?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.url=jdbc:mysql://localhost:3306/testproject?useSSL=false
jdbc.username=root
jdbc.password=889886hp

spring整合mybatis

编写spring-dao.xml配置文件

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

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

    
    
    

    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        


    

    
    class="com.smu.mapper.BooksMapperImpl">
        
    


    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
        
        
    

编写spring-service.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
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">









配置总的applicationContext.xml文件用于整合,引入的springmvc是表现层的,下面配

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

       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="spring-service.xml"/>
    <import resource="springmvc.xml"/>

sql映射文件需要和mapper接口同一个目录

 service层写一个业务需要的接口,然后写一个实现类,实现类注入mapper接口

public class BooksServiceImpl implements BooksService{
    private BooksMapper booksMapper;

    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public List selectAll() {
        return booksMapper.selectAll();
    }

    public Books selectById(int id) {
        return booksMapper.selectById(id);
    }

    @Override
    public int add(Books books) {
        return booksMapper.add(books);
    }

    @Override
    public int deleteById(int id) {
        return booksMapper.deleteById(id);
    }

    @Override
    public int update(Books books) {
        return booksMapper.update(books);
    }

    @Override
    public List selectByName(String bookName) {
        return booksMapper.selectByName(bookName);
    }
}

此类已经在spring-service.xml中注册过bean,代码如下

class="com.smu.service.BooksServiceImpl">
        
    
    class="com.smu.service.UsersServiceImpl">
        
    

写一个util包,将获得bean的操作简化

public class BooksServiceUtil {
    public static BooksServiceImpl getBooksServiceBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        return context.getBean("booksServiceImpl", BooksServiceImpl.class);
    }
}   

spring整合springMVC

在web.xml下部署dispatchSerclet

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

         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">
    
    
    
        springmvc
        class>org.springframework.web.servlet.DispatcherServletclass>

        
            contextConfigLocation
            classpath:applicationContext.xml
        

        
        1
    
    
        springmvc
        
        /
    
    
    
        encoding
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            encoding
            utf-8
        
    
    
        encoding
        /*
    

编写springmvc.xml配置文件

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

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:nvc="http://www.springframework.org/schema/mvc"
       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
       https://www.springframework.org/schema/context/spring-context.xsd">

    
    
    

    
    default-servlet-handler/>
    
    package="com.smu.controller"/>

    
    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
        
        
    

        
        
            
                
                class="com.smu.interceptor.LoginInterceptor"/>
            
        

        
    
    
    
    
    
    
    

controller层的部分代码截图