文章目录
- 1.新增mapper接口
- 2.修改mapper接口同级目录下的xml文件(UserMapper.xml)
- 3.编写src同级别目录下的test文件
- 4.万能的Map
1.新增mapper接口
package com.aloha.dao;import com.aloha.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 deleteUser(int id);
}
2.修改mapper接口同级目录下的xml文件(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对于的Dao/Mapper接口-->
<mapper namespace="com.aloha.dao.UserMapper"><!--select查询语句--><select id="getUserList" resultType="com.aloha.pojo.User">select * from mybatis.user;</select><select id="getUserById" parameterType="int" resultType="com.aloha.pojo.User">select * from mybatis.user where id = #{id}</select><insert id="addUser" parameterType="com.aloha.pojo.User">insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});</insert><update id="updateUser" parameterType="com.aloha.pojo.User">update mybatis.userset name=#{name}, pwd=#{pwd}where id=#{id};</update><delete id="deleteUser">delete from mybatis.user where id=#{id}</delete>
</mapper>
3.编写src同级别目录下的test文件
package com.aloha.dao;import com.aloha.pojo.User;
import com.aloha.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class UserDaoTest {@Testpublic void test() {// 1. 获取SqlSessionFactory对象SqlSession sqlSession = MybatisUtils.getSqlSession();// 2. 执行sql// 方式一: getMappertry {UserMapper userdao = sqlSession.getMapper(UserMapper.class);List<User> userList = userdao.getUserList();for (User user : userList) {System.out.println(user);}}catch(Exception e) {e.printStackTrace();}finally {// 3. 关闭SqlSessionsqlSession.close();}}@Testpublic void test2() {SqlSession sqlSession = MybatisUtils.getSqlSession();try {UserMapper usermapper = sqlSession.getMapper(UserMapper.class);User user = usermapper.getUserById(1);System.out.println(user);}catch(Exception e) {e.printStackTrace();}finally {// 3. 关闭SqlSessionsqlSession.close();}}@Testpublic void test3() {SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);int res = mapper.addUser(new User(4, "周杰伦", "12333"));if(res > 0) {System.out.println("插入成功");}// 提交事务sqlSession.commit();sqlSession.close();}@Testpublic void test4() {SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);int res = mapper.updateUser(new User(1, "无敌", "12333"));if(res > 0) {System.out.println("修改成功");}sqlSession.commit();sqlSession.close();}@Testpublic void test5() {SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);int i = mapper.deleteUser(4);System.out.println(i);sqlSession.commit();sqlSession.close();}
}
4.万能的Map
使用场景:比如插入一个对象,不想填所有的方法
UserMapper
// 万能的Mapperint addUser2(Map<String, Object> map);
UserMapper.xml
<insert id="addUser2" parameterType="map">insert into mybatis.user (id, pwd) values (#{userId},#{passWord});</insert>
测试方法
@Testpublic void test6() {SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 4);map.put("passWord", "222333");mapper.addUser2(map);sqlSession.close();}