Mybatis的基本操作--增删改查
目录
查看数据
无参数
一个参数
多个参数
添加数据
修改数据
删除数据
注释的方式进行查找数据
查看数据
分三种情况:无参,有一个参数,有多个参数的情况。
(这里的详细操作步骤是博主的上一篇博客写的:初识Mybatis,并创建第一个Mybatis项目(详细图文教程))
无参数
我们在接口中声明,在对应的xml文件中进行实现接口,下面简单展示一下注意点。
接口:
xml文件:
进行测试:
测试结果:
可以看到返回的结果就是我们连接的数据库中的结果。
一个参数
我们传递参数的时候,会用到一个注解 @Param 。传递单个参数的时候,注解可以不用,也可以使用。使用注解就要和注解中的值保持一致,不适用注解的时候,接口中和 xml 中的值可以不一样。
//接口声明
import com.example.springmybatisdemo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;//接口是用来声明方法的
@Mapper
public interface UserMapper {/*** 不带参数情况* @return*/List<User> userAll();/*** 一个参数的情况1:接口中的参数和xml文件的参数名称对应* @param id* @return*/User userById(Integer id);/*** 一个参数的情况2:使用传参注解,注解中的值要和后边XML文件中的值对应* 注解中的值可以和参数一样也可以不一样* @param id* @return*/User userById2(@Param("uid")Integer id);/*** 一个参数的情况3:接口中的参数和XML文件的参数可以不对应* @param aaaa* @return*/User userById3(Integer aaaa);
}
<!-- xml文件-->
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybati
s.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springmybatisdemo.mapper.UserMapper"><select id="userAll" resultType="com.example.springmybatisdemo.model.User">select * from userinfo;</select><select id="userById" resultType="com.example.springmybatisdemo.model.User">select * from userinfo where id=#{id}</select><select id="userById2" resultType="com.example.springmybatisdemo.model.User">select username from userinfo where id=#{uid}</select><select id="userById3" resultType="com.example.springmybatisdemo.model.User">select * from userinfo where id=#{oooo}</select></mapper>
多个参数
多个参数的时候就必须要用到注解@Param,且注解中的值要和xml接收参数的值一样。
//接口/*** 多个参数的情况,注解中的值要和XML文件中接收的值对应* @param name* @param id* @return*/User userByNameAndId(@Param("name")String name,@Param("id")Integer id);
<select id="userByNameAndId" resultType="com.example.springmybatisdemo.model.User">select * from userinfo where name= #{name} and id= #{id}
</select>
添加数据
可以一个一个的增加属性,但是这样太繁琐,我们可以直接传递对象。传递对象的时候可以直接传递一个默认的,也可以传递一个指定名称的对象,传递默认的对象的时候,Mybatis会自动帮我们生成 以属性名命名的变量。传递指定对象的时候,我们取值的时候,就是 对象名.属性 的方式取值。
/*** 插入一个对象* @param user* @return*/Integer insert(User user);/*** 插入一个指定名称的对象* @param user* @return*/Integer insert2(@Param("user1") User user);
<insert id="insert">insert into userinfo(username,password,photo)values(#{username},#{password},#{photo})</insert><insert id="insert2">insert into userinfo(username,password,photo)values(#{user1.username},#{user1.password},#{user1.photo})</insert>
获取自增的id
修改数据
修改数据的时候,也可以传入属性或者传入对象,传入属性的话,方法传参直接传递的就是要修改的值,传入对象的话,传入的是一个新的对象,设置新的对象的属性,用这个新的对象去替换之前的值。
/*** 修改数据(更新数据):传入的是对象* @param user*/void update(User user);/*** 直接传入要修改的属性* @param username* @param id*/void update2(String username,Integer id);
<update id="update">update userinfo set username=#{username},password=#{password} where id=#{id}</update><update id="update2">update userinfo set username=#{username},id=#{id} where id=#{id}</update>
删除数据
/*** 删除数据* @param id*/void delete(Integer id);
<delete id="delete">delete from userinfo where id=#{id}</delete>
注释的方式进行查找数据
Mybatis提供了注释的方式去对数据库进行操作,但是注释的方式不是很建议新手去使用,因为涉及到注释的时候,代码错了没有提示,其二就是涉及动态SQL的时候比较繁琐和复杂。
这里给大家展示一下注释方式的大概使用,也推荐大家在SQL语句简单的时候使用。
//增加@Insert("insert into articleinfo(uid,title,content) values(#{uid},#{title},#{content}) ")void insert2(Article a);
//查找@Select("select * from articleinfo where uid=#{uid}")List<Article> quaryAll(Integer uid);