MyBatis动态代理生成DAO的步骤:
1) 编写数据管理的接口XxxMapper
2) 编写该接口对应的Mapper.xml
a) namespace必须与Mapper接口全名一致
b) statement的id必须和Mapper接口中的对应方法名一致
c) statement的resultType必须和Mapper接口中对应方法返回值一致
3) 通过SqlSession的getMapper(XxxMapper.class)方法来获取动态代理的Mapper实现类对象
开发中运用动态代理的比较多,下面简单的介绍一个简单的案例关于增删改查的测试:
1.编写相应的接口
public interface UserMapper { /**根据id查询用户 * @param id * @return */ public User queryUserById(Long id); /**查询全部的用户 * @return */ public List<User> queryAll(); /**增加用户 * @param user */ public void insertUser(User user); /**更新用户 * @param user */ public void updateUser(User user); /** * 删除用户 * @param id */ public void deleteById(Long id); }
2.编写主配置文件
<?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 resource="jdbc.properties"></properties> <settings> <!--开启驼峰命名法--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 起别名 --> <typeAliases> <typeAlias alias="User" type="cn.mybatis.pojo.User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <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> <!-- 指定mapper的配置文件 --> <mappers> <mapper resource="mapper.xml"/> <mapper resource="usermapper.xml"/> </mappers> </configuration>
3.编写映射配置文件
<?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.mapper.UserMapper"> <select id="queryUserById" parameterType="java.lang.Long" resultType="User"> select * from tb_user where id = #{id} </select> <select id="queryAll" resultType="User"> select * from tb_user </select> <insert id="insertUser" parameterType="User"> INSERT INTO tb_user ( id, user_name, password, name, age, sex, birthday, created, updated )VALUES( null, #{userName}, #{password}, #{name}, #{age}, #{sex}, #{birthday}, NOW(), NOW() ); </insert> <delete id="deleteById" parameterType="Long"> DELETE FROM tb_user WHERE id = #{id} </delete> </mapper>
4.编写测试类
package cn.mybatis.junit.test; import static org.junit.Assert.*; import java.io.InputStream; import java.util.List; 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; public class UserMapperTest { private UserMapper userMapper; InputStream inputStream; SqlSessionFactory sqlSessionFactory; SqlSession sqlSession; @Before public void setUp() throws Exception { String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); // 通过动态代理获取 userMapper = sqlSession.getMapper(UserMapper.class); } /** * 根据id查询 */ @Test public void test() { User user = userMapper.queryUserById(1L); System.out.println(user); } /** * 查询全部 */ @Test public void test1() { List<User> all = userMapper.queryAll(); for (User user2 : all) { System.out.println(user2); } } /** * 插入用户 */ @Test public void test2() { User user = new User(); user.setAge(22); user.setBirthday(null); user.setName("jack"); user.setPassword("123"); user.setSex(1); user.setuserName("pick jack"); userMapper.insertUser(user); this.sqlSession.commit(); } /** * 删除 */ @Test public void test3() { this.userMapper.deleteById(12L); this.sqlSession.commit(); } }