MyBatis常用注解:
这是基于注解的映射方式,实现对数据的增删改查,将sql语句直接写在注解的括号中
这是一个接口,其不需要类去实现它
@Insert简单插入
@Insert("insert into student(name,age) values(#{name},#{age})")
public void addStudent(Student student) throws Exception;
@Upload简单修改
// 根据id修改姓名 ,方法的返回值代表修改的总数
@Update("update student set name = #{arg0} where id = #{arg1}")
public Integer updateNameById(String name, Integer id) throws Exception;
@Delete简单删除
// 根据id删除学生,方法的返回值代表删除的总数
@Delete("delete from student where id = #{id}")
public Integer deleteStudentById(Integer id) throws Exception;
@Select简单查询
resultMap:
将查询结果集合中的列一一映射到java对象的各个属性,常用于多表查询以及查询时使用别名的情况.
当实体类跟表中属性名不一致,
在resultMap标签中做实体类中属性名和表中字段名之间的映射
id: 当前resultMap映射结果
property: 实体类中属性名
column: 表中字段名称
<resultMap type="java.util.Map" id="stuMap">
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
//resultMap:表示sql语句的查询结果类型对应哪一个映射关系
<select id="queryMulField2" resultMap="stuMap">
select id,age from student;
</select>
// 查询所有学生
@Select("select * from student")
@Results({
@Result(column = "name", property = "name"),
@Result(column = "age", property = "age")
})
public List<Student> queryAllStudent() throws Exception;