设为首页 收藏本站
查看: 616|回复: 0

[经验分享] mybatis 一些总结

[复制链接]

尚未签到

发表于 2016-11-24 06:22:02 | 显示全部楼层 |阅读模式
  最近用mybatis开发,一些总结:



  • 结合spring框架,需要在spring配置文件中加入sessionFactory定义:          

    <!-- 创建SqlSessionFactory,同时指定数据源 -->
    <bean id="sqlSessionFactory"
    class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="dataSource" ref="dataSource" />
    </bean>

  • 定义mybatis-config.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>  
    <typeAliases>  
    <typeAlias alias="App" type="com.cyou.appserver.entity.App"/>  
    <typeAlias alias="Feedback" type="com.cyou.appserver.entity.Feedback"/>   
    <typeAlias alias="RecommendApp" type="com.cyou.appserver.entity.RecommendApp"/>
    <typeAlias alias="UpdateInfo" type="com.cyou.appserver.entity.UpdateInfo"/>
    <typeAlias alias="DeviceToken" type="com.cyou.appserver.entity.DeviceToken"/>
    <typeAlias alias="Cards" type="com.cyou.appserver.entity.Cards"/>
    <typeAlias alias="UnableVideoLog" type="com.cyou.appserver.entity.UnableVideoLog"/>
    <typeAlias alias="Battle" type="com.cyou.appserver.entity.Battle"/>
    <typeAlias alias="Card" type="com.cyou.appserver.entity.Card"/>
    <typeAlias alias="Occupational" type="com.cyou.appserver.entity.Occupational"/>
    <typeAlias alias="Licensing" type="com.cyou.appserver.entity.Licensing"/>
    <typeAlias alias="Pictures" type="com.cyou.appserver.entity.Pictures"/>
    <typeAlias alias="Timetable" type="com.cyou.appserver.entity.Timetable"/>
    <typeAlias alias="IndexImage" type="com.cyou.appserver.entity.IndexImage"/>
    <typeAlias alias="CardStat" type="com.cyou.appserver.entity.CardStat"/>
    <typeAlias alias="AppPush" type="com.cyou.appserver.entity.AppPush" />
    <typeAlias alias="Formation" type="com.cyou.appserver.entity.Formation" />
    <typeAlias alias="FormationParise" type="com.cyou.appserver.entity.FormationParise" />
    <!--        <typeAlias alias="AppConfig" type="com.cyou.appserver.entity.AppConfig"/>
    <typeAlias alias="AppMenus" type="com.cyou.appserver.entity.AppMenus"/>
    <typeAlias alias="AppClick" type="com.cyou.appserver.entity.AppClick"/>-->
    </typeAliases>
    <mappers>
    <mapper resource="mybatis/Ad.xml" />
    <mapper resource="mybatis/App.xml" />
    <mapper resource="mybatis/Feedback.xml" />
    <mapper resource="mybatis/RecommendApp.xml" />
    <mapper resource="mybatis/UpdateInfo.xml" />
    <mapper resource="mybatis/DeviceToken.xml" />
    <mapper resource="mybatis/Cards.xml" />
    <mapper resource="mybatis/UnableVideoLog.xml" />
    <mapper resource="mybatis/Battle.xml" />
    <mapper resource="mybatis/Card.xml" />
    <mapper resource="mybatis/Occupational.xml" />
    <mapper resource="mybatis/Licensing.xml" />
    <mapper resource="mybatis/Pictures.xml" />
    <mapper resource="mybatis/Timetable.xml" />
    <mapper resource="mybatis/IndexImage.xml" />
    <mapper resource="mybatis/CardStat.xml" />
    <mapper resource="mybatis/AppPush.xml" />
    <!-- <mapper resource="mybatis/AppConfig.xml" />
    <mapper resource="mybatis/AppMenus.xml" />
    <mapper resource="mybatis/AppClick.xml" /> -->
    <mapper resource="mybatis/Formation.xml" />
    <mapper resource="mybatis/FormationParise.xml" />
    </mappers>
    </configuration>

  • 定义具体的域对象xml文件,比如Formation.xml
    <?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="Formation">
    <select id="list" resultType="Formation" parameterType="map">
    SELECT f.*,count(p.formationId) as praiseCount
    FROM
    formation f LEFT JOIN formation_parise p ON f.id = p.formationId
    WHERE
    f.appId=#{appId}
    <if test="level != null">  
    and f.level = #{level}  
    </if>
    <if test="query2 != null">
    and f.createTime &gt; #{query2}
    </if>
    GROUP BY
    f.id
    <if test="query1 != null">
    ORDER BY
    f.${query1}
    </if>
    LIMIT #{offset},#{limit}
    </select>
    <select id="countByParams" resultType="int" parameterType="map">
    SELECT count(f.id)
    FROM
    formation f
    WHERE
    f.appId=#{appId}
    <if test="level != null">  
    and f.level = #{level}  
    </if>
    <if test="query2 != null">
    and f.createTime &gt; #{query2}
    </if>
    </select>
    <select id="totalCount" resultType="int" parameterType="int">
    select count(*) from formation where appId=#{id}
    </select>
    <insert id="insert" parameterType="Formation" keyProperty="id" useGeneratedKeys="true">
    <!-- <selectKey keyProperty="id" resultType="int">select nextval('formation')</selectKey> -->
    insert into formation(appId, name, remark, imageUrl,createTime,contentUrl,commentCount,imei,level,deviceType)
    values (#{appId}, #{name}, #{remark}, #{imageUrl},#{createTime},#{contentUrl},#{commentCount},#{imei},#{level},#{deviceType})
    </insert>
    <update id="update" parameterType="Formation">
    update formation set
    <!--    appId=#{appId}, name=#{name}, remark=#{remark}, image=#{image}, -->
    topicId=#{topicId} where id=#{id}
    </update>
    <delete id="delete" parameterType="int">
    delete from formation where id=#{id}
    </delete>
    <select id="findById" resultType="Formation" parameterType="int">
    select f.*,count(p.id) as praiseCount from formation f,formation_parise p where p.id=#{id} and f.id = p.formationId
    </select>
    <update id="updateCommentCount" parameterType="int">
    update formation set commentCount = commentCount + 1 where id=#{id}
    </update>
    <update id="updatePraiseCount" parameterType="map">
    update formation set praiseCount = praiseCount+1 where id=#{id}
    </update>
    </mapper>
     

  •   在java中调用:首先需要在bean中引用factory对象:

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
     

  • 然后使用名称调用
    Map<String,Object> queryMap = new HashMap<String, Object>();
    queryMap.put("appId", appId);
    queryMap.put("offset", (pageNum-1) * pageSize);
    queryMap.put("limit", pageSize);
    List<Formation> list = (List<Formation>) sqlSessionTemplate.selectList("Formation.list", queryMap);
     
  • 需要注意的是,传递的参数在mybatis里可以使用#{变量名},标示带引号的,比如appId=#{appId},和${变量名},不带引号的,比如order by f.${query}.这一点要灵活运用。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-304564-1-1.html 上篇帖子: Mybatis Tool简介 下篇帖子: MyBatis的使用(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表