思路
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由MyBatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。 Mapper接口开发需要遵循以下规范:
Mapper.xml文件中的namespace与mapper接口的类路径相同:
<!--namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有着特殊重要的作用 -->
<mapper namespace="com.will.dao.UserMapper">
- Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
- Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
- Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
<select id="findUserById" parameterType="int" resultType="com.crow.po.User">
SELECT * FROM USER WHERE id = #{id}
</select>
//根据id查询用户信息
public User findUserById(int id) throws Exception;
总结: 以上开发规范主要是对下边的代码进行统一生成:
User user = sqlSession.selectOne("test.findUserById", id);
sqlSession.insert("test.insertUser", user);
实现代码
UserMapper.java(接口文件)
Public interface UserMapper {
//根据用户id查询用户信息
public User findUserById(int id) throws Exception;
//查询用户列表
public List<User> findUserByUsername(String username) throws Exception;
//添加用户信息
public void insertUser(User user)throws Exception;
}
接口定义有如下特点:
- Mapper接口方法名和Mapper.xml中定义的statement的id相同
- Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同
- Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同
UserMapper.xml(映射文件)
定义mapper映射文件UserMapper.xml(内容同Users.xml),需要修改namespace的值为 UserMapper接口路径。将UserMapper.xml放在classpath 下mapper目录 下。
<?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">
<!--此处namespace需要改为UserMapper接口的路径-->
<mapper namespace="com.will.dao.UserMapper">
<!-- 根据id获取用户信息 -->
<select id="findUserById" parameterType="int" resultType="com.will.entiy.User">
select * from user where id = #{id}
</select>
<!-- 自定义条件查询用户列表 -->
<select id="findUserByUsername" parameterType="java.lang.String"
resultType="com.will.dao.UserDao">
select * from user where username like '%${value}%'
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.will.entiy.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
</mapper>
在SqlMapConfig.xml中加载UserMapper.xml
<mappers>
<!-- 通过resource引用mapper的映射文件 -->
<mapper resource="mapper/User.xml"/>
<!-- <mapper resource="mapper/UserMapper.xml" /> -->
<!-- 通过class引用mapper接口 class:配置mapper接口全限定名
要求:需要mapper.xml和mapper.java同名并且在一个目录 中 -->
<!--<mapper class="com.will.dao.UserMapper"/>-->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
这里插一点Mybatis配置的小知识,参考链接
Mybatis 加载 Mapper配置共有四种方式
第一种
<configuration>
<mappers>
<!-- class 级别的指定 -->
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelMapper"/>
<mapper class="com.bestcxx.stu.springmvc.mapper.UserModelTwoMapper"/>
</mappers>
</configuration>
这种情况下,如果是非注解模式的话xml配置文件必须和这个类在同一级目录,且与Mapper类同名。
结构如下图:
第二种
<configuration>
<mappers>
<package name="com.bestcxx.stu.springmvc.mapper"/>
</mappers>
</configuration>
这种情况下,如果是非注解模式的话xml配置文件必须也处于同一级 package 下,且与Mapper类同名。
结构如下图:
第三种
<configuration>
<mappers>
<!-- 使用这个方案,可以单独指定Mapper的位置 -->
<mapper resource="mybatis/mappings/UserModelMapper.xml"/>
<mapper resource="mybatis/mappings/UserModelTwoMapper.xml"/>
</mappers>
</configuration>
第三种是把 Mapper 的xml配置文件单独放置到 resources 中,和Mapper 类分开了。
就是本项目采用的方式。
类似于这样:
mapper XML的配置
mapper接口类:
第四种
直接在spring中配置Mapper的路径,但是mybatis.xml还是必不可少的
在sqlSessionFactory中设定 mybitas的xml和aliases
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="实体类包路径" />
<property name="typeAliasesSuperType" value="实体类顶级包路径" />
<property name="mapperLocations" value="classpath:/mybatis/mappings/**/*.xml" />
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
</bean>
设置Mapper类
<!-- MustConfigPoint 扫描basePackage下所有以@MyBatisDao注解的接口 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper类的包路径" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="annotationClass" value="com.msyd.framework.common.persistence.annotation.MyBatisDao" />
</bean>
好了,言归正传
测试代码
以通过id查询用户为例
@Before
public void setUp() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void findUserByIdTest() throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//生成代理对象userMapper
User user = userMapper.findUserById(2);
System.out.println(user);
}
运行结果如下: