Mybatis(一)Mapper文件的编写

前面基于Maven搭了SSM框架,基于该框架使用Mybatis操作数据库
不写配置了,直接从mapper开始。

1.ResultMap标签的使用

在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。

  1. 通过JDBC查询得到ResultSet对象

  2. 遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值

  3. 根据ResultMap标签的type属性通过反射实例化领域模型

  4. 根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回

<resultMap id="userMapper" type="com.spring.pojo.User">
        <id property="id" column="id"/>
        <result property="userName" column="username"/>
        <result property="password" column="password"/>
        <result property="email" column="email"/>
        <result property="isLock" column="isLock"/>
        <result property="rePassword" column="repassword"/>
</resultMap>

resultMap中的id是指给这个Map取一个名字,因为后面关联查询可能还会用到这个映射,type是指他要映射到的Bean类型,是pojo层的实体类

result中property和column分别指定实体类属性和数据表的列名。

2.查询语句的编写

2.1查询所有用户

mapper中的sql语句是根据Dao层写的,dao层有什么方法mapper就要写对应的。

<select id="findAll" resultType="com.spring.pojo.User">
        select * from user
</select>

需要注意id是和dao的接口名称一致的
resultType用于指定执行SQL语句返回给dao的类型,此处虽然是一个链表,但是只需要指出其中的一个元素的类型即可。PS:如果用free mybatis plugin插件就可以直接在dao层通过alt+enter生成mapper了。

这里用上面写的resultMapper也是一样,没有太大的区别。

插件地址:https://github.com/wuzhizhan/free-idea-mybatis
server层就不写了,调用dao层方法就行
controller层:

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<User> list(){
        List<User> users = userService.findAll();
        return users;
    }

此处通过@ResponseBody注解直接返回json
效果如下

2.2按照某一属性查找

    <select id="findByName" resultType="com.spring.pojo.User" parameterType="string">
        select * from user where username = #{name}
    </select>

parameterType用来指定传入参数的类型,#{name}是传入的变量,需要和Dao层接口的形参名称一致。
resultType同样指定查询后返回的类型

service层

@RequestMapping(value = "/findbyname/{name}", method = RequestMethod.GET)
    @ResponseBody
    public User find(@PathVariable(value = "name") String name){
        User user = userService.findByName(name);
        return user;
}

此处采用@PathVariable获取url中的参数,此处为RESTful风格API

2.3参数不止一个

2.3.1使用Map

<select id="findByIdName" resultType="com.spring.pojo.User" parameterType="map">
        select * from user where username = #{name} and id = #{id}
</select>
@RequestMapping(value = "/findbyidname/{id}/{name}", method = RequestMethod.GET)
    @ResponseBody
    public User find(@PathVariable(value = "id") Integer id, @PathVariable(value = "name") String name){
        Map m = new HashMap();
        m.put("name", name);
        m.put("id", id);
        User user = userService.findByIdName(m);
        return user;
    }

2.3.2Dao层使用@param注解

dao

public User findByNameLock(@Param("name")String name, @Param("lock") Integer lock);

mapper

<select id="findByNameLock" resultType="com.spring.pojo.User">
        select * from user where username = #{name} and isLock = #{lock}
</select>

好像比较推荐这种写法

2.4返回单个属性

比如只查询是否被锁定,也就是Integer类型的isLock

<select id="isLock" resultType="integer">
        select isLock from user where username = #{name}
</select>
@RequestMapping(value = "/islock/{name}", method = RequestMethod.GET)
    @ResponseBody
    public Map isLock(@PathVariable(value = "name") String name){
        Integer isLock = userService.isLock(name);
        Map m = new HashMap();
        m.put("islock", isLock);
        return m;
}

3.插入

<insert id="createUser">
        insert into user (username) values (#{name})
</insert>

注意这里(username)中的username不需要加引号

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值