栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

mybatis02

Java 更新时间:发布时间: 百科书网 趣学号

目录:

1、使用注解的方式

2、输入输出参数的类型

        输入参数类型

        输出参数类型

3.mybatis中的动态SQL

1、使用注解的方式

		
		public interface UserMapper {
			@Select("select * from userInfo where id=#{id}")
			@ResultType(UserInfo.class)
			UserInfo getUserById(int id);
			
			@Select("select * from userInfo")
		  //@ResultType(UserInfo.class)  这个注解不指定也可以
			List getAll();
			
			@Select("select * from userInfo")
			@Results({
				@Result(id=true,column="id",property="id"),
				@Result(column="userName",property="userName"),
				@Result(column="password",property="password"),
				@Result(column="note",property="note")
			})
			List getAllUser();
			
			@Insert("insert into userInfo (userName,password,note) values (#{userName},#{password},#{note} )" )
			int add(UserInfo user);
			
			//本例可以返回自增主键
			@Insert("insert into userInfo (userName,password,note) values (#{userName},#{password},#{note} )" )
			@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
			int addAndReturnKey(UserInfo user);
			
			@Delete("delete from userInfo where id =#{id}")
			int delUser(int id);
		
			@Update("update userInfo set userName=#{userName} ,password=#{password} , note =#{note} where id =#{id}" )
			int updateUser(UserInfo user);
			
			@Select("select * from userInfo where userName=#{a} and password=#{b} ")
			UserInfo login(@Param("a")String userName, @Param("b") String password);
		}

使用了注解方式,映射文件就可以不要 
        
 映射文件也可以和注解方式同时存在 

2、输入输出参数的类型

(1)输入参数类型 parameterType

	
					insert into userInfo (userName,password,note) values (#{userName},#{password},#{note} ) 
	

parameterType主要有以下几种

        1)简单类型

        2)pojo

        3)包装过的pojo

//例 
class School{
			Student stu;
			String address;	
 }

		insert into student (stuName ...  ) values (shcool.stu.studentName ...) 

//用school.stu取到Student的一个对象,然后接着.studentName就取到了stuName

        4)hashMap

UserMapper接口中

public interface UserMapper{
    UserInfo login(Map params);
}

映射文件中UserMapper.xml


    select * from userInfo where userName=#{key1} and password=#{key2}

测试文件中

public class Test{
    public static void main(String[] args){
        //以map的方式传参
        Map paramMap=new HashMap<>();
        paramMap.put("Key1","赵强");
        paramMap.put("key2","123456");

        //调用登录方法
        UserInfo user=userMapper.login(paramMap);
    }
}

用这样的方式传参,适合参数个数较多,而且比较散乱的情况

 另一种方式

附:也可以用 @Param 的方式对Mpper接口中的参数进行指定                           

   UserInfo login(@Param("userName") String userName,@Param("password") String password);    


  然后在配置文件中:                          

  
                                 select id, userName uname, password pwd ,note  from userInfo where id = #{id} 
                                

  3.mybatis中的动态SQL

        mysql中实现动态SQL的主要元素有

               if
              choose (when otherwise)
              trim
              where
              set
              foreach

1) if 典型情况就是在多条件查询下

               //例 接口中要加一个多条件查询的方法            

UserMapper中

List  getSearchWhereList(UserInfo user);

             映射文件中 (UserMapper.xml)                           


		select * from userinfo where 1=1
		
			
				and userName=#{userName}
			
			
				and age=#{age}
			
			
				and note=#{note}
			
		
	

3)   trim

prefix:前缀覆盖并增加其内容
                suffix:后缀覆盖并增加其内容
                prefixOverrides:前缀判断的条件
                suffixOverrides:后缀判断的条件
    
                    

    
			 ${value}
	
	接口:
						List getUserListByIdList(String sql);
	    
	        测试: 
            List userList=userMapper.getUserListByIdList("select * from userinfo where id in (1,3,5,7,9) " );
		

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/295714.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号