1.Mybatis-config.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>
2.namespace
<mapper namespace="com.ming.dao.UserMapper">
在Mybatis-config.xml中绑定一个对应的Mapper接口,namespace中的包名要和Dao/Mapper一致。
3.CRUD
3.1 select
- 接口方法(UserMaper.java)
//根据ID查询用户
User getUserById(int id);
- sql语句(UserMapper.xml)
<select id="getUserById" parameterType="int" resultType="com.ming.pojo.User">
select * from mybatis.user where id=#{id};
</select>
id:对应namespace中的方法名。
parameterType:参数类型。
resultType:sql执行的返回值类型。
- 测试(UserMapperTest.java)
@Test
public void getUserById(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
User user=mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
3.2 insert
- 接口方法(UserMaper.java)
//插入一个用户
int addUser(User user);
- sql语句(UserMapper.xml)
<insert id="addUser" parameterType="com.ming.pojo.User" >
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
resultType:添加返回类型默认是int,所以这里无返回值类型(后面的修改和删除也是一样)。
- 测试(UserMapperTest.java)
@Test
public void addUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
int res=mapper.addUser(new User( 4,"哈哈哈","123"));
if(res>0){
System.out.println("插入成功");
}
sqlSession.commit();//提交事务
sqlSession.close();
}
注意:增删改需要提交事务!
3.2 update
- 接口方法(UserMaper.java)
//修改用户
int updateUser(User user);
- sql语句(UserMapper.xml)
<update id="updateUser" parameterType="com.ming.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
- 测试(UserMapperTest.java)
@Test
public void updateUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"呵呵","1234"));
sqlSession.commit();//提交事务
sqlSession.close();
}
3.2 delete
- 接口方法(UserMaper.java)
//删除用户
int deleteUser(int id);
- sql语句(UserMapper.xml)
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
- 测试(UserMapperTest.java)
@Test
public void deleteUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();//提交事务
sqlSession.close();
}
4.MyBatis-Map
4.1 使用场景
当实体类或者数据库中的字段,参数过多时,应当考虑使用Map。
4.2 代码
- 接口方法
int addUser2(Map<String, Object> map);
- sql实现
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id,name,pwd) values(#{userid},#{username},#{passWord});
</insert>
- 测试
@Test
public void addUser2(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
Map<String,Object> map=new HashMap<String, Object>();
map.put("userid",5);
map.put("userName","hello");
map.put("passWord",234);
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
4.3 Map传递参数和对象传递参数的区别
Map传递参数,直接在sql中取出key即可。
对象传递参数,直接在sql中取对象的属性即可。