Spring+Mybatis(一)無mapper.xml方式

weixin_44046865發表於2020-10-21

Spring整合Mybatis

Maven依賴包

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
<!--      <scope>test</scope>-->
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
<!--      <scope>test</scope>-->
    </dependency>

    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-impl</artifactId>
      <version>1.2.5</version>
    </dependency>

    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-spec</artifactId>
      <version>1.2.5</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.18</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.22</version>
    </dependency>

把applicationContext.xml和mybatis-config.xml整合成一個xml

在resources目錄下新增jdbc.properties檔案
該檔案用來寫有關資料庫連結的資訊

#jdbc
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/shortterm?characterEncoding=utf-8
jdbc_username=root
jdbc_password=123
validationQuery=SELECT 1

刪除applicationContext.xml 和mybatis-config.xml檔案,增加spring-mybatis.xml檔案
實際上就是把mybatis裡的配置放到了spring的配置中
看看spring-mybatis.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:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <context:component-scan base-package="service,servlet" />

    <!-- 配置資料來源-->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}"/>
        <property name="username" value="${jdbc_username}"/>
        <property name="password" value="${jdbc_password}"/>
        <!--連線池大小-->
        <property name="initialSize" value="0"/>
        <!--連線池最大使用連線數-->
        <property name="maxActive" value="20"/>
        <!--連線池最小空閒-->
        <property name="minIdle" value="0"/>
        <!--連線池最大等待時間-->
        <property name="maxWait" value="60000"/>
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <!-- 開啟removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分鐘 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 關閉abanded連線時輸出錯誤日誌 -->
        <property name="logAbandoned" value="true" />
        <!-- 監控資料庫 -->
        <!-- <property name="filters" value="stat" /> -->
        <property name="filters" value="mergeStat" />
    </bean>

    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>



    <!--配置mapper-->
    <bean id="adminMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="mapper.AdminMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

資料庫連線池用的是阿里的Druid,Maven配置裡有依賴包
${jdbc_url}引用了之前寫好的jdbc.properties檔案
對SqlSessionFactoryBean和MapperFactoryBean進行注入
MapperFactoryBean會給出一個藉口的實現類(動態代理)
但這樣配置需要對每一個mapper配置所以我們可以用MapperScannerConfigurer

    <!--配置mapper-->
<!--    <bean id="adminMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!--        <property name="mapperInterface" value="mapper.AdminMapper"/>-->
<!--        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
<!--    </bean>-->


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

< property name=“basePackage” value=“mapper”/>掃描的包
他會自動掃描包下面的mapper介面

看看AdminMapper

public interface AdminMapper {
    @Select("select * from admin where name=#{name}")
    public Admin getByName(String name);

    @Insert("insert into admin values(null,#{name},#{password}")
    public void insert(Admin admin);

    @Delete("delete from admin where id=#{id}")
    public void delete(Integer id);

    @Update("update admin set name=#{name},password=#{password} where id=#{id}")
    public void update(Admin admin);
}

你可以使用註解或者xml方式注入物件(看看Spring就知道怎麼寫了)
這裡用註解方式

@Service
public class AdminServiceImp{


    private AdminMapper adminMapper;

    @Autowired
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    public void add(Admin a) {
        adminMapper.insert(a);
    }

    public void delete(Integer id) {
        adminMapper.delete(id);
    }

    public void update(Admin a){
        adminMapper.update(a);
    }

    public Admin getByName(String name){
        System.out.println("come");
        System.out.println(adminMapper);
        Admin admin=adminMapper.getByName(name);
        return admin;
    }


}

相關文章