使用Mybatis进行增删查改
如果在返回类型不喜欢实体类,同样可以将resultType
改为Map:
1
2
3
|
<select id="selectStudent" resultType="Map">
select * from student
</select>
|
1
2
3
|
public interface TestMapper {
List<Map> selectStudent();
}
|
在默认的实体类映射中,类中的字段和数据库中的字段必须保持一致。如果不想保持一致可以使用以下方法进行对应:
1
2
3
4
5
|
<resultMap id="Test" type="Student">
<result column="sid" property="sid"/>
<result column="sex" property="name"/>
<result column="name" property="sex"/>
</resultMap>
|
当类中有多个构造函数的时候,需要指定使用何种constructor
来进行对象的构造:
1
2
3
4
5
6
|
<resultMap id="test" type="Student">
<constructor>
<arg column="sid" javaType="Integer"/>
<arg column="name" javaType="String"/>
</constructor>
</resultMap>
|
条件查询
只需要在原来的select
块中新增parameterType
即可(好像不加也可以)。然后在需要查询的条件上加入#{}
即可。
我们通过使用#{xxx}
或是${xxx}
来填入我们给定的属性,实际上Mybatis本质也是通过PreparedStatement
首先进行一次预编译,有效地防止SQL注入问题,但是如果使用${xxx}
就不再是通过预编译,而是直接传值,因此我们一般都使用#{xxx}
来进行操作。
1
2
3
|
<select id="selectStudentById" resultType="com.entity.Student">
select * from student where id = #{id}
</select>
|
同时在接口中新增方法,不再需要放在List
里面,直接返回实体类即可。这里注意要在方法中加入参数。
1
|
Student selectStudentById(int id);
|
此时在原来getMapper
的基础上使用testMapper.selectStudentById(id)
方法即可进行条件查询
1
2
3
4
|
TestMapper testMapper = session.getMapper(TestMapper.class);
testMapper.selectStudent().forEach(System.out::println);
System.out.println("Searching id with 235297...");
System.out.println(testMapper.selectStudentById(235297));
|
插入数据
同样的道理,使用<insert>
标签进行数据的插入:
1
2
3
|
<insert id="addStudent">
insert student(id, name, sex) value(#{id}, #{name}, #{sex})
</insert>
|
由于不需要返回对象,这里不用写任何的返回值类型。但是注意这里同样会返回一个int
值。
然后在接口中定义一个和id
相同的方法,调用即可。
1
|
int addStudent(@Param("id") int id, @Param("name") String name, @Param("sex") String sex);
|
1
2
3
|
System.out.println("Insert new student...");
Student tmp = new Student(235299,"小刚", "男");
System.out.println(testMapper.addStudent(tmp.getId(), tmp.getName(), tmp.getSex()));
|
当映射的方法参数超过一个的时候,强烈建议使用@Param注解将方法中的参数和Mapper中的SQL语句的参数对应起来,不然可能出现莫名奇妙的错误。
删除数据
同理,先定义<delete>
标签,然后在接口中编写对应的函数即可:
1
2
3
|
<delete id="deleteStudent">
delete from student where id = #{id}
</delete>
|
1
|
int deleteStudent(int id);
|
接着使用即可:
1
2
|
System.out.println("Delete the new student...");
System.out.println(testMapper.deleteStudent(235299));
|
将Mapper绑定到接口后,Mybaits的优势就展现出来了,可以非常方便的调用自定义的接口方法来实现对数据库统一的操作。