zoukankan      html  css  js  c++  java
  • mybatis

    Mybatis入门      

    MyBatis是一个支持普通SQL查询存储过程高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

    1、添加Mybatis的配置文件conf.xml

     <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
     3 <configuration>
     4     <environments default="development">
     5         <environment id="development">
     6             <transactionManager type="JDBC" />
     7             <!-- 配置数据库连接信息 -->
     8             <dataSource type="POOLED">
     9                 <property name="driver" value="com.mysql.jdbc.Driver" />
    10                 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
    11                 <property name="username" value="root" />
    12                 <property name="password" value="XDP" />
    13             </dataSource>
    14         </environment>
    15     </environments>
    16     
    17 </configuration>

    2 实体配置

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     3 <!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
     4 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
     5  -->
     6 <mapper namespace="me.gacl.mapping.userMapper">
     7     <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
     8     使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
     9     resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
    10     User类就是users表所对应的实体类
    11     -->
    12     <!-- 
    13         根据id查询得到一个user对象
    14      -->
    15     <select id="getUser" parameterType="int" 
    16         resultType="me.gacl.domain.User">
    17         select * from users where id=#{id}
    18     </select>
    19 </mapper

    使用if+where实现多条件查询

     <select id="getStudentByIf" parameterType="stu" resultType="stu">
            select * from student
           <where>
               <if test="stuAge!=0">
               and stuAge>#{stuAge}
           </if>
                 <if test="stuName!=null">
                     and stuName LIKE '%' #{stuName} '%'
                 </if>
    
           </where>
    
        </select>

    choose  when 分类

    <select id="getAllStudentByLike" parameterType="Map" resultType="stu">
            select * from student
            <where>
            <choose>
                <when test="stuName!=null">
                     stuName like CONCAT('%',#{stuName},'%')
                </when>
                <when test="stuAge!=0">
                     stuAge> #{stuAge}
                </when>
    <otherwise>
        1=1
    </otherwise>
    
    
            </choose>
            </where>
        </select>

     c:使用foreach完成复杂 查询,有三种方式,

     第一种:传入的参数为数组类型

    //传一组 xueshengID 
    public List<student> getStudentBystuId_foreach_array(Integer[] ints);
    
    
    
    
    映射文件配置
     <!--跟据学生id查询学生Interger-->
        <select id="getStudentBystuId_foreach_array" resultMap="studentList">
            select * from student
            <if test="array.length>0">
            where stuId IN
            /*数组形式传入学生Id*/
            <foreach collection="array" item="stu" open="(" separator="," close=")">
                  #{stu}
            </foreach>
            </if>
        </select>

         第二种:传入list集合

     <!--跟据学生id查询学生list方式-->
        <select id="getStudentBystuId_foreach_list" resultMap="studentList">
            select * from student
            <if test="list.size>0">
                where stuId IN
            /*集合形式传入学生Id*/
            <foreach collection="list" item="stu" open="(" separator="," close=")">
                #{stu}
            </foreach>
            </if>
        </select>

     第三种:根据Map集合

     <!--跟据学生id查询学生map方式-->
        <select id="getStudentBystuId_foreach_map" resultMap="studentList">
            select * from student where stuId IN
            /*集合形式传入学生Id*/
            <foreach collection="stuId" item="stu" open="(" separator="," close=")">    <!--collection是自己定义的,就是map的key值-->
                #{stu}
            </foreach>
        </select>
  • 相关阅读:
    5.19 省选模拟赛 T1 小B的棋盘 双指针 性质
    5.15 省选模拟赛 容斥 生成函数 dp
    5.15 省选模拟赛 T1 点分治 FFT
    5.15 牛客挑战赛40 B 小V的序列 关于随机均摊分析 二进制
    luogu P4929 【模板】舞蹈链 DLX
    CF 878E Numbers on the blackboard 并查集 离线 贪心
    5.10 省选模拟赛 拍卖 博弈 dp
    5.12 省选模拟赛 T2 贪心 dp 搜索 差分
    5.10 省选模拟赛 tree 树形dp 逆元
    luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
  • 原文地址:https://www.cnblogs.com/xzjf/p/7450760.html
Copyright © 2011-2022 走看看