美文网首页
Mybatis 学习(二)

Mybatis 学习(二)

作者: 时代劫匪 | 来源:发表于2020-05-21 23:03 被阅读0次

    1.namespace

    namespace 中的 类名要与Dao包下的接口名一致,且为全限定名

    2.select insert update delete标签参数

    • id:就是对应的namespace中的方法名
    • resultType:sql语句的返回值
    • parameterType:参数类型
    <?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">
    <mapper namespace="com.sun.dao.UserDao">
        <select id="getUserList" resultType="com.sun.pojo.User">
            select * from user
        </select>
    
        <select id="getUserById" resultType="com.sun.pojo.User" parameterType="int">
            select * from user where id = #{id}
        </select>
    
        <insert id="addUser" parameterType="com.sun.pojo.User">
            insert into user(name,pwd) values (#{name},#{pwd})
        </insert>
    
        <update id="updateUser" parameterType="com.sun.pojo.User">
            update user set name=#{name },pwd=#{pwd }  where id=#{id};
        </update>
    
        <delete id="deleteUser" parameterType="int">
            delete from user where id=#{id}
        </delete>
    </mapper>
    

    3.Dao层

    package com.sun.dao;
    
    import com.sun.pojo.User;
    
    import java.util.List;
    
    public interface UserDao {
        List<User> getUserList();
    
        User getUserById(int id);
    
        int addUser(User user);
    
        int updateUser(User user);
    
        int deleteUser(int id);
    }
    

    4.测试

    package com.sun.dao;
    
    import com.sun.pojo.User;
    import com.sun.utils.MybatisUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    public class UserDaoTest {
        @Test
        public void test(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            for (User user : mapper.getUserList()) {
                System.out.println(user);
            }
            sqlSession.close();
        }
    
        @Test
        public void test1(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            User user = mapper.getUserById(2);
            System.out.println(user);
            sqlSession.close();
        }
    
        @Test
        public void test3(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            User user = new User(5,"ceshi","asdsadasd");
            int i = mapper.addUser(user);
            sqlSession.commit();
            if(i>0){
                System.out.println("增加成功");
            }
            sqlSession.close();
        }
    
        @Test
        public void test4(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            User user = new User(5,"ceshi","55555555555555");
            int i = mapper.updateUser(user);
            sqlSession.commit();
            if(i>0){
                System.out.println("修改成功");
            }
            sqlSession.close();
        }
    
        @Test
        public void test5(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            int i = mapper.deleteUser(5);
            sqlSession.commit();
            if(i>0){
                System.out.println("删除成功");
            }
            sqlSession.close();
        }
    }
    

    注意修改数据库是提交事务

    5.错误排查

    • 标签不要匹配错
    • resource绑定Mapper文件,需要使用路径
    • 程序配置文件必须符合规范
    • 空指针异常,没有注册到资源
    • 输出文件中文乱码问题
      idea配置中文编码
    • maven资源没有导出

    6.Map标签

    int addUser2(Map<String,Object> map);
    
    <insert id="addUser2" parameterType="Map">
            insert into user(name,pwd) values (#{username },#{userpwd })
    </insert>
    
    @Test
        public void test5(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserDao mapper = sqlSession.getMapper(UserDao.class);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("username","撒点娇阿萨德拉萨家里卡上");
            map.put("userpwd","kkkkkkkkkkkk");
            int i = mapper.addUser2(map);
            if(i>0){
                System.out.println("添加成功");
            }
            sqlSession.commit();
            sqlSession.close();
        }
    

    map可以替代类型,万能的map,xml中进行赋值时,为map的key值

    7.resultMap

    <!-- 解决数据库字段与实体属性不一致的问题 -->
        <resultMap id="userMap" type="User">
            <result column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="pwd" property="pwd"/>
        </resultMap>
    
    • resultMap 元素是MyBatis中最重要的最强大的元素
    • resultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了
    • resultMap 最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式的用到它们
    • 如果世界总是这么简单就好了

    8.日志

    如果一个数据库操作,出现了异常,我们需要排错,日志就是最好的助手
    曾经:sout,debug
    现在:日志工厂

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    
    image.png

    9.分页

    思考:为什么分页?

    • 减少数据的处理量
      limit分页
    语法:select * from user limit startIndex,pageSize   //startIndex 为开始条数,pageSize 每页多少条数据
    select * from user limit 3  [0,n]
    
    List<User> getUserByLimit(Map<String,Integer> map);
    
    <select id="getUserByLimit" parameterType="map" resultType="User">
          select * from user limit #{startIndex},#{pageSize}
    </select>
    
    //测试
    @Test
        public void testLimit(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("startIndex",0);
            map.put("pageSize",2);
            for (User user : mapper.getUserByLimit(map)) {
                System.out.println(user);
            }
            sqlSession.close();
        }
    

    github地址:https://github.com/sunmaio159/mybatis-study

    相关文章

      网友评论

          本文标题:Mybatis 学习(二)

          本文链接:https://www.haomeiwen.com/subject/zrpxahtx.html