Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象。
Mapper接口开发需要遵循以下规范:
1、Mapper.xml文件中的namespace与mapper接口的类路径相同。
此步骤目的:通过mapper.xml和mapper.java进行关联。
2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同。
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同。
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同。
在前一篇的基础上,新增UserMapper接口,方法在UserMapperTest中测试。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.edu.jxnuss.dpc.mapper.UserMapper">
<!--通过Id查询User-->
<select id="findUserById" parameterType="java.lang.Integer" resultType="cn.edu.jxnuss.dpc.pojo.User">
select * from tb_user where id=#{id}
</select>
<!--通过姓名模糊查询-->
<select id="findUserByname" parameterType="java.lang.String" resultType="cn.edu.jxnuss.dpc.pojo.User">
select * from tb_user
where username like '%${value}%'
</select>
<!--添加User,并返回自增主键id值-->
<!-- #{}:如果传入的是pojo类型,那么#{}中的变量名称必须是pojo中对应的属性.属性.属性..... 如果要返回数据库自增主键:可以使用select LAST_INSERT_ID() -->
<!-- 执行 select LAST_INSERT_ID()数据库函数,返回自增的主键 keyProperty:将返回的主键放入传入参数的Id中保存.
order:当前函数相对于insert语句的执行顺序,在insert前执行是before,在insert后执行是AFTER resultType:id的类型,也就是keyproperties中属性的类型 -->
<insert id="insertUser" parameterType="cn.edu.jxnuss.dpc.pojo.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select
LAST_INSERT_ID()
</selectKey>
insert into tb_user (username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
<!--删除User-->
<delete id="deleteUserById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<!--修改User-->
<update id="updateUserById" parameterType="cn.edu.jxnuss.dpc.pojo.User">
update tb_user set username=#{username},sex=#{sex} where id=#{id}
</update>
</mapper>
package cn.edu.jxnuss.dpc.mapper;
import cn.edu.jxnuss.dpc.pojo.User;
import java.util.List;
public interface UserMapper {
public User findUserById(int id);
public List<User> findUserByname(String username);
public int insertUser(User user);
public void deleteUserById(int id);
public void updateUserById(User user);
}
遵循以上四个规范,在创建Mapper对象的时候,Mybatis就能够通过xml文件里的namespace找到接口的实现,在调用对象的方法时,自动调用对应id的SQL语句。
package cn.edu.jxnuss.dpc.mapper;
import cn.edu.jxnuss.dpc.pojo.User;
import org.apache.ibatis.annotations.Mapper;
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;
import static org.junit.Assert.*;
public class UserMapperTest {
SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
//配置文件
String resource = "mybatis-config.xml";
//加载配置文件到输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void findUserById() {
//开启会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用UserMapper.xml实例化对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//mapper就相当于一个实现了UserMapper接口的对象,使用方法和普通对象一样
User user = mapper.findUserById(16);
System.out.println(user);
sqlSession.close();
}
@Test
public void findUserByname() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.findUserByname("王");
System.out.println(users);
sqlSession.close();
}
@Test
public void insertUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("小张");
user.setSex("男");
user.setAddress("江西南昌");
System.out.println("Before insert: userId " + user.getId());
mapper.insertUser(user);
System.out.println("After insert: userId " + user.getId());
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUserById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUserById(35);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUserById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(22);
user.setUsername("王明");
mapper.updateUserById(user);
sqlSession.commit();
sqlSession.close();
}
}