namespace
namespace中的包名要和Dao/mapper接口的包名一致
crud
选择,查询语句
- id:对应namespace中的方法名;
- resultType:Sql语句执行的返回值
- parameterType:参数类型
编写接口
package com.qin.dao;
import com.qin.pojo.User;
import java.util.List;
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//插入一个用户
int addUser(User user);
//更新一个用户
int updateUser(User user);
//删除一个用户
int deleteUserByid(int id);
}
编写对应的mapper中的sql语句
<?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">
<!--类似继承UserDao接口-->
<mapper namespace="com.qin.dao.UserMapper">
<!-- 实现UserDao里面的方法-->
<select id="getUserList" resultType="com.qin.pojo.User">
select * from mybatis.user;
</select>
<!-- 对象中的属性可以直接拿到-->
<select id="getUserById" parameterType="int" resultType="com.qin.pojo.User">
select * from `user` where id=#{id}
</select>
<insert id="addUser" parameterType="com.qin.pojo.User">
insert into `user` values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="com.qin.pojo.User">
update `user` set `name` = #{name} , pwd = #{pwd} where id = #{id}
</update>
<delete id="deleteUserByid" parameterType="int">
delete from `user` where id=#{id}
</delete>
</mapper>
测试
package com.qin.dao;
import com.qin.pojo.User;
import com.qin.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void getUser(){
//使用工具类获得SqlSesison
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void testGetUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//使用SqlSeeion对象的getMapper方法 得到一个UserMapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//使用得到的UserMapper对象 调用里面的方法
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
//增删改需要提交事务
@Test
public void testAddUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//使用SqlSeeion对象的getMapper方法 得到一个UserMapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//使用得到的UserMapper对象 调用里面的方法
int row = mapper.addUser(new User(4,"灰太狼","123456"));
System.out.println(row);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//使用SqlSeeion对象的getMapper方法 得到一个UserMapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//使用得到的UserMapper对象 调用里面的方法
int row = mapper.updateUser(new User(4,"红太狼","123456"));
System.out.println(row);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//使用SqlSeeion对象的getMapper方法 得到一个UserMapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//使用得到的UserMapper对象 调用里面的方法
int row = mapper.deleteUserByid(4);
System.out.println(row);
sqlSession.commit();
sqlSession.close();
}
}
Map
使用map传递参数
编写接口
//使用map传递参数
//插入一个用户
int addUser2(Map map);
配置接口
<insert id="addUser2" parameterType="Map">
<!-- 传递的参数名是map中的键 -->
insert into `user` values (#{id},#{name},#{pwd})
</insert>
测试
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//使用SqlSeeion对象的getMapper方法 得到一个UserMapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//使用得到的UserMapper对象 调用里面的方法
Map<String,Object> map = new HashMap<>();
map.put("table","`user`");
map.put("id","4");
map.put("name","孙悟空");
map.put("pwd","666666");
int row = mapper.addUser2(map);
System.out.println(row);
sqlSession.commit();
sqlSession.close();
}
模糊查询
编写接口
//模糊查询
List<User> getUserList2(String name);
配置接口
使用通配符
<select id="getUserList2" resultType="com.qin.pojo.User">
select * from `user` where `name` like "%"#{name}"%"
</select>
测试
@Test
public void getUser2(){
//使用工具类获得SqlSesison
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList2("孙");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}