您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MyBatis入门(三)---多个参数

2025/1/16 12:57:37发布15次查看
一、建立表
1.1、建立表,并插入数据
/*sqlyog enterprise v12.09 (64 bit)mysql - 5.6.27-log : database - mybatis
**********************************************************************//*!40101 set names utf8 */;/*!40101 set sql_mode=''*/;/*!40014 set @old_unique_checks=@@unique_checks, unique_checks=0 */;/*!40014 set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0 */;/*!40101 set @old_sql_mode=@@sql_mode, sql_mode='no_auto_value_on_zero' */;/*!40111 set @old_sql_notes=@@sql_notes, sql_notes=0 */;create database /*!32312 if not exists*/`mybatis` /*!40100 default character set utf8 */;use `mybatis`;/*table structure for table `author` */drop table if exists `author`;create table `author` (
 `author_id` int(11) unsigned not null auto_increment comment '作者id主键',
 `author_username` varchar(30) not null comment '作者用户名',
 `author_password` varchar(32) not null comment '作者密码',
 `author_email` varchar(50) not null comment '作者邮箱',
 `author_bio` varchar(1000) default '这家伙很赖,什么也没留下' comment '作者简介',
 `register_time` datetime default current_timestamp comment '注册时间',  primary key (`author_id`)
) engine=innodb auto_increment=10 default charset=utf8;/*data for the table `author` */insert  into `author`(`author_id`,`author_username`,`author_password`,`author_email`,`author_bio`,`register_time`)
values (1,'张三','123456','123@qq.com','张三是个新手,刚开始注册','2015-10-29 10:23:59'),(2,'李四','123asf','lisi@163.com','魂牵梦萦 ','2015-10-29 10:24:29'),(3,'王五','dfsd342','ww@sina.com','康熙王朝','2015-10-29 10:25:23'),(4,'赵六','123098sdfa','zhaoliu@qq.com','花午骨','2015-10-29 10:26:09'),(5,'钱七','zxasqw','qianqi@qq.com','这家伙很赖,什么也没留下','2015-10-29 10:27:04'),(6,'张三丰','123456','zhangsf@qq.com','这家伙很赖,什么也没留下','2015-10-29 11:48:00'),(7,'金庸','qwertyuiop','wuji@163.com','这家伙很赖,什么也没留下','2015-10-29 11:48:24'),(8,'知道了','456789','456789@qq.com','哈哈哈哈哈雅虎','2015-10-29 14:03:27'),(9,'不知道','1234567890','123456@qq.com','哈哈哈哈哈雅虎','2015-10-29 14:01:16');/*!40101 set sql_mode=@old_sql_mode */;/*!40014 set foreign_key_checks=@old_foreign_key_checks */;/*!40014 set unique_checks=@old_unique_checks */;/*!40111 set sql_notes=@old_sql_notes */;
二、创建项目
2.1、创建项目
2.2、创建pojo类
package com.pb.mybatis.po;import java.util.date;/**
*
* @title: author.java
* @package com.pb.mybatis.po
* @classname author
* @description: todo(blog作者类)
* @author 刘楠
* @date 2015-10-29 上午9:27:53
* @version v1.0 */public class author {    //作者id
   private int authorid;    
   //作者用户名
   private string authorusername;    
   //作者密码
   private string authorpassword;    
   //作者邮箱
   private string authoremail;    
   //作者介绍
   private int authorbio;    
   //注册时间
   private date registertime;    /**
    * @return the authorid     */
   public int getauthorid() {        return authorid;
   }    /**
    * @param authorid the authorid to set     */
   public void setauthorid(int authorid) {        this.authorid = authorid;
   }    /**
    * @return the authorusername     */
   public string getauthorusername() {        return authorusername;
   }    /**
    * @param authorusername the authorusername to set     */
   public void setauthorusername(string authorusername) {        this.authorusername = authorusername;
   }    /**
    * @return the authorpassword     */
   public string getauthorpassword() {        return authorpassword;
   }    /**
    * @param authorpassword the authorpassword to set     */
   public void setauthorpassword(string authorpassword) {        this.authorpassword = authorpassword;
   }    /**
    * @return the authoremail     */
   public string getauthoremail() {        return authoremail;
   }    /**
    * @param authoremail the authoremail to set     */
   public void setauthoremail(string authoremail) {        this.authoremail = authoremail;
   }    /**
    * @return the authorbio     */
   public int getauthorbio() {        return authorbio;
   }    /**
    * @param authorbio the authorbio to set     */
   public void setauthorbio(int authorbio) {        this.authorbio = authorbio;
   }    /**
    * @return the registertime     */
   public date getregistertime() {        return registertime;
   }    /**
    * @param registertime the registertime to set     */
   public void setregistertime(date registertime) {        this.registertime = registertime;
   }    /** (non javadoc)
* <p>title: tostring</p>
* <p>description:重写tostring方法 </p>
* @return
* @see java.lang.object#tostring()     */
   @override    public string tostring() {        return author [authorid= + authorid + , authorusername=
               + authorusername + , authorpassword= + authorpassword                + , authoremail= + authoremail + , authorbio= + authorbio                + , registertime= + registertime + ];
   }
}
2.3、创建configruation
<?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>
<properties resource="db.properties" />
<typealiases>
<!--使用默认别名 -->
<package name="com.pb.mybatis.po"/>
</typealiases>
<environments default="development">
<environment id="development">
   <transactionmanager type="jdbc"/>
   <datasource type="pooled">
       <property name="driver" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="username" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
   </datasource>
</environment>
</environments>
<mappers>
<!-- 加载映射 -->
<package name="com.pb.mybatis.mapper"/>
</mappers>
</configuration>
2.3、创建mapper接口
public interface authormapper {    
   /**
    *
    * @title: findbyid
* @description: todo(根据查找一个用户)
* @param id
    * @return author     */
   public author findauthorbyid(int authorid);
}
2.4、创建mapper.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.pb.mybatis.mapper.authormapper"><!--使用resultmap映射 type使用别名,--><resultmap type="author" id="authorresultmap"><!--主键 --><id property="authorid" column="author_id"/><!--普通属性与表中的字段对应 --><result property="authorusername" column="author_username"/><result property="authorpassword" column="author_password"/><result property="authoremail" column="author_email"/><result property="authorbio" column="author_bio"/><result property="registertime" column="register_time"/></resultmap><!--根据查找一个用户 --><select id="findauthorbyid" parametertype="int" resultmap="authorresultmap">select * from author
where author_id=#{authorid}</select></mapper>
三、传入多个id,进行查找使用list
3.1、更改mapper接口
/**
    *
    * @title: findauthors
* @description: todo(根据多个id进行查找)
* @param idlists
    * @return list<author>     */
   public list<author> findauthors(list<integer> idlists);
3.2、更改mapper.xml
<!--根据多个id查找 --><select id="findauthors" resultmap="authorresultmap">select * from author
where author_id in<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">#{item}</foreach>
<!-- collection:传入参数的名称 index:索引: item:collection的别名 --></select>
3.3、测试
@test    public void testfindauthors() {        //获取会话
       sqlsession sqlsession=sqlsessionfactory.opensession();        //mapper接口
       authormapper authormapper=sqlsession.getmapper(authormapper.class);
       list<integer> list=new arraylist<integer>();
list.add(1);
       list.add(3);
       list.add(4);
       list.add(6);
       list.add(7);        //调用方法
       list<author> authors=authormapper.findauthors(list);
       system.out.println(authors);        //关闭会话        sqlsession.close();
   }
四、使用map做为参数
4.1、在mapper接口中增加相应方法
/**
    *
    * @title: findauthorsbymap
* @description: todo(使用map做为参数)
* @param map
    * @return list<author>     */
   public list<author> findauthorsbymap(map<string, object> map);
4.2、更改mapper.xml
<!--使用map查找 --><select id="findauthorsbymap" resultmap="authorresultmap">select * from author<!-- 参数使用map的key-->where author_username like %#{username}%
or author_bio like%#{bio}%</select>
4.3、测试
@test    public void testfindauthorsbymap() {        //获取会话
               sqlsession sqlsession=sqlsessionfactory.opensession();                //mapper接口
               authormapper authormapper=sqlsession.getmapper(authormapper.class);
               map<string, object> map=new hashmap<string, object>();
               map.put(username, 张);
               map.put(bio, 哈);                
               //调用方法
               list<author> authors=authormapper.findauthorsbymap(map);
               system.out.println(authors);                //关闭会话                sqlsession.close();                for(author a:authors){
                   system.out.println(a.tostring());
               }
   }
五、直接使用多个参数
5.1、mapper接口
/**
    *
    * @title: findauthorsbyparams
* @description: todo(使用多个参数
* @param id
    * @param username
    * @return list<author>     */
   public list<author> findauthorsbyparams(int authorid,string authorusername);
5.2、mapper.xml
<!--直接使用多个参数 --><select id="findauthorsbyparams" resultmap="authorresultmap">select * from author
where author_id=#{0}
or author_username like %#{1}%<!-- 其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。 --></select>
5.3、测试
@test    public void testfindauthorsbyparams() {        //获取会话
               sqlsession sqlsession=sqlsessionfactory.opensession();                //mapper接口
               authormapper authormapper=sqlsession.getmapper(authormapper.class);
//调用方法
               list<author> authors=authormapper.findauthorsbyparams(6,张);
               system.out.println(authors);                //关闭会话                sqlsession.close();                for(author a:authors){
                   system.out.println(a.tostring());
               }
   }
六、直接使用多个参数注解写法
6.1、mapper接口
public list<author> findauthorsbyparams(@param(id) int authorid,@param(username)string authorusername);
6.2、mapper.xml
<!--使用注解的方式使用多个参数 --><select id="findauthorsbyparams" resultmap="authorresultmap">select * from author
where author_id=#{id}
or author_username like %#{username}%<!-- 使用注解的方式。,直接使用param中的参数即可 --></select>
以上就是mybatis入门(三)---多个参数的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product