在Mybatis入门01–第一个Mybatis程序的基础上进行CRUD
1. namespace
namespace 中的包名要和 Dao/mapper 中的包名一致!
2. select、insert、update、delete
查询语句
- id:就是对应namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
步骤
1. 编写接口UserMapper.java
package com.ywm.mapper;
import com.ywm.pojo.User;
import java.util.List;
//这里用mapper 等价于我以前用的Dao 以后都会用mapper
public interface UserMapper {
// 获取全部用户
List<User> getUserList();
// 根据ID查询用户
User getUserById(int id);
// insert 一个用户
int addUser(User user);
// 修改一个用户
int updateUser(User user);
// 删除一个用户
int deleteUser(int id);
}
2. 编写对应的 UserMapper.xml 中的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">
<!-- namespace = 绑定一个对应的Mapper接口 -->
<!-- 千万不能写错!!!! -->
<mapper namespace="com.ywm.mapper.UserMapper">
<!-- 查询语句 -->
<!-- 里面的id对应上面接口的方法名 -->
<select id="getUserList" resultType="com.ywm.pojo.User">
select * from user
</select>
<!--根据 id 查询用户,#{} 占位符取id-->
<select id="getUserById" parameterType="int" resultType="com.ywm.pojo.User">
select * from user where id = #{id}
</select>
<!--insert 一个用户, insert没有返回-->
<!--对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.ywm.pojo.User">
insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd});
</insert>
<!--修改一个用户-->
<update id="updateUser" parameterType="com.ywm.pojo.User">
update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};
</update>
<!--删除一个用户-->
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
</mapper>
3. 测试
需要特别注意的是:增删改需要提交事务
package mapper;
import com.ywm.mapper.UserMapper;
import com.ywm.pojo.User;
import com.ywm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test() {
// 第一步:获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
sqlSession = MybatisUtils.getSqlSession();
// 执行SQL
// 方法一:getMapper 建议使用
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
// 方法二: 记住有这个方法就行
// List<User> userList = sqlSession.selectList("mapper.UserMapper.getUserList");
for (User user : userList) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭SqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
// 第一步:获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 获取 mapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 拿到对象
User user = mapper.getUserById(1);
// 输出
System.out.println(user);
// 关闭SqlSession
sqlSession.close();
}
// 增删改需要提交事务
@Test
public void addUser(){
// 第一步:获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 获取 mapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 拿到对象
int res = mapper.addUser(new User(5,"哈哈","123123"));
if (res > 0) {
System.out.println("插入成功");
}
// 必须要写提交事务,才能插入成功
sqlSession.commit();
// 关闭SqlSession
sqlSession.close();
}
@Test
public void uodateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"呵呵","123123123"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(1);
sqlSession.commit();
sqlSession.close();
}
}