MyBatis/MySQL操作

工作中用到的关于mybatis的一些操作,从简单到复杂,慢慢更新……
同时也会加一些关于MySQL的部分知识….


mybatis部分:

  • 关于resultType 和resultMap的区别
    他们都是代表返回类型,不同的是:
    resultType是直接表示返回类型的(对应着我们的model对象中的实体)。
    resultMap是对实体类的引用,需要提前定义好。(它是实体类与数据库表中的映射)
    注意:resultType和resultMap不可以同时存在,同样用resultType使用映射的resultMap会报错。同理,相似于parameterType和parameterMap。

关于resultType 和resultMap的区别详细可参考

http://www.tuicool.com/articles/ju2Y7n

resultType="com.entity.demo"

resultMap = "demo"

关于resultMap的定义

<resultMap type="com.entity.demo" id="demo">
        <!-- property 代表model中的名称 column 代表数据库表中的字段名称 -->
        <!-- 可以加jdbcType,可以不加 -->
        <id property="pk" column="id" jdbcType="VARCHAR"*></id>
        <result property="corpName" column="corpname" />
        <result property="taxPayerNo" column="taxpayerno" />
</resultMap>

关于的使用

<sql id = "colum">
    ID,
    NAME,
    PASSWORD
</sql>

具体的例子:

<select id="demo" parameterType = "java.lang.String" resultMap="demo">
    select 
    <include refid = "colum">
    where id = #{id}
</select>
或
<select id="demo" parameterType = "java.lang.String" resultType="com.entity.demo">
    select 
    <include refid = "colum">
    where id = #{id}
</select>

简单的增删查改

<mapper namespace="com.demoDao">

<insert id="" parameterType="" resultType="" useGeneratedKeys="true" keyProperty="id">
    insert into table (id, name, age)
    value(#{id}, #{name}, #{age})
</insert>
//useGeneratedKeys="true" keyProperty="id"的意思是如果主键是自增的,那么返回主键值keyProperty后写主键的key
<select id="" parameterType="" resultType="">
    select * from table where id = #{id}
</select>
<update id="" parameterType="" resultType="">
    update table
    set
    name= #{name}
    age = #{age}
    where id = #{id}
</update>
<delete id="" parameterType="" resultType="">
    delete from table
    where id = #{id}
</delete>

</mapper>

mybatis的进阶查询

查询id在一个list中的数据

<select id="" parameterType="list" resultType="">
    select * 
    from table 
    where id in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
    #{item}
    </foreach>
</select>

多条件的查询

Map<String, String> map = new HashMap<String, String>();
map.put(key1,name);
map.put(key2,23);
map.put(key3,1);
<select id="" parameterType="map" resultType="">
    select #{key1}
    form table
    where age = #{key2} and id = #{key3}
</select>

<delete id="" parameterType="map" resultType="">
    delete a, b, c
    from table1 a, table2 b, table3 c
    where a.name=#{key1} and b.age = #{key2} and c.id = #{id}
</delete>
//使用and链接,含义如同&&,一个条件不成立则都无法删除;也可以使用or链接,其含义相当于 ||

多表的联合查询
这里写图片描述

<resultMap id="queryForListMap" type="com.sica.domain.User">  
        <id column="id" property="id" jdbcType="VARCHAR"/>  
        <result column="username" property="username" jdbcType="VARCHAR"/>  
        <result column="password" property="password" jdbcType="VARCHAR"/>  
        <collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role">  
            <id column="r_id" property="id" jdbcType="VARCHAR" />  
            <result column="r_name" property="name" jdbcType="VARCHAR" />  
            <result column="r_jsms" property="jsms" jdbcType="VARCHAR" />  
            <result column="r_bz" property="bz" jdbcType="VARCHAR" />  
            <result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />  
            <result column="r_glbm" property="glbm" jdbcType="VARCHAR" />  
        </collection>  
    </resultMap>  
    <select id="queryForList" resultMap="queryForListMap">  
        SELECT  
          u.id,  
          u.username,  
          u.password,  
          r.id r_id,  
          r.name r_name,  
          r.jsms r_jsms,  
          r.bz r_bz,  
          r.jlzt r_jlzt,  
          r.glbm r_glbm  
        FROM  
          user u  
        LEFT JOIN  
          role r  
        ON  
          u.id = r.userid  
    </select>  

参考网址:

http://blog.csdn.net/happylee6688/article/details/45967763

where 语句增加if判断

<select id="" parameterType="" resultMap="">     
    SELECT * from STUDENT ST      
    <if test="studentName!=null and studentName!='' ">     
        WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
    </if>     
</select> 
<!-- 查询学生list,like姓名,=性别 -->     
<select id="" parameterType="" resultMap="">     
    SELECT * from STUDENT_TBL ST      
    <where>     
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
    </where>     
</select>  

set标签

<update id="" parameterType="">
        update
        intt
        set
        <set>
            <if test="address != null and address != '' ">address = #{address},</if>
            <if test="phone != null and phone != '' ">phone = #{phone},</if>
            <if test="revEmail != null and revEmail != '' ">revemail = #{revEmail},</if>
            <if test="revPhone != null and revPhone != '' ">revphone = #{revPhone}</if>
        </set>
        where id = #{id}

    </update>

choose标签

<!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose -->     
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
    <where>     
        <choose>     
            <when test="studentName!=null and studentName!='' ">     
                    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
            </when>     
            <when test="studentSex!= null and studentSex!= '' ">     
                    AND ST.STUDENT_SEX = #{studentSex}      
            </when>     
            <when test="studentBirthday!=null">     
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday}      
            </when>     
            <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">     
                AND ST.CLASS_ID = #{classEntity.classID}      
            </when>     
            <otherwise>     

            </otherwise>     
        </choose>     
    </where>     
</select> 

mybatis多个参数传递的一种解决方法:

public String selectNameFromStudents(@Param("id") String id, @Param("class") String class)
//传的参数名以()中的为主
<select id="selectNameFromStudents" resultType="java.lang.String">
    select name
    form Students
    where id = #{id} and class = #{class}
</select>

mybatis中如果涉及到>,>=,<,<=需要进行转义 (;gt > ) (;lt <) (;ge >=)(;le <=)

涉及到动态表名等需要进行一个设置,去掉mybatis的预编译。即 statementType
它拥有三个选择:STATEMENT,PREPARED和CALLABLE,对应的分别为statement(非预编译的),preparedstatement(预编译的)和CallableStatement

 statementType="STATEMENT"
 此时:#{}将不可以使用,转为使用${}

预编译(preparedstatement)和非预编译(statement)对比:

  1. 代码的可读性和可维护性. preparedstatement肯定是胜出的。
  2. PrepareStatement对于批量处理可以大大提高运行效率。每一种数据库都会尽最大努力对预编译语句提供最大的性能优化.因为预编译语句有可能被重复调用.所以语句在被DB的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中(相当于一个涵数)就会得到执行.这并不是说只有一个Connection中多次执行的预编译语句被缓存,而是对于整个DB中,只要预编译的语句语法和缓存中匹配.那么在任何时候就可以不需要再次编译而可以直接执行.而statement的语句中,即使是相同一操作,而由于每次操作的数据不同所以使整个语句相匹配的机会极小,几乎不太可能匹配。当然如果只是用一次或少次,使用statement肯定是好的,预编译的会产生较大的开销。
  3. 极大地提高了安全性.

关于union和union all
这是两个select语句的结果作为一个整体显示出来需要使用的东西。
区别:

  1. Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
  2. Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
  3. Intersect:对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
  4. Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

example:
select * from table_a where name = "john"
union all
select * from table_b where name = "john"

MySQL部分:

建表的时候因为不知道,就建立了一个myisam类型的,大牛说要改成innerdb。但小白表示并不能懂,于是就查了资料:

  1. myisam和innerdb都是MySQL建表的常用类型,M是强调性能,不支持事务处理等高级处理,不支持事务但是执行速率由于I。I则是支持事务等高级处理。
  2. I不支持FULLTEXT类型索引,且不保存行数,故select count(*)的时候会扫描整个表来计算。
  3. AUTO_INCREMENT字段,I必须有其索引,M则可以和其他建立联合索引。
  4. DELETE FROM table时,I不会重新建立表,而是一行一行的删除。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值