id:是当前命名空间下的Statement的唯一标识(必须属性);
parameterType:输入的参数类型(可以省略);
resultType:将结果集映射为的java对象类型(必须属性);
标签内部:编写SQL语句
INSERT INTO tb_user (
id,
user_name,
password,
name,
age,
sex,
birthday,
created,
updated
)
VALUES
(
NULL,
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday},
NOW(),
NOW()
);
id:是当前命名空间下的Statement的唯一标识(必须属性);
parameterType:输入的参数类型(可以省略);
标签内部:编写SQL语句
INSERT INTO tb_user (
id,
user_name,
password,
name,
age,
sex,
birthday,
created,
updated
)
VALUES
(
NULL,
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday},
NOW(),
NOW()
);
@Test
public void testSaveUser() {
User user = new User();
user.setAge(20);
user.setBirthday(new Date());
user.setName("test_3");
user.setPassword("123456");
user.setSex(1);
user.setUserName("test_username_3");
this.userMapper.saveUser(user);
//获取自增id
System.out.println("id:"+user.getId());
}
UPDATE tb_user
SET
user_name = #{userName},
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday},
updated = NOW()
WHERE
id = #{id}
id:是当前命名空间下的Statement的唯一标识(必须属性);
parameterType:输入的参数类型(可以省略);
标签内部:编写SQL语句
DELETE FROM tb_user WHERE id = #{id}
id:是当前命名空间下的Statement的唯一标识(必须属性);
parameterType:输入的参数类型(可以省略);
标签内部:编写SQL语句
package cn.zto.mybatis.mapper;
import cn.zto.mybatis.pojo.User;
public interface UserMapper {
/**
* 模拟登陆
* @Title: login
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param userName
* @param: @param passwd
* @param: @return
* @return: User
* @throws
*/
public User login(String userName,String passwd);
}
package cn.zto.mybatis.mapper;
import java.io.InputStream;
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.zto.mybatis.pojo.User;
/**
* 动态代理实现类测试用例
*
*/
public class UserMapperTest {
private UserMapper userMapper;
@Before
public void setUp() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "test");
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 通过sqlSession获取到动态代理的实现类
this.userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testLogin() {
User user = this.userMapper.login("zhangsan","123");
System.out.println(user);
}
}
或者:
测试通过。这种方式为使用默认规则参数,还有一种是通过@param注解获取参数
/**
* 模拟登陆
* @Title: login
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param userName
* @param: @param passwd
* @param: @return
* @return: User
* @throws
*/
public User login(@Param("userName")String userName,@Param("passwd")String passwd);
package cn.zto.mybatis.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface CommonMapper {
/**
* 根据表名查询表数据
*
* @param id
* @return
*/
public List
CommonMapper.xml
@Test
public void queryByTableName() {
List
id,user_name,password,name,age,sex,birthday,created,updated
另外一种用法:
将所有的公用的SQL片段集中定义到一个Mapper.xml文件中,并将此Mapper.xml加入到全局配置文件mybatis-config.xml ,其他Mapper.xml文件如需引入,通过命名空间.id即可。
id,user_name,password,name,age,sex,birthday,created,updated