之前有详细介绍过使用配置文件完成基本的增删改查操作,今天介绍使用注解来完成这些操作
首先我们不需要之前的UserMapper.xml了,直接在相应接口上进行注解标注。比如:
package com.leoham.mapper;
import com.leoham.domain.User;
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 java.util.List;
public interface UserMapper {
@Insert("insert into user values (#{id}, #{username}, #{password}, #{birthday})")
public void save(User user);
@Update("update user set username = #{username}, password = #{password} where id = #{id}")
public void update(User user);
@Delete("delete from user where id = #{id}")
public void delete(int id);
@Select("select * from user")
public List<User> findAll();
@Select("select * from user where id = #{id}")
public User findById(int id);
}
然后需要在核心配置文件中加载映射关系并指定接口所在的包位置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--通过properties标签加载外部properties文件-->
<properties resource="jdbc.properties"></properties>
<!-- 定义别名-->
<typeAliases>
<typeAlias type="com.leoham.domain.User" alias="user"></typeAlias>
</typeAliases>
<!-- 数据源的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射关系-->
<mappers>
<!-- 指定接口所在的包-->
<package name="com.leoham.mapper"/>
</mappers>
</configuration>
测试:
package com.znan;
import com.leoham.domain.User;
import com.leoham.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MapperTest {
private UserMapper mapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testFindAll() throws IOException {
List<User> all = mapper.findAll();
System.out.println(all);
}
@Test
public void testSave() throws IOException {
User user = new User();
user.setUsername("ouou");
user.setPassword("998");
mapper.save(user);
}
@Test
public void testFindById() throws IOException {
User user = mapper.findById(1);
System.out.println(user);
}
@Test
public void testDelete() throws IOException {
mapper.delete(2);
}
}
我这里测试findById()方法,可以发现注解方法要比配置文件实现更加简单和方便。