高端网站建设价钱,网站改版需要注意,做网站要用框架吗,建设网站询价对比表模板目录
查看数据
无参数
一个参数
多个参数
添加数据
修改数据
删除数据
注释的方式进行查找数据 查看数据
分三种情况#xff1a;无参#xff0c;有一个参数#xff0c;有多个参数的情况。 #xff08;这里的详细操作步骤是博主的上一篇博客写的#xff1a;初识My…目录
查看数据
无参数
一个参数
多个参数
添加数据
修改数据
删除数据
注释的方式进行查找数据 查看数据
分三种情况无参有一个参数有多个参数的情况。 这里的详细操作步骤是博主的上一篇博客写的初识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*/ListUser 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 version1.0 encodingUTF-8?!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybati
s.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.springmybatisdemo.mapper.UserMapperselect iduserAll resultTypecom.example.springmybatisdemo.model.Userselect * from userinfo;/selectselect iduserById resultTypecom.example.springmybatisdemo.model.Userselect * from userinfo where id#{id}/selectselect iduserById2 resultTypecom.example.springmybatisdemo.model.Userselect username from userinfo where id#{uid}/selectselect iduserById3 resultTypecom.example.springmybatisdemo.model.Userselect * 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 iduserByNameAndId resultTypecom.example.springmybatisdemo.model.Userselect * 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 idinsertinsert into userinfo(username,password,photo)values(#{username},#{password},#{photo})/insertinsert idinsert2insert 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 idupdateupdate userinfo set username#{username},password#{password} where id#{id}/updateupdate idupdate2update userinfo set username#{username},id#{id} where id#{id}/update
删除数据 /*** 删除数据* param id*/void delete(Integer id); delete iddeletedelete 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})ListArticle quaryAll(Integer uid);