spring整和springmvc、mybatis

一、整合mybatis

①导包

mybatis
<!--mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.2</version>
</dependency>
mybatis-spring
<!--spring整合mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.3</version>
</dependency>

mybatis-spring帮助mybatis无缝整合到spring中

mysql
<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

mysql驱动,在配置数据源时需要用到

spring-jdbc
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

需要使用到该包下的org.springframework.jdbc.datasource.DriverManagerDataSource创建spring的数据源

②mybatis配置文件

在resource下创建mybatis的配置文件 mybtis-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>
    

    <mappers>
        <mapper resource="mappers/userMapper.xml"></mapper>
    </mappers>
</configuration>

③spring-dao配置文件

在resource下创建spring-dao配置文件,

【1】<context:component-scan base-package="top.wuliaodebaozi2.pojo"/>扫描包下的注解

注意:如果使用@ComponentScan(basePackages = "org.example")则需要在配置文件中配置< context:annotation-config />,使用< context:component-scan>隐式启用<context:annotation-config >的功能。使用< context:component-scan>时通常不需要包含< context:annotation-config/>标签

【2】创建数据源

【3】根据数据源创建SqlSessionFactoryBean实例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
">

    <context:component-scan base-package="top.wuliaodebaozi2.pojo"/>

    <!--使用spring数据源替代mybatis数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://114.55.103.50:3306/block?allowMultiQueries=true&amp;useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="Admin123!"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
</beans>

④接口文件

package top.wuliaodebaozi2.mapper;

import top.wuliaodebaozi2.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();
}

⑤SQL映射文件

注意:创建了SQL映射文件,就需要在mybatis的配置文件中注册

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.wuliaodebaozi2.mapper.UserMapper">
    <select id="selectAll"  resultType="top.wuliaodebaozi2.pojo.User">
        select * from user;
    </select>
</mapper>

⑥实现接口

实现接口,需要继承自org.mybatis.spring.support.SqlSessionDaoSupport类,调用SqlSessionDaoSupport的getSqlSession()方法

public class UserMapperIml extends SqlSessionDaoSupport implements UserMapper {
    public List<User> selectAll() {
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectAll();
    }
}

⑦将接口实现添加到容器中

需要设置它的一个属性sqlSessionFactory,SqlSessionDaoSupport的属性

<bean id="userMapperIml" class="top.wuliaodebaozi2.mapper.UserMapperIml">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

二、整合spring-mvc

①配置DispatcherServlet

DisptcherServlet是springmvc的一个中央servlet,它可以将请求分发给控制器,它包含一个初始化参数:配置文件的路径

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

②创建springmvc的配置文件

springmvc的配置文件需要中包含需要扫描的包,开启spring-mvc注解支持

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

    <mvc:annotation-driven/>

    <context:component-scan base-package="top.wuliaodebaozi2.controller"/>

</beans>
posted @ 2020-05-10 15:27  无聊的包子A  阅读(28)  评论(0)    收藏  举报