这期内容当中小编将会给大家带来有关如何在MyBatis中开发Dao层,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上一篇博客中Dao接口实现类方法。
Mapper接口开发需要遵循以下规范:
(1)Mapper.xml文件中的namespace与mapper接口的类路径相同。
(2)Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
(3)Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
(4)Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
1、定义mapper映射文件UserMapper.xml(内容同User.xml),需要修改namespace的值为 UserMapper接口路径。将UserMapper.xml放在classpath 下sqlmapperr目录下。
<?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="com.xyfer.mapper.UserMapper"> <!-- 根据id查询用户 --> <select id="getUserById" parameterType="int" resultType="com.xyfer.po.User"> select * from user where id = #{id} </select> <!-- 添加用户 --> <insert id="insertUser" parameterType="com.xyfer.po.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> <!-- 修改用户 --> <update id="updateUser" parameterType="com.xyfer.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update> <!-- 删除用户 --> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
2、UserMapper.java接口文件
package com.xyfer.mapper; import com.xyfer.po.User; public interface UserMapper { public User getUserById(int id); //根据id值查询一个用户 public void insertUser(User user); //新增一个用户 public void updateUser(User user); //修改一个用户 public void deleteUser(int id); //删除一个用户 }
接口定义有如下特点:
(1)UserMapper接口方法名和Mapper.xml中定义的statement的id相同
(2)UserMapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同
(3) UserMapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同
3、在SqlMapConfig.xml文件中加载UserMapper.xml文件
<mappers> <mapper resource="UserMapper.xml"/> </mappers>
4、测试
package com.xyfer.mapper; import static org.junit.Assert.*; import java.io.InputStream; import java.util.Date; 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 com.xyfer.po.User; import junit.framework.TestCase; public class UserMapperTest extends TestCase{ private SqlSessionFactory sqlSessionFactory; protected void setUp() throws Exception { SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder(); InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = sessionFactoryBuilder.build(inputStream); } @Test public void testGetUserById() { //获取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //获取UserMapper接口代理对象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //调用代理对象方法 User user = userMapper.getUserById(10); //打印结果 System.out.println(user); //关闭sqlsession sqlsession.close(); } @Test public void testInsertUser() { //获取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //获取UserMapper接口代理对象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //新建一个对象 User user = new User(); user.setUsername("小谢"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("浙江省杭州市"); //调用代理对象方法 userMapper.insertUser(user); //关闭sqlsession sqlsession.close(); } @Test public void testUpdateUser() { //获取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //获取UserMapper接口代理对象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //新建一个对象 User user = new User(); user.setUsername("小谢"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("上海市"); //调用代理对象方法 userMapper.updateUser(user); //关闭sqlsession sqlsession.close(); } @Test public void testDeleteUser() { //获取sqlsession SqlSession sqlsession = sqlSessionFactory.openSession(); //获取UserMapper接口代理对象 UserMapper userMapper = sqlsession.getMapper(UserMapper.class); //调用代理对象方法 userMapper.deleteUser(6); //关闭sqlsession sqlsession.close(); } }
上述就是小编为大家分享的如何在MyBatis中开发Dao层了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。