一、使用MyBatis
定义sql映射xml文件
userMapper.xml文件的内容如下:
<!--头文件-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:
命名空间:区分不同空间下的同名SQLID
A: findlAll
B: findAll
-->
<mapper namespace="cn.happy.dao.IStudentInfoDAO">
<!--SQL标签
id:唯一锁定到SQL标识
paramenterType:SQL语句的入参 可以省略
resultType:
增删除操作:不能 写
查询:单个实体的类型
-->
<sql id="columns">
stuid,stuname,stuage,studate
</sql>
<resultMap id="studentMap" type="StudentInfo">
<!-- <result column="stuname2" property="stuName"></result>-->
</resultMap> <select id="findAll" resultMap="studentMap">
/*SQL文:SQL语句*/
select <include refid="columns"></include> from studentinfo
</select>
<!--按主键查询-->
<select id="getStudentById" resultType="StudentInfo">
select * from studentinfo WHERE stuid=#{stuId}
</select>
<!--添加学生-->
<insert id="addStudent">
insert into studentinfo( stuName,stuAge,stuDate) VALUES (#{stuName},#{stuAge},#{stuDate})
</insert> <!--修改学生-->
<update id="updateStudent">
update studentinfo set stuName= #{stuName} WHERE stuId=#{stuId}
</update> <!--删除学生-->
<delete id="deleteStudent">
delete from studentinfo WHERE stuId=#{stuId}
</delete> <!--模糊查询-->
<select id="findStudentListLike" resultType="StudentInfo">
<!--select * from studentinfo where stuname like concat('%',#{stuName},'%') and stuAge>#{stuAge}-->
select * from studentinfo where stuname like '%${stuName}%' and stuAge>#{stuAge}
</select> <!--多条件查询-->
<select id="findStudentsByCondition" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{stuName} '%' and stuAge>#{stuAge}
</select> <!--多条件查询使用索引-->
<select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
</select> <!--智能标签foreach List-->
<select id="findByForeachListStudent" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.stuId}
</foreach>
</if>
</where>
</select>
<!--智能标签foreach List-->
<select id="findByForeachList" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stuno">
#{stuno}
</foreach>
</if>
</where>
</select>
<!--智能标签foreach Array-->
<select id="findByForeachArray" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="array.length>0">
stuid in
<foreach collection="array" open="(" close=")" separator="," item="stuno">
#{stuno}
</foreach>
</if>
</where>
</select> <!--智能标签choose-->
<select id="findByChoose" resultType="StudentInfo">
select * from studentinfo
<where>
<choose>
<when test="stuName!=null">
and stuName like '%' #{stuName} '%'
</when>
<when test="stuAge!=null">
and stuAge>#{stuAge}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</where>
</select> <!--智能标签if-->
<select id="findByIf" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="stuName!=null"><!--用户录入的姓名字段-->
and stuName like '%' #{stuName} '%'
</if>
<if test="stuAge!=null">
and stuAge>#{stuAge}
</if>
</where>
</select>
</mapper>
一、使用MyBatis的更多相关文章
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- mybatis plugins实现项目【全局】读写分离
在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...
- MyBatis基础入门--知识点总结
对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MyBatis源码分析(一)开篇
源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...
- (整理)MyBatis入门教程(一)
本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
随机推荐
- 测试jdbc连接下,mysql和mycat的吞吐性能
最近一个项目需要数据库有较大的吞吐量,因为项目要求的访问量和数据量较大,决定采用一个数据库中间件来对数据库进行管理.经过一番查询,决定使用阿里的一个开源项目-mycat.因为mycat基于mysql, ...
- hdu-5734 Acperience(数学)
题目链接: Acperience Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- codevs 2456栅栏
传送门 2456 栅栏 2006年省队选拔赛四川 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Maste 题目描述 Description 农夫约翰打算建立一个栅 ...
- UItextFied 的属性
网络学习笔记 在iPhone应用中通过UITextField填写信息时,经常出现出现自动更正输入信息.首字母大写等情况 尤其是在填写用户名时,这种本想提供便捷的功能反而让人感到特别麻烦 今天查了相关书 ...
- mina框架之---服务端NioSocketAcceptor的学习
接上一讲对mina的简单应用和对mina服务端和客户端中几个重要的技术知识点的理解后,今天着重对mina服务端的NioSocketAcceptor 进行学习. 说这个玩意之前,先整体上看一下在mina ...
- 调试 Hadoop 源代码
环境是 64bit Ubuntu 14.04 系统, jdk 1.7 以及 Eclipse Mars (4.5) 这里介绍两种调试 Hadoop 源代码的方法: 利用 Eclipse 远程调试工具和打 ...
- Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts
https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...
- eclipse编译Jmeter源码
1.在apache官网下载源码和安装包 http://jmeter.apache.org/ 2. 解压 解压安装包和源码包, 将安装包apache-jmeter-3.3 里lib ...
- python数据分析笔记中panda(1)
1 例子1 from pandas import read_csv; df = read_csv('H://pythonCode//4.1//1.csv') df 截图 1.1 修改表的内容编码 df ...
- JAVA基础--JAVA API集合框架(ArrayList、HashSet、HashMap使用)14
一.集合Collection 1. 集合介绍 变量:表示的内存中的一个空间,只能保存确定类型的单个数据 数组:表示的是内存中的多个连续的空间,这些空间中可以存储多个同类型的数据. 后期继续学习面向对象 ...