MybatisStudy01

ce1、环境配置(environments)

 2、别名配置

 将别名配置放到mybatis.config.xml中,(位置需要放到前面)

    <typeAliases>
        <typeAlias type="com.entity.User" alias="User"/>
    </typeAliases>

或者使用这个

    <typeAliases>
        <package name="com.entity"/>
    </typeAliases>

 3、映射器

 4、结果级映射

<!--结果集映射-->
   <resultMap id="UserMap" type="User">
    <!--column数据库中的字段,property实体类中的属性-->
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="pwd" property="password"/>
    </resultMap>
    
  <select id="getUserByid" resultMap="UserMap">
     select * from mybatis.user where id =#{id}
  <select>

字段一样的就不需要再映射了:id,name

 日志,复制上去可能会出错,手敲<settings>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

log4J

导入依赖

        <!--log4J-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>

    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

limit分页

接口

public interface UserMapper {

    //分页查询
    List<User>getUserByLimit(Map<String,Integer>map);

}
<mapper namespace="com.mapper.UserMapper">

    <select id="getUserByLimit"  resultType="User">
        select * from user limit #{startIndex},#{pageSize}
    </select>

</mapper>
    @Test
    public void getUserByLimit(){

        SqlSession sqlseesion = MybatisUtils.getSqlseesion();

        UserMapper mapper = sqlseesion.getMapper(UserMapper.class);

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        map.put("startIndex",0);
        map.put("pageSize",2);

        List<User> limit = mapper.getUserByLimit(map);

        for (User user:limit){
            System.out.println(user);
        }
        sqlseesion.close();
    }

 注解开发

注解写在接口上

 

 

 

 注解CRUD

public interface UserMapper {
    @Select("select * from user")
    List<User> getUsers();
    //方法存在多个参数,所有的参数前面必须加上@param("id")
    @Select("select * from user where id = #{id}")
    User getUserById(@Param("id") int id);
    @Insert("insert into user(id,name,pwd) values(#{id},#{name},#{paswword})")
    int addUser(User user);
    @Update("update user set name=#{name},pwd=#{password} where id=#{id}")
    int updateUser(User user);
    @Delete("delete from user where id=#{id}")
    int deleteUser(@Param("id") int id);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis中,可以使用XML配置文件来实现模糊查询。在XML映射文件中,可以使用两种方式定义参数进行模糊查询。 第一种方式是使用#定义参数。首先,在持久层接口中添加一个根据名字内容模糊查询的方法,方法名为`findByNameLike`,参数类型为`String`,返回类型为`List<User>`。然后,在XML映射文件中添加一个`select`标签,设置`id`为`findByNameLike`,`parameterType`为`string`,`resultType`为`com.mybatisstudy.pojo.User`,并在`select`标签内编写SQL语句,使用`#{name}`来引用参数。具体代码如下所示: ```xml <!-- 使用#定义参数 --> <select id="findByNameLike" parameterType="string" resultType="com.mybatisstudy.pojo.User"> select * from user where username like #{name} </select> ``` 第二种方式是使用$定义参数。在XML映射文件中,将`select`标签内的SQL语句修改为使用`${value}`来引用参数,并在持久层接口中的方法参数前不加`%`。具体代码如下所示: ```xml <!-- 使用$定义参数 --> <select id="findByNameLike" resultType="com.mybatisstudy.pojo.User" parameterType="string"> select * from user where username like '%${value}%' </select> ``` 以上是在XML映射文件中实现模糊查询的两种方式。你可以根据具体需求选择其中一种方式来实现模糊查询。 #### 引用[.reference_title] - *1* *2* *3* [Mybatis模糊查询——种定义参数方法和聚合查询、主键回填](https://blog.csdn.net/qq_53317005/article/details/129762660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三金学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值