mybatis学习教程(三)mybatis映射开发规范

1、前言


前面一讲讲到讲到初级的Mybatis开发,现在晋级将一些他的规范开发,规范的东西更便于维护和阅读。

2、为Mybatis开发规范

1、mapper.xml中namespace指定为mapper接口的全限定名
 
此步骤目的:通过mapper.xml和mapper.java进行关联。
 
2、mapper.xml中statement的id就是mapper.java中方法名
 
3、mapper.xml中statement的parameterType和mapper.java中方法输入参数类型一致
 
4、mapper.xml中statement的resultType和mapper.java中方法返回值类型一致.

现在只需要一个mapper.java(UserMapper.java)接口文件与一个mapper.xml(UserMapper.xml),注意一点无论是返回li大st集合还是单个对象,返回参数都只需要单个对象的类型,这个是不是萌萌哒!以为他判断了你的java文件里面是集合类型
java对象:UserMapper依旧
import com.ycy.mybatis.module.User;

import java.util.List;

/**
 * Created by Administrator on 2015/8/31 0031.
 */
public interface UserMapper {
    public User getUserById(int id) throws Exception;

    public List<User> findUserByName(String username) throws Exception;

    public void  insertUser(User user) throws  Exception;
}
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">
<!--命名空间:分类管理sql隔离,方便管理-->
<mapper namespace="com.ycy.mybatis.dao.UserMapper">
    <!--id标示一个sql语句,一个Statement,封装为一个MapperStatement-->
    <!--parameterType:传入参数类型;resultType:输出结果类型,指定映射的pojo-->
    <!--#{}标示一个占位符,-->
    <!--查询用户-->
    <!--oracle-->
    <!--   SELECT * FROM  USER  WHERE  username  LIKE  '%'||#{_parameter}||'%'-->
    <!-- mysql-->
    <select id="getUserById" parameterType="int" resultType="com.ycy.mybatis.module.User">
         SELECT  * FROM USER WHERE id=#{id}
        </select>
    <select id="findUserByName" parameterType="String" resultType="com.ycy.mybatis.module.User">
      SELECT * FROM  USER  WHERE  username  LIKE  '%${_parameter}%'
    </select>
    <!--新增用户-->
    <insert id="insertUser" parameterType="com.ycy.mybatis.module.User" >
        <!--插入值之后返回主键值-->
        <selectKey resultType="int" order="AFTER" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT  INTO USER  (username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})
    </insert>
    <!--删除用户-->
    <delete id="deleteUser" parameterType="int">
    DELETE  FROM USER  WHERE  id=#{id}
    </delete>
    <!--更新用户-->
    <update id="updateUser" parameterType="com.ycy.mybatis.module.User">
        UPDATE USER  SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id}
    </update>
</mapper>
测试结果:MybatisTest3
package com.ycy.mybatis.test;

import com.ycy.mybatis.dao.UserMapper;
import com.ycy.mybatis.module.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2015/8/31 0031.
 */
public class MybatisTest3 {
    private SqlSessionFactory sqlSessionFactory = null;
    @Before
    public void  before() throws IOException {
        String resource="SqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        sqlSessionFactory= new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public  void testGet() throws Exception {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userService=  sqlSession.getMapper(UserMapper.class);
        User user=  userService.getUserById(1);
        System.out.println(user.getUsername());
    }
    @Test
    public  void testFindByname() throws Exception {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userMapper=  sqlSession.getMapper(UserMapper.class);
        List<User> userList=  userMapper.findUserByName("陈小明");
        for (User user : userList) {
            System.out.println(user.getUsername());
        }
    }
}


如有疑问:qq群:78275755(目前木有人,来啦更好)
本项目1-5章 初级教程 项目百度分享盘:
http://pan.baidu.com/s/1o63SUaI



上一篇:mybatis学习教程(一)JDBC到mybatis


下一篇:SpringMVC+MyBatis整合(2)MyBatis篇