Mybatis(三)返回值四.注解配置

一. Mybatis返回值

   MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

 

   在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。

 

   ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性resultType的时候,MyBatis自动的给对应的值赋给resultType所指定对象的属性。

 

   ②当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用(association,Collection),

  

复制代码

<resultMap type="com.softjx.model.User" id="UserMap">
  <result column="t_id" property="id"/>
  <result column="t_username" property="username" />
  <result column="t_password" property="password"/>
</resultMap>


 <!-- 查询数据表中的所有记录,并封装User对象集合 -->
<select id="selectAll" resultMap="UserMap">
 select * from t_user
</select>

<!-- 根据id查询数据表中的一条记录,并封装User对象 -->
<select id="selectById"  resultMap="UserMap">
 select * from t_user where t_id=#{id};
</select>

//统计id>?记录数
    public int coutUser(Integer id);

//统计id>?记录数

<select id="coutUser" resultType="int">
 select count(*) from t_user where t_id>#{id};
</select>

复制代码

 

二. Mybatis注解配置

 

  Mybatis中使用注解,就不需要编写mapper.xml文件,直接在接口代码中使用注解。

 

 

  1、mybatis的全局配置文件要修改,指向接口文件路

  <mappers>        
         <mapper class="com.softjx.dao.UserMapper"/>         
      </mappers>

 

  2、不用编写mapper.xml文件,写接口,在接口中使用注解

 

  

复制代码

@Select("select t_id as id,t_username username,t_password as password from t_user")
    //@Select("select * from t_user")//不好自动填充javabean中的数据
    //@ResultType(User.class)//不写没有关系
    public List<User> selectAll();
    
    @Select("select t_id as id,t_username username,t_password as password from t_user  where t_id=#{id}")
    //@Select("select * from t_user where t_id=#{id}")//不好自动填充javabean中的数据
    //@ResultType(User.class)//不写没有关系
    public User selectById(int id);

    
    @Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{a} and t_username=#{b}")
    @ResultType(User.class)
    public List<User> selectByNameId(Map<String, Object> map);
    
    
    
    
    @Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{0} and t_username=#{1}")
    @ResultType(User.class)
    public List<User> selectByNameId1(Integer id,String ame);
    

    
    @Select("select t_id as id,t_username username,t_password as password from t_user where t_id > #{id} and t_username=#{name}")
    @ResultType(User.class)
    public List<User> selectByNameId2(@Param("id")Integer id,@Param("name")String name);
    
    

    
    @Select("select * from t_user")
    @Results({@Result(property="id",column="t_id")
             ,@Result(property="username",column="t_username")
             ,@Result(property="password",column="t_password")
             
            })
        public List<User> selectAllUsers();
    

    
    
    @Insert("insert into t_user (t_username,t_password) values (#{username},#{password})")
    public int insertUser(User user);
    

    @Update("update t_user set t_username=#{username},t_password=#{password} where t_id=#{id}")
    public int updateUser(User user);

    
    
    @Delete("delete from t_user where t_id=#{a}")
    public int deleteUser(int id);

        

复制代码

  3、编写测试类与以前一样,没有区别。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用SpringBoot整合MyBatis时,个文件的作用分别如下: 1. `application.properties`或`application.yml`:用于配置应用程序的各种属性,包括数据库连接信息、数据源信息、日志输出级别等。其中,需要配置MyBatis的数据源信息,如下所示: ``` spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10 spring.datasource.username=root spring.datasource.password=123456 ``` 2. `UserMapper.xml`:MyBatis的Mapper文件,提供SQL语句的定义和映射关系的配置。在该文件中,需要定义SQL语句的ID、参数类型、返回值类型、SQL语句等信息,如下所示: ``` <?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.mapper.UserMapper"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.example.entity.User"> select * from user where id = #{id,jdbcType=INTEGER} </select> </mapper> ``` 3. `pom.xml`:Maven的配置文件,用于定义项目的依赖关系和打包方式等信息。 这个文件是整合MyBatis必须的,并且需要同时存在。除此之外,还需要在SpringBoot的主类上添加`@MapperScan`注解,指定MyBatis的Mapper接口所在的包路径,如下所示: ``` @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,就可以成功地整合MyBatis了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值