Mybatis学习总结(二)—使用接口实现数据的增删改查

在这一篇中,让我们使用接口来实现一个用户数据的增删改查。

完成后的项目结构如下图所示:

Mybatis学习总结(二)—使用接口实现数据的增删改查

在这里,person代表了一个用户的实体类。在该类中,描述了相关的信息,包括id、name、age、id_num信息。而personMapper则是该实体类的一个配置文件。需要注意的是,在上一篇博文中,namespace属性的值是其本身,而在这一篇中,使用的是接口。那么两者有什么区别呢?使用接口,那么相关的操作方法不用自己去实现,只需要调用该接口的相关的方法就能够实现相应的功能。

那么,在这里就有一个问题,接口如何实例化呢?直接new吗?这当然不行,在这里要想得到接口的实例,需要使用session的getmapper方法。然后就能够得到接口的实例,通过调用接口的相关方法,就能够实现相应的功能。在这里有一点需要注意的是,调用完方法之后,要记得调用session.commit()方法,将相应的结果保存于数据库中,另外,不要忘记调用close()方法来关闭session。

到这里,可能又有人要问了,那么这个接口与要操作的数据之间怎么保持联系呢?换句话说,我调用了这个方法,Mybatis怎么就知道这个方法是用来干什么的。其实在这里,有一点需要注意的就是,这个接口中的方法名必须与配置文件中的相对应属性的id保持一致。

好了到这里,让我们看看person实体类与该类的配置文件吧。

person.java

public class person {
private int id;
private int age;
private String name;
private String id_num; public person() { } public person(int id) {
this.id = id;
} public person(String name) {
this.name = name;
} public person(int id, int age, String name, String id_num) {
super();
this.id = id;
this.age = age;
this.name = name;
this.id_num = id_num;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getId_num() {
return id_num;
} public void setId_num(String id_num) {
this.id_num = id_num;
} @Override
public String toString() {
return "person [id=" + id + ", age=" + age + ", name=" + name
+ ", id_num=" + id_num + "]" + "\n";
}

  personMapper.xml

<mapper namespace="cn.jiang.dao.personDao">
<!-- save -->
<insert id="save">
insert into tb_person values(#{id} , #{name} , #{age} , #{id_num});
</insert> <!-- delete -->
<delete id="delete">
delete from tb_person
<where>
<choose>
<when test="id != 0">
id = #{id}
</when>
<when test="name != null">
name = #{name}
</when>
<when test="id_num != null">
id_num = #{id_num}
</when>
</choose>
</where>
</delete> <!-- update -->
<update id = "update">
update tb_person set name=#{name} , age = #{age} , id_num=#{id_num}
where id = #{id};
</update> <!-- search -->
<select id="search" resultType="cn.jiang.domain.person">
select * from tb_person
<where>
<choose>
<when test="id != 0">
id = #{id}
</when>
<when test="name != null">
name = #{name}
</when>
<when test="id_num != null">
id_num = #{id_num}
</when>
</choose>
</where>
</select>
</mapper>

  在这里,mapper中的namespace属性使用的就是为接口,而不再是其xml文件。好,现在让我们来看看该接口吧。

personDao.java

public interface personDao {
//save
public void save(person per);
//delete
public boolean delete(person per);
//update
public boolean update(person per);
//search
public List<person> search(person per);
}

  好了,虽说直接调用该接口的方法就能够实现相关的数据操作,但是,对其进行一些封装,还是能够方便以后的使用,同时也能够是代码更加简洁美观。现在让我们来看看对该接口实现的一个封装类。

public class PerDao {
//save
public void save(person per)
{
SqlSession session = MyBatisUnit.getSession();
personDao dao = session.getMapper(personDao.class); dao.save(per);
session.commit();
session.close();
}
//delete
public boolean delete(person per)
{
boolean result = false;
SqlSession session = MyBatisUnit.getSession();
personDao dao = session.getMapper(personDao.class); result = dao.delete(per);
session.commit();
session.close(); return result;
}
//update
public boolean update(person per)
{
boolean result = false;
SqlSession session = MyBatisUnit.getSession();
personDao dao = session.getMapper(personDao.class); result = dao.update(per);
session.commit();
session.close(); return result;
}
//search
public List<person> search(person per)
{
List<person> result = null;
SqlSession session = MyBatisUnit.getSession();
personDao dao = session.getMapper(personDao.class); result = dao.search(per);
session.commit();
session.close();
return result;
}
}

  好了,到这里,基本上就完成了。读者如果想了解MyBatisUnit类的具体功能以及源代码,请看上一篇博文《Mybatis的学习总结(一)——使用配置文件实现增删改查》。另外,要提醒的是,在使用MybatisUnit来调用getSession的时候,如果没有写config.xml配置文件,会抛出NullPointException。原因,请参照MyBatisUnit的源代码。

上一篇:jQuery之简单动画效果


下一篇:Vue3.0 - watch函数