• MyBatis02


    导包

     创建**Mapper

    package cn.jiedada.ssm.mapper;
    
    import cn.jiedada.ssm.domain.Dept;
    
    import java.util.List;
    
    public interface DeptMapper {
        /*这里的方法名需要和配置文件中的方法id相同
        *   <select id="findAll" resultType="dept">通过mapper.xml中id找到对应的值*/
        List<Dept> findAll();
    }
    View Code

    创建**Mapper.xml

    需要在资源文件夹下创建mapper.xml这样就是为了区分,提高阅读性,其实都可以的我们就在resources中创建

    这里有一个注意的地方

     这里的namespace的名称一定需要和我们的接口对应

     就是和我这个权限命名

     注意这里需要

     这样的方式创建而不是通过cn.jiedada.ssm.mapper

    <?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">
    <!--namespace是我们自己对应的实现接口的名称-->
    <mapper namespace="cn.jiedada.ssm.mapper.DeptMapper">
        <select id="findAll" resultType="dept">
            select * from dept
        </select>
    </mapper>

    配置文件

    需要在启动spring的时候让,因为我们只配至了接口所以在spring中我们也需要配置动态代理

    application.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"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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
    ">
    <!--扫描service层-->
    <context:component-scan base-package="cn.jiedada.ssm.service"></context:component-scan>
    <!--添加jdbc.properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--创建连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--读取*Mapper.xml-->
        <property name="mapperLocations" value="classpath:cn/jiedada/ssm/mapper/*Mapper.xml" />
        <!--使用别名-->
        <property name="typeAliasesPackage">
            <value>
                cn.jiedada.ssm.domain
                cn.jiedada.ssm.query
            </value>
        </property>
    </bean>
    <!--动态代理自动生成实现类-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.jiedada.ssm.mapper"></property>
    </bean>
    <!--事务配置-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    View Code
  • 相关阅读:
    iOS- 网络访问JSON数据类型与XML数据类型的实现思路及它们之间的区别
    iOS- AVSpeechSynthesizer——iOS7语音合成器
    iOS- 利用AFNetworking(AFN)
    iOS- 利用AFNetworking(AFN)
    iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现
    iOS- 多线程技术的概述及优点
    150. Best Time to Buy and Sell Stock II【medium】
    213. String Compression【easy】
    495. Implement Stack【easy】
    547. Intersection of Two Arrays【easy】
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11774911.html
Copyright © 2020-2023  润新知