parameterType和@Param注解用法

在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了
parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的
输入参数类型即可。parameterType有基本数据类型和复杂的数据类型配置。
1.基本数据类型,如输入参数只有一个,其数据类型可以是基本的数据类型,也可以是
自己定的类类型。包括int,String,Integer,Date,如下:

 (1)根据id进行相应的删除:<delete id="deleteById" parameterType="Integer">

(2)添加员工:<insert id="addEmp" parameterType="com.pojo.Employee">

2.复杂数据类型:包含java实体类,map。

配置如:

  <select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher">  

       select * from Teacher where c_id=#{id} and sex=#{sex} 

  </select>  

java代码如下:

    Map<String,String> map=new HasMap<String,String>();  

    map.put("id","2");  

    map.put("sex","男");  

    List<Teacher> tList = teacherMapper.selectTeacher(map);   

    for (Teacher entityTemp : tList) {    

    System.out.println(entityTemp.toString()); } 

另外MyBatis还提供了一个使用注解来参入多个参数的方式。这种方式需要在接口的参数上添加@Param注解

    /**
     * 此处要注意的是,由于该方法需要传入多个参数,在进行Mybatis配置时,
     * 没有办法同时配置多个参数,另外MyBatis还提供了一个使用注解来参入
     * 多个参数的方式。这种方式需要在接口的参数上添加@Param注解。。
     * 注意,以下两种写法是完全相同的。但使用的时候要使用第一种类型
     */
    
    User login(@Param(value="name")String name,@Param(value="password")String password );
//    User login(String name,String password);

配置如下:

 <select id="login"  resultType="com.pojo.User">
    select * from us where name=#{name} and password=#{password}
   </select>

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,这是一个关于MyBatis的问题。以下是实现对表t_article的查改方法的代码示例。 首先,我们需要创建Article实体类和ArticleMapper接口。Article实体类中包含id、title、content和createTime等属性。 ```java public class Article { private Long id; private String title; private String content; private Date createTime; // getter and setter methods } ``` 接下来,创建ArticleMapper接口,并使用@Mapper注解声明这是一个Mapper接口。在ArticleMapper接口中,我们通过@Select和@Update注解实现对t_article表的查询和更新操作。 ```java @Mapper public interface ArticleMapper { @Select("SELECT * FROM t_article WHERE id = #{id}") Article findById(@Param("id") Long id); @Update("UPDATE t_article SET title = #{title}, content = #{content}, create_time = #{createTime} WHERE id = #{id}") void update(Article article); } ``` 接下来,我们创建ArticleMapper.xml文件,并实现对应的查询和更新操作。在ArticleMapper.xml文件中,我们使用<select>和<update>标签实现对t_article表的查询和更新操作。 ```xml <?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.example.demo.mapper.ArticleMapper"> <select id="findById" parameterType="java.lang.Long" resultType="com.example.demo.entity.Article"> SELECT * FROM t_article WHERE id = #{id} </select> <update id="update" parameterType="com.example.demo.entity.Article"> UPDATE t_article SET title = #{title}, content = #{content}, create_time = #{createTime} WHERE id = #{id} </update> </mapper> ``` 在以上代码中,<select>和<update>标签的id属性对应ArticleMapper接口中方法的名称。parameterType和resultType属性分别表示参数类型和返回值类型。 最后,我们可以编写测试用例来测试这些方法的正确性,例如: ```java @RunWith(SpringRunner.class) @SpringBootTest public class ArticleMapperTest { @Autowired private ArticleMapper articleMapper; @Test public void testFindById() { Article article = articleMapper.findById(1L); assertNotNull(article); assertEquals("test title", article.getTitle()); } @Test public void testUpdate() { Article article = articleMapper.findById(1L); article.setTitle("updated title"); article.setContent("updated content"); article.setCreateTime(new Date()); articleMapper.update(article); article = articleMapper.findById(1L); assertEquals("updated title", article.getTitle()); assertEquals("updated content", article.getContent()); } } ``` 以上测试用例使用JUnit和Spring Boot框架编写,分别测试了查询和更新操作的正确性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值