文章目录
一、insert.标签
1.简单应用
- 属性介绍
- id: 命名空间中的唯一标识符,可用来代表这条语句
- flushCache:默认值为true,任何时候只要语句被调用,都会清空一级和二级缓存
- timeout:设置在抛出异常之前,驱动程序等待数据库返回请求结果的秒数
- statementType:接口分为statement、prepared(默认)、callable。 MyBatis对应statement、preparedStatement、CallableStatement
- useGeneratedKeys:默认值为false。如果设置为true,MyBatis会使用JDBC的getGeneratedKeys()方法来取出数据库内部生成的主键
- keyProperty:MyBatis通过getGeneratedKeys()获取主键值后将要赋值的属性名。多参数逗号(,)隔开
- keyColumn:仅对和标签有用。通过生成键值设置表中的列名(在PostGreSQL中必须使用,当主键列不是表中第一列时需要设置!)配合selectKey使用
- databaseId:配置databaseIdProvider后,MyBatis会加载所有不带或匹配当前databaseId的语句。如果同时存在带databaseid和不带的语句,会忽略后者。
- javaTpe为防止类型出错,对于一些特殊的数据类型,建议制定具体的jdbcTpe值
<!-- mapper.xaml 中 -->
<insert id="insert" > <!-- id属性为 接口中的方法名 -->
insert into tb_adm values(null,#{adm_name},#{adm_post}) <!-- 动态参数 #{接口中方法参数的名字}-->
</insert>
2.使用JDBC方式返回主键自增的值
<insert id="insert" useGeneratedKeys="true" keyProperty="tb_adm">
insert into tb_adm values(tb_adm,#{adm_name},#{adm_post}) <!-- #{adm_id} 也可以 -->
</insert>
二、update.标签和delete.标签
1.update.标签的用法
//java
public boolean update(Tb_adm emp); //参数为实体类可以 传递多个参数!
<!-- xml -->
<update id="update">
update tb_adm set adm_name=#{adm_name}, adm_post=#{adm_post} where adm_id=#{adm_id}
</update>
2.delete.标签的使用
//java
public boolean deleteById(String id);
<!-- xml -->
<delete id="deleteById">
delete from tb_adm where adm_id=#{adm_id}
</delete>
(删除主从表数据时 需要把外键的删除 设置为级联删除或者设置为null!)
三、映射器方法多参数传递
1. 默认值映射
当javaBen实体类中的 属性名和数据完全一样时 可u以不用写字段映射 否则MyBatis查不出数据**!**
2.映射字段 不一样时
(1) 别名映射
//java
private String adm_id;
private String adm_name;
private String adm_post;
<!-- xml -->
<select id="select" resultType="com.xqh.model.Tb_adm"> <!-- 接受的实体类 -->
select id adm_id, name adm_name, post adm_post from tb_adm <!-- 起别名来对应java实体类中的 属性名 -->
</select>
(2)手动映射resultMap
//java
private String adm_id;
private String adm_name;
private String adm_post;
<!-- xml -->
<resultMap id="map01" type="TbAuthor"> <!-- id属性选择对应 resultMap值 type属性为接收类型 -->
<id property="adm_id" column="id"/> <!-- property为java实体类的属性值 column为数据库中的字段 -->
<result property="adm_name" column="name"/>
<result property="adm_post" column="post"/>
</resultMap>
<select id="select" resultMap="map01"> <!-- resultMap自定义名字 -->
select id adm_id, name adm_name, post adm_post from tb_adm <!-- 起别名来对应java实体类中的 属性名 -->
</select>
3.多参数传递映射关系
(1)单个参数传递时 MyBatis会自动识别
(2)多个参数传递时 MyBatis识别不出来
parameters are [arge1, arge2, arge3…,\ param1,param2,param3,…]
-1. 使用默认的参数名
List<TbAdmi> selectById(String adm_id,String adm_name);//多参数
<!-- xml -->
<select id="selectById" resultType="com.xqh.model.Tb_adm">
select * from tb_adm where adm_id=#{arge1} and adm_name=#{arge2} <!-- 或者#{param1} #{param2} -->
</select>
-2.使用Map传递参数(实体类类型也可以)
//java接口
List<TbAdmi> selectById(Map map);//map
List<TbAdmi> selectById(TbAdmi tb_admi);//实体类
<!-- xml -->
<select id="selectById" resultType="com.xqh.model.Tb_adm"> <!-- map -->
select * from tb_adm where adm_id=#{adm_id} and adm_name=#{adm_name}
</select>
//javaTest
public void select(){
myBatisSession(); //此方法为 MyBatis调用sqlSession的工具列 下面MyBatisUnit同理
HashMap<String, String> map = new HashMap<>();
map.put("adm_id","2"); //key值对应参数名才可以使用!
map.put("adm_name","yuyan");
ArrayList<Tb_adm> tb_adms = session.getMapper(AdmiMapper.class).selectById(map);
for (Tb_adm tb_adm :
tb_adms) {
System.out.println(tb_adm);
}
session.close();
}
//实体类
public void select02(){
Tb_adm tb_adm = new Tb_adm();
tb_adm.setAdm_name("erer");
tb_adm.setAdm_id("113");
ArrayList<Tb_adm> tb_adms = MyBatisUnit.session.getMapper(AdmiMapper.class).selectById(tb_adm);
for (Tb_adm tb_adm :
tb_adms) {
System.out.println(tb_adm);
}
MyBatisUnit.session.close();
}
-3. 使用注解注解类型作为参数
//java接口
TbAdmi selectById(@Param("adm_id")String adm_id, @Param("adm_name")String adm_name);
<!-- xml -->
<select id="selectById" resultType="com.xqh.model.Tb_adm">
select * from tb_adm where adm_id=#{arge1} and adm_name=#{arge2}
</select>
//javaTest
//实体列
public void selectById(){
MyBatisUnit.myBatisSession();
TbAdmi tb_admi = MyBatisUnit.session.getMapper(AdmiMapper.class).selectById("1","yuyan02");//可直接写参数值!
System.out.println("tb_admi");
MyBatisUnit.session.close();
}
四、MyBatis注解的基本用法
注解属性
//java
@Select("select * from tb_adm")
ArrayList<Tb_adm> select();
@Select("select * from tb_adm where adm_id = #{adm_id}")
Tb_adm selectById(int id);
@Insert("insert into tb_adm values(null,#{adm_name},#{adm_post})")
boolean insert(Tb_adm emp);
@Delete("delete from tb_adm where adm_id = #{adm_id}")
boolean delete(int id);
@Update("update tb_adm set adm_name=#{adm_name}, adm_post=#{adm_post} where adm_id=#{adm_id}")
boolean update(Tb_adm emp);