当前位置:主页 > 查看内容

Mybatis的增删改查以及拓展知识详解

发布时间:2021-05-24 00:00| 位朋友查看

简介:CRUD 1、namespace命名空间 namespace 中的包名要和接口中的一致全路径。 mapper namespace com.liu.dao.UserMapper 操作语句………… / mapper 2、select选择查询语句 id就是对应的namespace中的方法名 rusultTypesql语句执行的返回值 parameterType参数类……

CRUD

1、namespace命名空间

  • namespace 中的包名要和接口中的一致,全路径。
<mapper namespace="com.liu.dao.UserMapper">
	操作语句…………
</mapper>

2、select选择,查询语句

  • id:就是对应的namespace中的方法名;
  • rusultType:sql语句执行的返回值;
  • parameterType:参数类型;

(1)编写接口

//通过id查询用户
    User getUserById(int id);

(2)编写对应的mapper中的sql语句

 <select id="getUserById" parameterType="int" resultType="com.liu.enity.User">
        select * from mybatis.user where id = #{id}
</select>

(3)测试(注意:增删改必须要提交事务,sqlSession.commit() ; )

@Test
 public void getUserById(){
     SqlSession sqlSession = MybatisUtils.getSqlSession();
     UserMapper mapper = sqlSession.getMapper(UserMapper.class);
     User userById = mapper.getUserById(1);
     System.out.println(userById);
     sqlSession.close();
}

3、insert插入数据

<insert id="addUser" parameterType="com.liu.enity.User">
       insert into mybatis.user(id,name,pwd) value (#{id},#{name},#{pwd})
</insert>

4、update更新数据

 <update id="updateUser" parameterType="com.liu.enity.User">
            update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}
</update>

5、delete删除数据

    	<delete id="deleteUser" parameterType="int">
            delete from mybatis.user where id = #{id}
        </delete>

特别注意点:增删改一定要提交事务(在测试类中)才可以成功。

例:

@Test
 public void getUserById(){
     SqlSession sqlSession = MybatisUtils.getSqlSession();
     UserMapper mapper = sqlSession.getMapper(UserMapper.class);
     User userById = mapper.getUserById(1);
     System.out.println(userById);
     //提交事务
     sqlSession.commit();
     sqlSession.close();
}

6、分析错误

  • 标签匹配问题
  • resources绑定mapper,需要使用路径,使用(/),不能用(.)
  • 程序配置文件必须符合规范
  • NullPointerException,没有注册到资源
  • 输出的xml文件中,存在中文乱码问题
  • maven资源没有导出问题:在pom文件中加入build标签

SQL片段:(将公用的sql提出来用来公用),通过include引用到需要的地方。

但是不推荐使用

<sql  id="stuColumn">sid ,sname,sex</sql>
    <select id = "search" resultType="student">
    	select
    	<include refid="stuColumn"/>  from student where sname……
</select>

7、使用Map(多个参数)

方法一:假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应该考虑用Map

   //使用Map插入一个用户
    int addUser2(Map<String,Object> map);
     <insert id="addUser2" parameterType="map">
            insert into mybatis.user (id,name,pwd) value (#{userId},#{userName},#{password})
     </insert>

     //使用Map插入数据
        @Test
        public void testAddUser2(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userId",5);
            map.put("userName","客行");
            map.put("password","0510");
            mapper.addUser2(map);
            sqlSession.commit();
            sqlSession.close();
        }

  • Map传递参数,直接在sql中取出key即可!【parameterType=“map”】

  • 对象传递参数,直接在sql中取出属性即可!【parameterType=“Object”】

  • 只有一个参数的情况下,可以直接在sql中取出!

  • 多个参数用Map,或者注解

方法二:实体类对象传参

    @org.junit.Test
        public void select() {
            try {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                SqlSession sqlSession = sqlSessionFactory.openSession();
    
                Student student = new Student();
                student.setId(1);
                Student select = sqlSession.selectOne("select", student);
                System.out.println(select);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     <select id="select" resultType="com.liu.pojo.Student">
          select * from student where id = #{id}
      </select>

8、模糊查询

方式一:java代码执行时,传递通配符(%,%)

     //模糊查询
        List<User> getUserList2(String value);
    
    	<select id="getUserList2" resultType="com.liu.enity.User">
            select * from mybatis.user where name like #{value}
        </select>
    
    
        //模糊查询
        @Test
        public void getUserList(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserList2("%老%");
            for (User user : userList) {
                System.out.println(user);
            }
            sqlSession.close();
        }

方式二:在sql中拼接使用通配符

<select id="getUserList2" resultType="com.liu.enity.User">
     select * from mybatis.user where name like "%"#{value}"%"
</select>

好了,Mybatis的增删改查以及拓展内容的分享到这里就结束了,希望可以对大家有帮助。

;原文链接:https://blog.csdn.net/mmy_ly/article/details/115522403
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐