标签:add tor lis details clipboard bsp strong 修改 config
一、实现原理
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的类型相同
二、Mapper.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">
- <mapper namespace="cn.itcast.mybatis.mapper.UserMapper">
- <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
- select * from user where id = #{id}
- </select>
- <select id="findUserByUsername" parameterType="java.lang.String"
- resultType="cn.itcast.mybatis.po.User">
- select * from user where username like ‘%${value}%‘
- </select>
- <insert id="insertUser" parameterType="cn.itcast.mybatis.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>
-
- </mapper>
三、Mapper.java(接口文件)
- Public interface UserMapper {
-
- public User findUserById(int id) throws Exception;
-
- public List<User> findUserByUsername(String username) throws Exception;
-
- public void insertUser(User user)throws Exception;
- }
接口定义有如下特点:
1、Mapper接口方法名和Mapper.xml中定义的statement的id相同
2、Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同
3、Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同
四、加载UserMapper.xml文件
修改sqlMapConfig.xml文件:
- <mappers>
- <mapper resource="mapper/UserMapper.xml"/>
- </mappers>
五、测试
- Public class UserMapperTest extends TestCase {
-
- private SqlSessionFactory sqlSessionFactory;
-
- protected void setUp() throws Exception {
-
- String resource = "sqlMapConfig.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
-
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- }
-
-
- Public void testFindUserById() throws Exception {
-
- SqlSession session = sqlSessionFactory.openSession();
-
- UserMapper userMapper = session.getMapper(UserMapper.class);
-
- User user = userMapper.findUserById(1);
- System.out.println(user);
-
- session.close();
-
- }
- @Test
- public void testFindUserByUsername() throws Exception {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
- List<User> list = userMapper.findUserByUsername("张");
- System.out.println(list.size());
-
- }
- Public void testInsertUser() throws Exception {
-
- SqlSession session = sqlSessionFactory.openSession();
-
- UserMapper userMapper = session.getMapper(UserMapper.class);
-
- User user = new User();
- user.setUsername("张三");
- user.setBirthday(new Date());
- user.setSex("1");
- user.setAddress("北京市");
-
- userMapper.insertUser(user);
-
- session.commit();
-
- session.close();
- }
-
-
- }
六、总结
selectOne和selectList
动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。
namespace
mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。
Mybatis实现Mapper动态代理方式
标签:add tor lis details clipboard bsp strong 修改 config
原文地址:http://www.cnblogs.com/efforts-will-be-lucky/p/7388791.html