mybatis学习记录------2

 一  单条记录查询

上篇博客的例子便是单条记录查询

二 多条记录查询

1 在映射文件中加入如下sql查询,${}:表示拼接sql串,将接收到的参数不加任何修饰拼接在sql中,${value}:接收输入参数的内容,如果传入类型是简单类型,${}中只能使用value。

<!--根据名称查找记录,可能返回多条记录
     resultType:指定就是单条记录所映射的java对象类型 
     ${}:表示拼接sql串,将接收到的参数不加任何修饰拼接在sql中。
     使用${}拼接sql,容易引起sql注入
     ${value}:接收输入参数的内容,如果传入类型是简单类型,${}中只能使用value
      -->
     <select id="findUserByName" parameterType="String" resultType="pers.czs.mybatis.po.User">
         select * from user where username like '%${value}%'
     </select>

2 在测试代码中加入如下执行代码,由于查询可能出现多条,因此应该使用list集合存放selectList查询出来的多条记录

@Test
    public void findUserByNameTest() throws IOException {
        //Mybatis配置文件
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会话工厂,传入配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        List<User> list = sqlSession.selectList("test.findUserByName", "小明");
        System.out.println(list);
        sqlSession.close();
    }

3 运行结果

三 添加用户(insert)

1 在映射文件中加入相应的代码:其中parameterType:指定输入参数类型是pojo,无返回值,若主键是自增的,则可以不写主键,mysql会自动分配,我这里主键是ID,是自增的,故没写出来

<!--添加用户 
         parameterType:指定输入参数类型是pojo
         #{}中指定pojo属性名,接收到pojo对象的属性值,mybatis通过OGNL获取对象属性值
         若主键是自增的,则可以不写主键,mysql会自动分配,我这里主键是ID,是自增的,故没写出来
      -->
     <insert id="insertUser" parameterType="pers.czs.mybatis.po.User">
         insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
     </insert>

2 测试代码 此处记得使用sqlSession进行commit

@Test
    public void insertUserTest() throws IOException {
        //Mybatis配置文件
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会话工厂,传入配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        User user = new User();
        user.setAddress("福建福州");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setUsername("零零七");
        sqlSession.insert("test.insertUser", user);
        
        //提交
        sqlSession.commit();
        sqlSession.close();
    }

3 测试结果

四 添加用户--主键返回

1 自增主键返回

 修改上述插入用户的映射文件代码,last_insert_id(),得到刚刚insert进去记录的主键值,只适用自增主键,keyProperty:将查询到的主键设置到parameterType指定的对象的特定属性中,resultType:指定select last_insert_id()的结果类型

<insert id="insertUser" parameterType="pers.czs.mybatis.po.User">
         <!--将插入数据的主键返回,返回到user对象中
         last_insert_id():得到刚刚insert进去记录的主键值,只适用自增主键
         keyProperty:将查询到的主键设置到parameterType指定的对象的特定属性中
         resultType:指定select last_insert_id()的结果类型
          -->
         <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
             select last_insert_id() 
         </selectKey>
         insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
     </insert>

2 在测试代码中加入输出id的语句

@Test
    public void insertUserTest() throws IOException {
        //Mybatis配置文件
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会话工厂,传入配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        User user = new User();
        user.setAddress("福建福州");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setUsername("零零七");
        sqlSession.insert("test.insertUser", user);
        
        System.out.println(user.getId());
        //提交
        sqlSession.commit();
        sqlSession.close();
    }

3 输出结果

五 非自增主键返回

1 使用mysql的uuid()函数生成主键,需要修改表中id字段类型为string,长度设置为35位

思路:

先通过uuid()查询到主键,将主键输入到sql语句中。

执行uuid()语句顺序相对于insert语句之前执行

将上面的映射文件代码改成如下:

<!-- 
          使用mysql的uuid()生成主键
          执行过程:
          首先通过uuid()得到主键,将主键设置到user对象的id属性中
          其次在执行insert时,从user对象取出id属性值
          -->
         <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
             select uuid()
         </selectKey>
         insert into user (id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})

猜你喜欢

转载自www.cnblogs.com/czsy/p/10354202.html