• MyBatis的简单使用


    使用接口代理是需要注意:

    1.xml和java接口必须同包同名

    2.主xml文件中<mapper>标签中‘class’属性需要映射到接口

    3.被映射的xml(和接口同名)中<mapper namespace="接口全路径">

    4.操作标签(增删改查)的id和接口中方法名一致

    5.接口不需要实现,SqlSessionFactory获取SqlSession对象,再通过反射sqlSession.getMapper(接口名.class) 得到对象,调用接口方法;

    InputStream is = Resources.getResourceAsStream("com/bwf01/config/main.xml");//读主配置文件
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    接口 jm = sqlSession.getMapper(接口.class);//jm 调用直接接口中的方法
    

      

    主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>
        <!-- 设置配置文件 -->
        <properties resource="com/bwf01/config/sqlConfig.properties">
                <property name="driver" value="driver"/>
                <property name="url" value="url"/>
                <property name="username" value="user"/>
                <property name="password" value="password"/>
        </properties>
        <!-- 设置映射别名   别名是为 Java 类型设置一个短的名字
        <typeAliases>
             <typeAlias type="" alias=""/>         
        </typeAliases> -->
        <settings><!-- 配置日志 -->
        <setting name="logImpl" value="LOG4J"/>
        </settings>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC">            
                </transactionManager>
                <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${user}"/>
                <property name="password" value="${password}"/>
                </dataSource>
            </environment>
        </environments>
    <!-- 设置映射 和接口同名的xml文件-->    
    <mappers>
         <mapper class="com.bwf01.mapper.JobMapper"></mapper>
         <mapper class="com.bwf01.mapper.UserMapper"></mapper>
         <mapper class="com.bwf01.mapper.FirmMapper"></mapper>
         <mapper class="com.bwf01.mapper.AdminMapper"></mapper>
         <mapper class="com.bwf01.mapper.PVMapper"></mapper>
    </mappers>
    </configuration>

    实现接口代理的xml文件:

    1.操作标签(增删改查)的id和接口中方法名一致

    2.数据库字段和类属性的映射对应

    3.配置字段和属性映射时可以使用 extends 

    4.操作标签中可以用动态sql语句

    <?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="com.bwf01.mapper.JobMapper">
      
      <resultMap type="com.bwf01.bean.Job" id="shareselectJob">
          <!--数据库字段和类属性的映射对应  -->
          <id column="jobId" property="jobId"/>
             <result column="jobName" property="jobName"/>
             <result column="workPlace" property="workPlace"/>
             <result column="workType" property="workType"/>
             <result column="numbers" property="numbers"/>
             <result column="salary" property="salary"/>
             <result column="payType" property="payType"/>
             <result column="workDate" property="workDate"/>
             <result column="workTime" property="workTime"/>
             <result column="endDate" property="endDate"/>
             <result column="firmId" property="firmId"/>
             <result column="principal" property="principal"/>
             <result column="tell" property="tell"/>
             <result column="workDetails" property="workDetails"/>
             <result column="status" property="status"/>
             <result column="curentNum" property="curentNum"/>
             <result column="joinStatus" property="joinStatus"/>
      </resultMap>
      
      <resultMap type="com.bwf01.bean.Job" id="selectJob" extends="shareselectJob">
                  <!-- 某个属性是自定义类型,联表查询映射到一个表中的对应字段 -->         
             <association property="firm" javaType="com.bwf01.bean.Firm">
                <id column="firmId" property="firmId"/>
                <result column="logo" property="logo"/>
                <result column="firmName" property="firmName"/>
            </association>
            <!-- 某个属性是自定义类型,联表查询映射到一个表中的对应字段 --> 
            <association property="jobKind" javaType="com.bwf01.bean.JobKind">
                <id column="kindId" property="kindId"/>
                <result column="jobStyle" property="jobStyle"/>
            </association>
      </resultMap>
    <select id="selectJobByPage" parameterType="com.bwf01.vo.JobPage" resultMap="selectJob">
            select job.*,logo,firmName,jobStyle from job INNER JOIN firm join jobkind 
             on job.firmId = firm.firmId and jobkind.kindId = job.kindId 
                        where job.status = '审核通过' and joinStatus = '正在报名'
        <if test="kindId !=0">
        and job.kindId = #{kindId}
        </if>
        <if test="region!=null and region!=''">
         and job.workPlace like '%${region}%'
        </if>                                
        limit ${(page-1)*10},10    
      </select>
    </mapper>
  • 相关阅读:
    Unity项目
    Unity基本操作
    博客园自定义主题样式
    明解C语言
    C指针基础知识
    排序算法
    session理解与总结【session原理、应用、与cookie区别】
    cookie理解与实践【实现简单登录以及自动登录功能】
    【数据结构与算法】双指针思想——数组首部或尾部同向遍历
    【数据结构与算法】双指针思想——首尾双向遍历
  • 原文地址:https://www.cnblogs.com/nijunyang/p/7526734.html
Copyright © 2020-2023  润新知