一. namespace
- namespace中的包名要和 Dao/mapper 接口的包名一致!
<mapper namespace="com.hjf.dao.PeopleMapper"> ... </mapper>
二. select语句
1. 说明
- select: 查询语句
- id: 对应方法名
- resultType: sql语句执行的返回值类型(完整的名称)
- parameterType: 参数类型
2. 代码
-
编写接口
/** * 获取人员信息列表 * @return */ List<People> getPeopleList(); /** * 通过id查询人员信息 * @param id * @return */ People getPeopleById(int id); /** * 通过用户名模糊查找 * @param name * @return */ List<People> getPeopleListByLike(String name);
-
编写对应的mapper中的sql语句
<!-- select: 查询语句 id: 对应方法名 resultType: sql语句执行的返回值类型(完整的名称) --> <select id="getPeopleList" resultType="com.hjf.pojo.People"> select * from people </select> <!-- parameterType: 参数类型 #{id}: 参数名称与实体类的属性名保持一致 --> <select id="getPeopleById" resultType="com.hjf.pojo.People" parameterType="int"> select * from people where id=#{id} </select> <select id="getPeopleListByLike" resultType="com.hjf.pojo.People" parameterType="String"> select id, name, pwd from people where name like #{name} </select>
-
编写测试函数
@Test public void testGetPeopleList() { // 1. 获得sqlSession的对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); // 方式1: getMapper PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); List<People> peopleList = mapper.getPeopleList(); // 方法2: // List<People> peopleList = sqlSession.selectList("com.hjf.dao.PeopleMapper.getPeopleList"); for (People people : peopleList) { System.out.println(people); } // 关闭资源 sqlSession.close(); } @Test public void testGetPeopleById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); People people = mapper.getPeopleById(3); System.out.println(people); sqlSession.close(); } @Test public void testGetPeopleListByLike(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); List<People> peopleList = mapper.getPeopleListByLike("%张%"); for (People people: peopleList) { System.out.println(people); } sqlSession.close(); }
三. Insert
1. 代码
-
编写接口
/** * 添加人员信息 * @param people * @return */ int addPeople(People people);
-
编写对应的mapper中的sql语句
<!-- insert: 插入语句 需要传递类中的所有参数 --> <insert id="addPeople" parameterType="com.hjf.pojo.People"> insert into people(id, name, pwd) values(#{id}, #{name}, #{pwd}) </insert>
-
编写测试函数
@Test public void testAddPeople() { SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); int flag = mapper.addPeople(new People(8, "people8", "111111")); if (flag > 0) { System.out.println("添加人员信息成功!"); } else { System.out.println("添加人员信息失败!"); } // 提交事务 // 增,删,改必须提交事务 sqlSession.commit(); sqlSession.close(); }
四. update
1. 代码
-
编写接口
/** * 修改人员信息 * @param people * @return */ int updatePeople(People people);
-
编写对应的mapper中的sql语句
<update id="updatePeople" parameterType="com.hjf.pojo.People"> update people set name=#{name}, pwd=#{pwd} where id=#{id} </update>
-
编写测试函数
@Test public void testUpdatePeople() { SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); int flag = mapper.updatePeople(new People(8, "用户8", "888888")); if (flag > 0) { System.out.println("修改用户成功!"); } else { System.out.println("修改用户失败"); } sqlSession.commit(); sqlSession.close(); }
五. Delete
1. 代码
-
编写接口
/** * 通过id删除人员信息 * @param id * @return */ int delPeopleById(int id);
-
编写对应的mapper中的sql语句
<delete id="delPeopleById" parameterType="int"> delete from people where id=#{id} </delete>
-
编写测试函数
@Test public void testDelPeopleById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); int flag = mapper.delPeopleById(8); if (flag > 0) { System.out.println("删除用户成功!"); } else { System.out.println("删除用户失败"); } sqlSession.commit(); sqlSession.close(); }
六. 万能Map
1. 说明
- 之前的方法添加或修改时需要传入实体类中的所有参数, 如果实体类中的属性过多时, 就会很麻烦
- 此时, 我们可以考虑使用Map来传指定的参数
2. 代码
-
编写接口
/** * 通过map传递参数 * @param map * @return */ int addPeople2(Map<String, Object> map);
-
编写对应的mapper中的sql语句
<!-- 只传递了id和name --> <insert id="addPeople2" parameterType="map"> insert into people(id, name) values(#{uid}, #{username}) </insert>
-
编写测试函数
@Test public void testAddPeople2() { SqlSession sqlSession = MybatisUtils.getSqlSession(); PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("uid", "15"); map.put("username", "赵子龙"); int flag = mapper.addPeople2(map); if (flag > 0) { System.out.println("添加人员信息成功!"); } else { System.out.println("添加人员信息失败!"); } // 提交事务 // 增,删,改必须提交事务 sqlSession.commit(); sqlSession.close(); }
说明:
- 本文参考了狂神的Mybatis的课件
- 课程链接