1.根据id查询
package com.shuai.dto;
import lombok.Data;
@Data
public class people {
private String name;
private Integer age;
}
pojo类这里使用了lombok插件顶替了get,set,tostring的一些繁琐的配置
映射文件中
<select id="findOne" resultType="com.shuai.dto.people" parameterType="java.lang.Integer">
select * from people where age=#{age};
</select>
parameterType指的是需要传送的查询的条件。
people people = sqlSession.selectOne("peopleMapper.findOne", 2);
System.out.println(people);
这里使用selectOne返回了单个类
2.添加:
测试,传入的是people,由于sqlsession默认的是不提交的,所以,这里需要手动提交事务。
people people=new people();
people.setName("xiaohu");
people.setAge(4);
sqlSession.insert("peopleMapper.save",people);
sqlSession.commit();
映射文件
<insert id="save" parameterType="com.shuai.dto.people">
insert into people values(#{name},#{age});
</insert>
3.更新:
映射文件:
<update id="update" parameterType="com.shuai.dto.people">
update people set name=#{name} where age=#{age}
</update>
测试:
people people=new people();
people.setName("留言");
people.setAge(2);
sqlSession.insert("peopleMapper.update",people);
sqlSession.commit();
4.删除:
测试:
people people=new people();
people.setName("留言");
people.setAge(2);
sqlSession.insert("peopleMapper.update",people);
sqlSession.commit();
映射:
<delete id="delete" parameterType="java.lang.Integer">
delete from people where age=#{age}
</delete>
5.#{}与必须是value
没有要求
当传的是pojo类型的时候
$会直接拼接上去
网友评论