百木园-与人分享,
就是让自己快乐。

第一次的ssm整合

数据库表

 

 导入依赖

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--        事务支持-->
        <!--spring事务-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <!--        使用forEach标签-->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.demidenko05</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.0.5</version>
        </dependency>
    </dependencies>

编写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\">
<configuration>
    <typeAliases>
        <!--        配置别名,配置之后返回类型就不用写全路径了-->
        <package name=\"com.smu.pojo\"/>
    </typeAliases>
    <mappers>
      <!--加载sql映射文件-->
<!--        <mapper resource=\"com/smu/mapper/BooksMapper.xml\"/>-->
        <!--Mapper代理方式-->
        <package name=\"com.smu.mapper\"/>
    </mappers>
</configuration>

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

    <!--    DataSource:使用Spring的数据源替换MyBatis的配置 c3p0/dbcp/druid-->
    <!--    1.关联数据库配置文件-->
    <context:property-placeholder location=\"classpath:jdbc.properties\"/>

    <!--    2.连接池
            dbcp:半自动化操作,不能自动连接
            c3p0: 自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!)
            druid:   hikari:
    -->
    <bean id=\"dataSource\" class=\"com.mchange.v2.c3p0.ComboPooledDataSource\">
        <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=\"10\"/>
    </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\"/>
<!--        <property name=\"mapperLocations\" value=\"classpath:com/smu/mapper/BooksMapper.xml\"/>-->

    </bean>

    <!--    使用继承SqlSessionDaoSupport的方式-->
    <bean id=\"booksMapperImpl\" class=\"com.smu.mapper.BooksMapperImpl\">
        <property name=\"sqlSessionFactory\" ref=\"sqlSessionFactory\"/>
    </bean>


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

</beans>

编写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
https://www.springframework.org/schema/context/spring-context.xsd\">
<!-- 用于扫描注解 -->
<!-- <context:component-scan base-package=\"com.smu.service\"/>-->
<!-- 使用扫描接口的方式注册bean-->
<bean id=\"booksServiceImpl\" class=\"com.smu.service.BooksServiceImpl\">
<property name=\"booksMapper\" ref=\"booksMapper\"/>
</bean>
<bean id=\"usersServiceImpl\" class=\"com.smu.service.UsersServiceImpl\">
<property name=\"usersMapper\" ref=\"usersMapper\"/>
</bean>
</beans>

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

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

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

 

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

public class BooksServiceImpl implements BooksService{
    private BooksMapper booksMapper;

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

    @Override
    public List<Books> 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<Books> selectByName(String bookName) {
        return booksMapper.selectByName(bookName);
    }
}

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

<bean id=\"booksServiceImpl\" class=\"com.smu.service.BooksServiceImpl\">
        <property name=\"booksMapper\" ref=\"booksMapper\"/>
    </bean>
    <bean id=\"usersServiceImpl\" class=\"com.smu.service.UsersServiceImpl\">
        <property name=\"usersMapper\" ref=\"usersMapper\"/>
    </bean>

写一个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\"?>
<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\">
    <!--    <display-name>springMVC</display-name>-->
    <!-- 部署 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>

        <!-- 表示容器再启动时立即加载servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 处理所有URL -->
        <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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

编写springmvc.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: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\">

    <!--  配置SpringMVC  -->
    <!--    1.开启SpringMVC注解驱动 -->
    <nvc:annotation-driven/>

    <!--    2.静态资源过滤默认servlet配置 -->
    <mvc:default-servlet-handler/>
    <!--    3.扫描相关的controller-->
    <context:component-scan base-package=\"com.smu.controller\"/>

    <!--    处理器映射器-->
    <bean class=\"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping\"/>
    <!--    处理器适配器-->
    <bean class=\"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter\"/>
    <!--    视图解析器 以后可能会用到的模板引擎Thymeleaf Freemarker-->
    <bean id=\"internalResourceViewResolver\" class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">
        <!--        前缀-->
        <property name=\"prefix\" value=\"/WEB-INF/jsp/\"/>
        <!--        后缀-->
        <property name=\"suffix\" value=\".jsp\"/>
    </bean>

        <!--    拦截器配置-->
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path=\"/book/**\"/>
                <bean class=\"com.smu.interceptor.LoginInterceptor\"/>
            </mvc:interceptor>
        </mvc:interceptors>

        <!--    文件上传支持-->
    <!--    <bean id=\"multipartResolver\" class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\">-->
    <!--        &lt;!&ndash;        请求表单的格式,需要和jsp的pageEncoding一样,默认为ISO-8859-1&ndash;&gt;-->
    <!--        <property name=\"defaultEncoding\" value=\"utf-8\"/>-->
    <!--        &lt;!&ndash;        上传文件的大小上限,单位为字节10485760=10m&ndash;&gt;-->
    <!--        <property name=\"maxUploadSize\" value=\"10485760\"/>-->
    <!--        <property name=\"maxInMemorySize\" value=\"40960\"/>-->
    <!--    </bean>-->
</beans>

controller层的部分代码截图

 


来源:https://www.cnblogs.com/happy12123/p/16332628.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 第一次的ssm整合

相关推荐

  • 暂无文章