Mybatis笔记的后续补充

返回值问题

1.返回值为单个属性的时候

//接口中方法设计,返回值为String:
String getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="String">
    select username from t_user where id = #{id}
</select>
//测试类执行:
String username = userDao.getUser(2);
//返回int类型
int id  =  userMapper.getuserid(336);

<select id="getuserid"  resultType="int" parameterType="int">
    select id from t_user where id=#{id}
</select>

2.返回值为多个属性的时候

方式一:采用对象形式

//接口中方法设计,返回值为User:
User getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="com.zx.entity.User">
    select username,age from t_user where id = #{id}
</select>
//测试类执行:
User user = userDao.getUser(2);

方式二:采用map形式处理返回结果

//接口中方法设计,返回值为HashMap:
HashMap getUser02(int id);
//XML中语法:
<select id="getUser02" parameterType="int" resultType="map">
    select username,age from t_user where id = #{id}
</select>
//测试类执行:
HashMap map = userDao.getUser02(2);

返回map的时候,键是属性名称,值就是具体的值,只能返回一条数据,返回多条就会报错

resultType一般都是返回已有的类型,或者你写好的实体类型,可以直接对应的类型。若是字段或者类型,或者不是能很好的直接对应,就需要用自定义对应的类型。

多对一关联映射

resultMap 如果返回值中有其他对象的属性时使用
属性:
id:固定id
type:查询结果的返回类型
子标签:
id:表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性
方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
select: 需要指定命名空间.id去查询关联对象
javaType: 查询结束之后的返回类型

<select id="getUser04" parameterType="int" resultMap="userResult">
    select * from t_user where id = #{id}
</select>
<resultMap id="userResult" type="com.zx.entity.User">
    <id column="id" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="age" property="age"></result>
    <association column="gid" property="group" select="com.zx.dao.GroupDao.getGroupByid" javaType="com.zx.entity.Group"></association>
</resultMap>

这个方法需要注意,select放的是你要二次执行的接口,(简单来说就是一个完整的查询接口,需要有mapper,有xml),查询的内容通常是用了关联字段或外键,没有中间表,但是查询完成以后,还是可以显示关联字段的所有信息。若是没有关联字段,任然需要联查

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
javaType: 查询结束之后的返回类型

association打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getUser05" parameterType="int" resultMap="userResult05">
    select u.id uid,u.username username,u.password password,u.age age,g.id gid,g.gname gname from t_user u,t_group g where u.gid = g.id and u.id = #{id};
</select>

<resultMap id="userResult05" type="com.zx.entity.User">
    <id column="uid" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="age" property="age"></result>
    <association column="gid" property="group" javaType="com.zx.entity.Group">
        <id column="gid" property="id"></id>
        <result column="gname" property="gname"></result>
    </association>
</resultMap>
一对多集合映射

方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

<select id="getGroupByid02" resultMap="groupResult02" parameterType="int">
    select * from t_group where id = #{id}
</select>

<resultMap id="groupResult02" type="com.zx.entity.Group">
    <id column="id" property="id"></id>
    <result column="gname" property="gname"></result>
    <collection column="id" property="users" select="com.zx.dao.UserDao.getUsersByGid" ofType="com.zx.entity.User" ></collection>
</resultMap>

对象对应多个用户 用list展示

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

collection打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getGroupByid03" resultMap="groupResult03" parameterType="int">
    select g.id gid,g.gname gname,u.id uid,u.username username,u.password password,u.age age from t_user u,t_group g where u.gid = g.id and g.id = #{gid}
</select>

<resultMap id="groupResult03" type="com.zx.entity.Group">
    <id column="gid" property="id"></id>
    <result column="gname" property="gname"></result>
    <collection column="gid" property="users" ofType="com.zx.entity.User" >
        <id column="uid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="age" property="age"></result>
    </collection>
</resultMap>

动态SQL

<select id="getUsersif" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user where 1=1
    <if test="username != null">
        and username = #{username}
    </if>
    <if test="age != 0">
        and age = #{age}
    </if>
</select>

<select id="getUserschoose" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user where 1=1
choose就是一个分支选择标签,when就是每一个分支,不管有多少分支,只会执行一个。
<choose>
        <when test="username!=null">
            and username = #{username}
        </when>
        <when test="age!=0">
            and age = #{age}
        </when>
默认选项,所有选项不匹配则进入。
        <otherwise>
            and password = #{password}
        </otherwise>
    </choose>
</select>

<select id="getUserswhere" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user   where  1=1
    <where>他就是咱们的字段 where 
<where>标签会自动处理WHERE子句中的AND和OR关键字,因此在使用时应该合理安排条件语句的位置,避免出现意外结果。
Where只能去除最前面的关键字,他会自动去除第个条件的and或者or 的关键字,
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="age != 0">
            and age = #{age}
        </if>
    </where>
</select>

<select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user
        <trim prefix="where" prefixOverrides="and" suffix="order by">
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="age != 0">
                and age = #{age}
            </if>
        </trim>
        id desc
</select>

<update id="updateUser" parameterType="com.zx.entity.User">
    update t_user
    <set>
        <if test="username != null">
            username = #{username},
        </if>
        <if test="age != 0">
            age = #{age},
        </if>
        <if test="password != null">
            password = #{password},
        </if>
    </set>
    where id = #{id}
</update>

<select id="getUsersforeach" resultType="com.zx.entity.User">
    select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select>

<insert id="addUser" >
    <foreach collection="list" item="user" separator=";">
        insert into t_user values (null,#{user.username},#{user.password},#{user.age})
    </foreach>
</insert>

Insert into user(username,pwd,) values(username,pwd,)

标签也是Mabits动态SQL中常用的一个标签,主要用于快速生成包含WHERE、SET等关键字的SQL语句,同时还能够自动处理SQL语句中的逗号(,)和AND/OR等连接符。

标签通常包含以下属性:

prefixOverrides:表示在生成SQL语句前需要忽略的字符前缀。

suffixOverrides:表示在生成SQL语句后需要忽略的字符后缀。

prefix:表示在生成SQL语句前需要添加的字符串。

suffix:表示在生成SQL语句后需要添加的字符串。

suffixOverrides:表示在生成SQL语句中需要忽略掉的字符串。 // as

<select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user
        <trim prefix="where" prefixOverrides="and" suffix="order by">
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="age != 0">
                and age = #{age}
            </if>
        </trim>
        id desc
</select>

通过使用标签和标签,可以根据用户传入的参数动态生成SQL语句的SET子句。如果参数不为空,则会包含相应的更新内容;否则,该更新内容会被忽略

需要注意的是,由于标签会自动处理SET子句中的逗号(,),因此在使用时应该合理安排更新内容的位置,避免出现意外结果

标签通过将SET子句中的所有条件用逗号(,)连接起来,可以根据不同的更新情况动态生成满足需求的SQL语句

<update id="updateUser" parameterType="com.zx.entity.User">
    update t_user
    <set>
        <if test="username != null">
            username = #{username},
        </if>
        <if test="age != 0">
            age = #{age},
        </if>
        <if test="password != null">
            password = #{password},
        </if>
    </set>
    where id = #{id}
</update>

标签是 MyBatis 中的一个迭代标签,可以对集合对象进行遍历,并生成多条 SQL 语句(如 INSERT、UPDATE、DELETE 等)。

批量添加,导入文件exsl 多条 添加多条,查询列表得到list,把list的内容添加到别的表中

在java代码中遍历添加,sql

批量删除,

使用 标签,我们可以将一个集合对象的元素依次取出,作为 SQL 语句中的参数进行插入、更新或删除操作。常用的语法如下:

其中,各个属性和元素的含义如下

collection:指定要遍历的集合对象的属性名。 参数名

item:指定在遍历过程中每个元素所对应的变量名。 起名字,对应变量名

separator:指定在生成多条 SQL 语句时,不同语句之间的分隔符,默认为英文逗号。

open:指定生成的 SQL 语句的头部。

close:指定生成的 SQL 语句的尾部。

List是单个参数

Item是单个参数的时候用单个参数,

List是对象,

则item对应的是对象名,用法是对象.内容

<select id="getUsersforeach" resultType="com.zx.entity.User">
    select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select>

<insert id="addUser" >
    <foreach collection="list" item="user" separator=";">
        insert into t_user values (null,#{user.username},#{user.password},#{user.age})
    </foreach>
</insert>
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in action之九mybatis 代码生成工具的使用 mybatis SqlSessionDaoSupport的使用附代码下载 转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看中文的:http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html) 写在这个系列前面的话: 以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活。性能也比hibernate好。而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记。后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目中很有可能采用这个ORM工具。所以在此重新温习了一下 mybatis, 因此就有了这个系列的 mybatis 教程. 什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点: 1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。 mybatis实战教程(mybatis in action)之一:开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包。这些软件工具均可以到各自的官方网站上下载。 首先建立一个名字为 MyBaits 的 dynamic web project 1. 现阶段,你可以直接建立java 工程,但一般都是开发web项目,这个系列教程最后也是web的,所以一开始就建立web工程。 2. 将 mybatis-3.2.0-SNAPSHOT.jar,mysql-connector-java-5.1.22-bin.jar 拷贝到 web工程的lib目录. 3. 创建mysql 测试数据库和用户表,注意,这里采用的是 utf-8 编码 创建用户表,并插入一条测试数据 程序代码 程序代码 Create TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值