Mybatis之Mapper动态代理方式

mybatis属于持久层框架,用它来写dao层开发者只需要编写mapper接口,由Mybatis框架根据接口定义创建接口的动态代理对象。

实体类User

public class User implements Serializable {

    private Integer id;
    private String username;
    private String password;
    private Date birthday;
    private String address;


    public User() {
    }
    public User(String username, String password, Date birthday, String address) {
        this.username = username;
        this.password = password;
        this.birthday = birthday;
        this.address = address;
    }
    get、set、toString方法
}

sql映射文件User.xml中使用上一篇博文mybatis快速入门
中已经写好的,如下:

<?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:命名空间,用于隔离sql,名称要写对应的mapper接口全路径名 -->
<!-- 写sql语句 -->
<mapper namespace="com.scu.mybatis.mapper.UserMapper">

    <!-- 插入数据 -->
    <insert id="save" parameterType="com.scu.mybatis.entity.User">
    <selectKey keyProperty="id" order="AFTER" resultType="int">
        SELECT LAST_INSERT_ID()
    </selectKey>
        INSERT INTO t_user (username,password,birthday,address) VALUES(#{username},#{password},#{birthday},#{address})
    </insert>
    <!-- 根据id查询数据 -->
    <select id="findById" parameterType="int" resultType="com.scu.mybatis.entity.User">
        SELECT * FROM t_user u WHERE u.id=#{id} 
    </select>
    <!-- 查询所有用户信息 -->
    <select id="findList" resultType="com.scu.mybatis.entity.User">
        SELECT * FROM t_user
    </select>
    <!-- 根据用户名模糊查询,一定要写成value不能写成username -->
    <select id="findByName" resultType="com.scu.mybatis.entity.User" parameterType="string">
        SELECT * FROM t_user u WHERE u.username LIKE '%${value}%' 
    </select>
    <!-- 修改用户信息 -->
    <update id="updateById" parameterType="com.scu.mybatis.entity.User">
        UPDATE t_user u SET u.username=#{username},u.password=#{password},
            u.birthday=#{birthday},u.address=#{address}
        WHERE u.id=#{id} 
    </update>
    <!-- 删除 -->
    <deleteid="delete" parameterType="int">
        DELETE FROM t_user  WHERE id=#{id} 
    </delete>
</mapper>

mapper接口
创建对应于User.xml的mapper接口UserMapper
Mapper接口开发需要遵循以下规范:

  1. 方法名要与User.xml中id一致;
  2. 返回参数类型要与User.xml中对应语句的返回参数类型(resultType)一致;
  3. 形参类型要与 User.xml中对应语句的入参(parameterType)类型一致;
  4. 另外,User.xml文件中的namespace要写UserMapper接口的完整路径名。

UserMapper接口如下

public interface UserMapper {

    //插入数据
    public void save(User user);
    //根据id查询数据 
    public User findById(int id);
    //查询所有用户信息
    public List<User> findList();
    //根据用户名模糊查询
    public List<User> findByName(String username);
    //修改用户信息
    public void updateById(User user);
    //删除用户
    public void delete(int id);
}

分别测试上面6个方法:
1、创建SqlSessionFactoryBuilder;
2、获取SqlSessionFactory;
3、获取SqlSession;
4、获取UserMapper;
5、执行方法;
6、关闭SqlSession和输入流;

/**
 * 测试Mapper中的crud
 * @author 12706
 *
 */
public class UserMapperTest {
    InputStream inputStream;
    SqlSession session;
    @Before
    public void init() throws Exception{
        String resource = "sqlMapConfig.xml";
        inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = ssfb.build(inputStream);
        session = sqlSessionFactory.openSession();
    }
    @After
    public void destroy() throws Exception{
        inputStream.close();
        session.close();
    }
    @Test
    /*
     * 添加用户信息
     */
    public void testSave(){
        User user = new User("小雨", "123456", new Date(), "安徽池州市");
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        //添加用户信息
        mapper.save(user);
        System.out.println(user.getId());
        session.commit();
    }
}

添加用户后查看数据库t_user表,用户信息成功添加,并且控制台输出了3(新添加的用户id):
这里写图片描述

剩下的方法测试结果就不展示了

@Test
    /*
     * 获取单个对象
     */
    public void testFindById(){
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        //根据id查询用户信息
        User user = mapper.findById(2);
        System.out.println(user);
    }
    @Test
    /*
     * 获取对象集合
     */
    public void testFindList(){
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        //查询用户列表
        List<User> list = mapper.findList();
        for (User user : list) {
            System.out.println(user);
        }
    }
    @Test
    /*
     * 模糊查询
     */
    public void testFindByName(){
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        //用户名模糊查询用户列表
        List<User> list = mapper.findByName("张");
        for (User user : list) {
            System.out.println(user);
        }
    }
    @Test
    /*
     * 修改用户信息
     */
    public void testUpdate(){
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.findById(2);
        user.setUsername("小红");
        user.setAddress("四川省成都市武侯区");
        //更新id为2的用户信息
        mapper.updateById(user);
        session.commit();
    }
    @Test
    /*
     * 删除用户信息
     */
    public void testDelete(){
        //获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.delete(2);
        session.commit();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值