??????? 1.namespace中的包名要和Dao/mapper接口的包名一致
??????? 2.id:就是对应的namespace中的方法名
??????? 3.resultType:sql语句执行的返回值
??????? 4.parameterType:参数类型
????????5.增删改需要提交事务
??????? 1.编写接口
//根据id查询用户
User getUserById(int id);
??????? 2.编写对应的mapper中的sql语句
<select id="getUserById" parameterType="int" resultType="com.sun.pojo.User">
select * from user where id = #{id}
</select>
??????? 3.编写测试类
@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();
}
??????? 1.编写接口
//插入用户
int addUser(User user);
??????? 2.编写对应的mapper中的sql语句
<!-- 对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.sun.pojo.User" >
insert into user (id,name,pwd) value (#{id},#{name},#{pwd})
</insert>
??????? 3.编写测试类
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(4,"哈哈","123333"));
if(res>0){
System.out.println("插入成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
??????? 1.编写接口
//修改用户
int updateUser(User user);
??????? 2.编写对应的mapper中的sql语句
<update id="updateUser" parameterType="com.sun.pojo.User">
update user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
??????? 3.编写测试类
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.updateUser(new User(4,"hehe","123123"));
if(res>0){
System.out.println("修改成功");
}
sqlSession.commit();
sqlSession.close();
}
??????? 1.编写接口
//删除用户
int deleteUser(int id);
??????? 2.编写对应的mapper中的sql语句
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
??????? 3.编写测试类
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.deleteUser(4);
if(res>0){
System.out.println("删除成功");
}
sqlSession.commit();
sqlSession.close();
}
??????? 1.标签不要匹配错。
??????? 2.resource绑定mapper需要使用路径。
??????? 3.程序配置文件必须符合规范。
??????? 4.NullPointerException,没有注册到资源。
??????? 5.输出的xml文件中存在中文乱码问题。
??????? 6.maven资源没有导出。
假设,我们实体类,或者数据库中的表字段或者参数过多,我们应该考虑使用map
??????? 1.编写接口
//万能的map
int addUser2(Map<String,Object> map);
??????? 2.编写对应的mapper中的sql语句
<insert id="addUser2" parameterType="map" >
insert into user (id,name,pwd) value (#{userid},#{userName},#{passWord})
</insert>
??????? 3.编写测试类
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap<String,Object>();
map.put("userid",5);
map.put("userName","Hello");
map.put("passWord","222222333");
mapper.addUser2(map);
//提交事务
sqlSession.commit();
sqlSession.close();
}
map传递参数,直接在sql中取出key即可。【parameterType="map"】
对象传递参数,直接在sql中取对象属性即可。【parameterType="对应的类型"】
只有一个基本类型参数的情况下,可以直接在sql中取到。
多个参数用map或注解。
??????? 1.编写接口
//模糊查询
List<User> getUserLike(String value);
??????? 2.编写对应的mapper中的sql语句
<!-- 方式一 -->
<select id="getUserLike" resultType="com.sun.pojo.User">
select * from user where name like #{value}
</select>
<!-- 方式二 -->
<select id="getUserLike" resultType="com.sun.pojo.User">
select * from user where name like "%"#{value}"%"
</select>
??????? 3.编写测试类
//方式一
@Test
public void getUserLike(){
//获取sqlsession对象
SqlSession sqlsession = MybatisUtils.getSqlSession();
//执行方式一:getMapper
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%李%");
for (User user : userList) {
System.out.println(userList);
}
sqlsession.close();
}
//方式二
@Test
public void getUserLike(){
//获取sqlsession对象
SqlSession sqlsession = MybatisUtils.getSqlSession();
//执行方式一:getMapper
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("李");
for (User user : userList) {
System.out.println(userList);
}
sqlsession.close();
}
?