ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring+sprinmvc+mybatis整合

2020-10-21 14:32:56  阅读:13  来源: 互联网

标签:sprinmvc xml spring 注解 mybatis org public


自学中 ,最近想做下ssm整合,不是专业出身的我,学习Java还是比较缓慢。(一边工作一边学习非计算类工作)

在整合通过网络基本学习了下,网上大概思路就是用spring整合springmvc 再spring整合mybatis,最后再整合。

作为不懂原理及未去熟悉源码的我,基本学会了怎么把网上基本ssm整合码出来。写得烂的,请多包含。作为我学习的动力写下了这篇博客。

1.搭建目录结构如下:

 

 

 2.在pom.xml文件中导入一下所需要的jar包坐标依赖

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.39</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.1</version>
      <classifier>sources</classifier>
      <type>jar.sha256</type>
    </dependency>
    <dependency>
      <groupId>com.10duke.client.json</groupId>
      <artifactId>jackson</artifactId>
      <version>1.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  导入坐标后我就开始搭建springmvc并进行测试

3.配置web.xml文件

  3.1配置过滤器

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  3.2配置基本搭建springmvc的配置

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 4.创建spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
    <!--可以后期静态资源访问、驱动器、过滤器配置等。
        现在我们导入了其他俩个xml配置文件和注解扫描
    -->
    
        <import resource="applicationContext.xml"></import>
        <import resource="mybatis-spring.xml"></import>
        <context:component-scan base-package="com.zero.Contorl"></context:component-scan>
</beans>

  <context:component-scan base-package="xx.xxx.xxxx"/>会自动扫描该xx.xxx.xxxx包下的注解,完成相应的配置

5.在控制文件当中使用注解

@Controller
@RequestMapping("/demo")
public class ZeroContorl {
    @Autowired
    private ZeroServiceimpl zeroserviceimpl;
    @RequestMapping("/index")
    public @ResponseBody List<ZeroDetails> index(){
        List<ZeroDetails> x =zeroserviceimpl.FindAll();
        return x;
    }

  其中注解的详解请读者自行进行网上浏览,我自己也做了下笔记:

@Component注解:作用于class类上,相当于<bean id="xxx" class="xxx.xxx.xxx">;@Component注解的衍生物分别:1.@Repository在Dao层进行使用;2.@Serivice在Serivice层进行使用;3.Controller在web层使用

@Autowired注解:功能和注解@Resource一样,可以对类成员变量,方法及构造函数进行标注,完成自动装配的工作。只不过注解@Resource是按照名称进行装配的,而@Autowired则是按照类型进行装配的

@Qualifier("名称") 配合@Autowired使用

6.开始进行测试服务器能否正常访问,

 

可以先替换成public void index(){System.out.print("hello");}

7.创建Dao层和Service层,并使用注解

Dao层:

@Repository
public class demodao {
    public List<ZeroDetails> FindAll(){
        ZeroDetails dd=new ZeroDetails();
        dd.setZid(22);
        dd.getZname("xihuan");
        List<ZeroDetails> xx=new ArrayList<>();
        xx.add(dd);
        return xx;
    }

}

  其中发现少pojo,自行创建pojo;为了方便后面整合mybatis,里面的变量要与数据库一致

Service层:

@Service
public class ZeroServiceimpl implements  ZeroService {
    @Autowired
    private com.zero.Dao.demodao2 demodao2;
    @Override
    public List<ZeroDetails> FindAll() {
        return demodao2.FindAll();
    }
}

8.创建applicationContext.xml,spring配置文件,整合spring、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"
       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:component-scan base-package="com.zero.Service"></context:component-scan>
    <context:component-scan base-package="com.zero.Dao"></context:component-scan>
</beans>

 

总结下基本思路:把所有的类全部交给spring容器,然后在我需要某个类的地方进行注入这个类即可使用该类;

9.搭建mybatis的配置及测试是否搭建成功
  创建mybatis.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>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
                <property name="username" value="xxx"/>
                <property name="password" value="xxx"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="com.zero.Dao.demodao2"></mapper>
    </mappers>
</configuration>

  在demodao2接口下加入mybatis注解

@Repository
public interface demodao2 {
    @Select("select * from zerodetails")
    List<ZeroDetails> FindAll();
}

  测试:

 

 10.当测试成功后,我们就开始准备spring、mybatis、springmvc整合

创建mybatis-spring.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">
    <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl"
                  value="jdbc:mysql:///xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
        <property name="user" value="xxxx"/>
        <property name="password" value="xxxx"/>
    </bean>
    <bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlsessionfactory"/>
        <property name="basePackage" value="com.zero.Dao"/>
    </bean>
</beans>

  自我总结下:通过spring进行配置DriverManagerDataSource类需要的基本链接数据驱动配置信息;再将dataSource配置信息、mybatis.xml环境配置注入到SqlSessionFactoryBean类当中;MapperScannerConfigurer类我还是专门再网上收的。我这里就不具体描述了;我认为就是在com.zero.Dao查找mapper注解信息,自动装配;

最后在web.xml中配置监听器

 

  对了,在之前我们替换测试部分记得返回到开始的文件哦

进行tomcat基本配置后启动服务,访问服务器,完成


标签:sprinmvc,xml,spring,注解,mybatis,org,public
来源: https://www.cnblogs.com/cnleihu/p/13852155.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有