MyBatis 注解方式(无映射文件)
MyBatis 还提供另外一种不需要映射文件的方式,SQL 语句使用注解的方式。定义一个接口,不需要具体去实现接口中的方法。
下面我们定义一个 IAccountMapper 的接口:
package cn.foxdevelop.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.foxdevelop.models.Account;
public interface IAccountMapper {
@Insert("insert into account(name, age,sex,addtime) values(#{name}, #{age}, #{sex},#{AddTime})")
public int add(Account account);
@Delete("delete from account where id=#{id}")
public int delete(int id);
@Update("update account set name=#{name},age=#{age} where id=#{ID}")
public int update(Account account);
@Select("select * from account where id=#{ID}")
public Account getById(int id);
@Select("select * from account")
public List<Account> getAll();
}Code language: PHP (php)
需要说明的是,我们不需要针对 IAccountMapper 接口去编写具体的实现类代码,这个具体的实现类由 MyBatis 帮我们动态构建出来,我们只需要直接拿来使用即可。
在 conf.xml 中注册这个映射接口
<mappers>
<!-- 注册AccountMapper.xml文件 -->
<mapper resource="cn/foxdevelop/mapper/AccountMapper.xml"/>
<!-- 注册IAccountMapper映射接口 -->
<mapper class="cn.foxdevelop.mapper.IAccountMapper"/>
</mappers>Code language: HTML, XML (xml)
使用方式
使用的时候,我们可以通过 session 来创建一个接口的对象:
IAccountMapper mapper = session.getMapper(IAccountMapper.class);Code language: PHP (php)
执行代码
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.foxdevelop.mapper.IAccountMapper;
import cn.foxdevelop.models.Account;
public class Program {
public static void main(String[] args) throws IOException {
SqlSession session = extracted();
IAccountMapper mapper = session.getMapper(IAccountMapper.class);
// 插入用户
Account a = new Account();
a.setAddTime(new Date());
a.setAge(123);
a.setSex(true);
a.setName("王五2222");
int result = mapper.add(a);
session.commit();
System.out.println(result > 0 ? "新增成功" : "新增失败");
// 下面是根据ID查询某个用户
Account account = mapper.getById(1);
// 修改用户
account.setName("梁三峰222");
account.setAge(100);
int res = mapper.update(account);
session.commit();
System.out.println(res > 0 ? "修改成功" : "修改失败");
// 获取所有用户
List<Account> listAccount = mapper.getAll();
for (Account item : listAccount) {
System.out.println(item.getName() + ":" + item.getID());
}
// 删除操作
int retResult = mapper.delete(7);
session.commit();
// 最后关闭session
session.close();
}
private static SqlSession extracted() {
String resource = "conf.xml";
InputStream inputStream = Program.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
return session;
}
}Code language: JavaScript (javascript)
两种方式对比
简单的操作可以使用接口注解的方式,但是接口的方式需要改动到代码,每次修改需要重新编译,而且写的 SQL 也不能太复杂。映射文件的方式可以直接修改,不需要改动到代码。
Previous: 使用Mybatis对表进行增删改查操作