上面2章写了mybatis的基本操作,今天就写写mybatis的动态代理吧。

动态代理有4个基本原则:

1.userMapper.xml里面的namespace="cn.my.dao.UserDaoMapper"一定要和接口的包名+接口名一致

2.userMapper.xml里面的statementid值要和接口中的方法名一致

3.userMapper.xml里面的入参类型要和接口中方法的参数类型一致

4.userMapper.xml里面的返回类型要和接口中方法的返回类型一致

好了不废话直接上代码.

首先改造一下上一篇的代码,为了不修改user.xml,添加一个新的UserMapper.xml文件,然后修改部分代码

<?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.my.dao.UserDaoMapper">
<select id="findById" parameterType="int" resultType="cn.my.mybatis.entity.User">
select * from user where id=#{id}
</select> <!-- 新增映射 修改id名称为了以后封装basedao-->
<insert id="insert" parameterType="cn.my.mybatis.entity.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> <!-- 修改用户 修改id名称为了以后封装basedao-->
<update id="update" parameterType="cn.my.mybatis.entity.User">
UPDATE `user`
SET
`username` = #{username},
`birthday` = #{birthday},
`sex` = #{sex},
`address` = #{address}
WHERE `id` = #{id} ;
</update>
<!-- 删除 修改id名称为了以后封装basedao -->
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>

第二步新增一个UserDaoMapper.java接口

package cn.my.dao;

import cn.my.mybatis.entity.User;

public interface UserDaoMapper {

    public User findById(int id);
public void insert(User user);
public void update(User user);
public void delete(int id);
}

第三步新增一个测试类TestUserMapper.java文件

package cn.my.test;

import java.io.IOException;
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 cn.my.dao.UserDaoMapper;
import cn.my.mybatis.entity.User; public class TestUserMapper {
private SqlSessionFactory factory;
@Before
public void setUp() throws IOException{
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void testFindById(){
SqlSession session = factory.openSession();
UserDaoMapper dao = session.getMapper(UserDaoMapper.class);//获取mybatis的动态代码实现接口对象
User user = dao.findById(10);
session.close();
System.out.println(user);
}
/**
* 新增用户
*/
@Test
public void testInserUser(){
SqlSession session = factory.openSession();
User user = new User();
user.setAddress("许昌");
user.setUsername("曹操1");
user.setSex("男");
user.setBirthday(new Date());
UserDaoMapper dao = session.getMapper(UserDaoMapper.class);//获取mybatis的动态代码实现接口对象
dao.insert(user);
session.commit();
session.close();
System.out.println(user.getId());
}
/**
* 修改
*/
@Test
public void testUpdateUser(){
SqlSession session = factory.openSession();
User user = new User();
user.setId(34);
user.setAddress("许昌1");
user.setUsername("曹操2");
user.setSex("女");
user.setBirthday(new Date());
UserDaoMapper dao = session.getMapper(UserDaoMapper.class);//获取mybatis的动态代码实现接口对象
dao.update(user);
session.commit();
session.close();
}
/**
* 删除
*/
@Test
public void testDeleteUser(){
SqlSession session = factory.openSession();
UserDaoMapper dao = session.getMapper(UserDaoMapper.class);//获取mybatis的动态代码实现接口对象
dao.delete(34);
session.commit();
session.close();
} }

运行结果:

查询结果:User [id=10, username=张三, sex=1, birthday=Thu Jul 10 00:00:00 CST 2014, address=北京市]

新增结果:     打印结果:34

修改结果:

删除结果:

以上修改为动态代理就已经修改好了。

下一章写sql动态标签

可以加微信群,一起交流,加时注明在哪里看到的