mybatis中example用法

mybatis中 example用法
        查询:
           排序:example.orderBy("id")
              selectByPrimaryKey(100):主键查询 :where id = 100
              selectByExample()和selectByExampleWithBLOGs() 示例:
                                                           UserExample example = new UserExample();
                                                           Criteria criteria = example.createCriteria();
                                                           criteria.andUsernameEqualTo("wyw");
                                                           criteria.andUsernameIsNull();
                                                           example.setOrderByClause("username asc,email desc");
                                                           List<?>list = XxxMapper.selectByExample(example);
                                                           //相当于:select * from user where username = 'wyw' and  username is null order by username asc,email desc
        插入:
             示例: 
                    User user = new User();
                    user.setId("dsfgsdfgdsfgds");
                    user.setUsername("admin");
                    user.setPassword("admin")
                    user.setEmail("wyw@163.com");
                    XxxMapper.insert(user);
                    //相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');
        更新:
             updateByPrimanryKey():
                 示例:
                  1) User user =new User();
                     user.setId("dsfgsdfgdsfgds");
                     user.setUsername("wyw");
                     user.setPassword("wyw");
                     user.setEmail("wyw@163.com");
                     XxxMapper.updateByPrimaryKey(user);
                     //相当于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'	
				
				  2)updateByExample() 和 updateByExampleSelective()
                    UserExample example = new UserExample();
                    Criteria criteria = example.createCriteria();
                    criteria.andUsernameEqualTo("admin");
                    User user = new User();
                    user.setPassword("wyw");
                    XxxMapper.updateByPrimaryKeySelective(user,example);
                    //相当于:update user set password='wyw' where username='admin 
					updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段
					
	    删除:
		     deleteByPrimaryKey():根据key删除
			     示例:
				    XxxMapper.deleteByPrimaryKey(1);  //相当于:delete from user where id=1
		     deleteByExample():根据字段删除
			     示例:
			        UserExample example = new UserExample();
                    Criteria criteria = example.createCriteria();
                    criteria.andUsernameEqualTo("admin");
                    XxxMapper.deleteByExample(example);
                    //相当于:delete from user where username='admin' 
					
		查询数据量:
		     countByExample()
			    示例:
				  UserExample example = new UserExample();
                  Criteria criteria = example.createCriteria();
                  criteria.andUsernameEqualTo("wyw");
                  int count = XxxMapper.countByExample(example);
                  //相当于:select count(*) from user where username='wyw'
				  
				  
				  
  附:
       mapper接口中的函数及方法表:
                          方法名 	                                    
                             int countByExample(UserExample example) 	按条件计数
                             int deleteByPrimaryKey(Integer id) 	按主键删除
                             int deleteByExample(UserExample example) 	按条件查询
                             String/Integer insert(User record) 	插入数据(返回值为ID)
                             User selectByPrimaryKey(Integer id) 	按主键查询
                             ListselectByExample(UserExample example) 	按条件查询
                             ListselectByExampleWithBLOGs(UserExample example) 	按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
                             int updateByPrimaryKey(User record) 	按主键更新
                             int updateByPrimaryKeySelective(User record) 	按主键更新值不为null的字段
                             int updateByExample(User record, UserExample example) 	按条件更新
                             int updateByExampleSelective(User record, UserExample example) 	按条件更新值不为null的字段
    
	    example实例方法
		                  方法名
     						 example.setOrderByClause(“字段名 ASC”); 	添加升序排列条件,DESC为降序
                             example.setDistinct(false) 	去除重复,boolean型,true为选择不重复的记录。
                             example.and(Criteria criteria) 	为example添加criteria查询条件,关系为与
                             example.or(Criteria criteria) 	为example添加criteria查询条件,关系为或
                             criteria.andXxxIsNull 	添加字段xxx为null的条件 
                             criteria.andXxxIsNotNull 	添加字段xxx不为null的条件
                             criteria.andXxxEqualTo(value) 	添加xxx字段等于value条件
                             criteria.andXxxNotEqualTo(value) 	添加xxx字段不等于value条件
                             criteria.andXxxGreaterThan(value) 	添加xxx字段大于value条件
                             criteria.andXxxGreaterThanOrEqualTo(value) 	添加xxx字段大于等于value条件
                             criteria.andXxxLessThan(value) 	添加xxx字段小于value条件
                             criteria.andXxxLessThanOrEqualTo(value) 	添加xxx字段小于等于value条件
                             criteria.andXxxIn(List<?>) 	添加xxx字段值在List<?>条件
                             criteria.andXxxNotIn(List<?>) 	添加xxx字段值不在List<?>条件
                             criteria.andXxxLike(“%”+value+”%”) 	添加xxx字段值为value的模糊查询条件
                             criteria.andXxxNotLike(“%”+value+”%”) 	添加xxx字段值不为value的模糊查询条件
                             criteria.andXxxBetween(value1,value2) 	添加xxx字段值在value1和value2之间条件
                             criteria.andXxxNotBetween(value1,value2) 	添加xxx字段值不在value1和value2之间条件 


					
					
					
					
					
					
					
					
             
					 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in action之九mybatis 代码生成工具的使用 mybatis SqlSessionDaoSupport的使用附代码下载 转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看文的:http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html) 写在这个系列前面的话: 以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活。性能也比hibernate好。而且也比较轻量级,因为当时在项目,没来的及做很很多笔记。后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目很有可能采用这个ORM工具。所以在此重新温习了一下 mybatis, 因此就有了这个系列的 mybatis 教程. 什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点: 1. 从配置文件(通常是XML配置文件)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。 mybatis实战教程(mybatis in action)之一:开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包。这些软件工具均可以到各自的官方网站上下载。 首先建立一个名字为 MyBaits 的 dynamic web project 1. 现阶段,你可以直接建立java 工程,但一般都是开发web项目,这个系列教程最后也是web的,所以一开始就建立web工程。 2. 将 mybatis-3.2.0-SNAPSHOT.jar,mysql-connector-java-5.1.22-bin.jar 拷贝到 web工程的lib目录. 3. 创建mysql 测试数据库和用户表,注意,这里采用的是 utf-8 编码 创建用户表,并插入一条测试数据 程序代码 程序代码 Create TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong'
入门 安装 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 即可。 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency>从 XML 构建 SqlSessionFactory 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。 从 XML 文件构建 SqlSessionFactory 的实例非常简单,建议使用类路径下的资源文件进行配置。但是也可以使用任意的输入流(InputStream)实例,包括字符串形式的文件路径或者 file:// 的 URL 形式的文件路径来配置。MyBatis 包含一个名叫 Resources 的工具类,它包含一些实用方法,可使从 classpath 或其他位置加载资源文件更加容易。 String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);XML 配置文件(configuration XML)包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。XML 配置文件的详细内容后面再探讨,这里先给出一个简单的示例: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>当然,还有很多可以在XML 文件进行配置,上面的示例指出的则是最关键的部分。要注意 XML 头部的声明,用来验证 XML 文档正确性。environment 元素体包含了事务管理和连接池的配置。mappers 元素则是包含一组 mapper 映射器(这些 mapper 的 XML 文件包含了 SQL 代码和映射定义信息)。 不使用 XML 构建 SqlSessionFactory 如果你更愿意直接从 Java 程序而不是 XML 文件创建 configuration,或者创建你自己的 configuration 构建器,MyBatis 也提供了完整的配置类,提供所有和 XML 文件相同功能的配置项。 .....................................

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值