zoukankan      html  css  js  c++  java
  • Springmvc+mybatis+ajax的最易懂的搭建方式

    这里就先不介绍mybatis的发展和原理了,直接在上一篇博客的前后端分离项目的基础上尝试着配置一下:

    1、导入必要的依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-连接池依赖-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.12</version>
    </dependency>

    2、然后在applicationContext.xml中配置连接池

    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://主机地址:3306/db1?useUnicode=true&amp;characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="minIdle" value="8"></property>
        <property name="maxActive" value="20"></property>
    </bean>

    3、然后配置SqlSessionFactory

    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="typeAliasesPackage" value="pojo的完全限定名(返回值类型的前缀)"></property>
    </bean>

    4、配置 mapperScanner

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao层的完全限定名(将SqlSessionFactory 和 Mapper对象 放进IOC容器)"></property>
    </bean>

    5、这里的dao层就不需要实现类了,只写一个接口就可以了,但是要在resource中对应java中的目录写一个对应的maper的xml文件(前三行可以当模板用),返回值如果是int可以不用写。

    namespace必须是接口的完全限定名,id必须对应方法,这里禁止重载

    <?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="lyt.dao.UserMapper">
        <select id="list" resultType="User">
            select * from user
        </select>
    </mapper>

    其他的层都一样,就是控制反转的dao层接口改一下就可以了:

  • 相关阅读:
    PTA(Advanced Level)1063.Set Similarity
    PTA(Advanced Level)1047.Student List for Course
    PTA(Advanced Level)1023.Palindromic Number
    PTA(Advanced Level)1023.Have Fun with Numbers
    PTA(Basic Level)1017.A除以B
    PTA(Advanced Level)1059.Prime Factors
    PTA(Advanced Level)1096.Consecutive Factors
    expected primary-expression before xx token错误处理
    PTA(Advanced Level)1078.Hashing
    PTA(Advanced Level)1015.Reversible Primes
  • 原文地址:https://www.cnblogs.com/mvpmvp/p/13430859.html
Copyright © 2011-2022 走看看