比如根据id或者字段条件查询获取表中的某一个字段值
user sel(int id); //根据id查询
sql映射文件
<select id="sel" resulttype="java.lang.string"> //注意这个写了类全名 select username from user_test where id = #{id} </select>
如果需要简写的话 需要定义resulttype的别名
java 的基本类型不需要别名的:
别名 | 映射的类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_boolean | boolean |
integer | integer |
string | string |
date | date |
boolean | boolean |
<select id="sel" resulttype="com.tx.springboottestdemo.entity.user"> select * from user_test where id = #{id} </select> <typealiases> <!-- 针对单个别名定义 type:类型的路径 alias:别名 --> <typealias type="cn.itcast.mybatis.po.user" alias="user"/>
有时候我们需要模糊查询或者是全表查询,返回的数据是多条的, 那么可以把多条数据保存到list里面的。
mapper 接口
list<user> getusers();
sql映射文件:
<select id="getusers" resulttype="com.tx.entity.user"> select * from user </select>
这里需要注意的是返回是list类型 但是resulttype依然是javabean, 有些人会困惑这里怎么不是集合类型呢?其实透过现象看本质, 还是javabean。
当我们在查询的时候返回一条数据的时候,我们可以把{字段名,字段值}封装成map结构。
map<string, object> finduserbyname(integer id);
sql 映射文件:
<select id="finduserbyname" resulttype="string"> select username from user where id=#{id}; </select>
1.其实可以看成是多个参数的
public list<user> finduser( string name1, string name2);
对应的sql映射文件:
<select id="finduser3" resulttype="com.tx.springboottestdemo.entity.user"> select * from user_test where username = #{0} and realname = #{1} </select>
其中里面#{0}, #{1}默认是按照mybatis传值的顺序位置索引的 但是在springboot2.1(集成mybatis框架)里面会报错,我看网上说是可以的 我尝试很多好像不行,下面贴出error:
~~org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.binding.bindingexception: parameter '0' not found. available parameters are [arg1, arg0, param1, param2]~~
2.可以看做是加了注解
public list<user> finduser( @param("name1") string name1, @param("name2") string name2);
对应的sql文件:
<select id="finduser" resulttype="com.tx.springboottestdemo.entity.user"> select * from user_test where username = #{name1} and realname = #{name2} </select>
3.可以把参数封装到map里面
有些时候我们的业务数据查询没有定义对应的pojo,就进行参数的封装操作。
public list<user> finduser1(map<string, object> map);
对应的sql文件:
<select id="finduser1" parametertype="java.util.map" resulttype="com.tx.springboottestdemo.entity.user"> select * from user_test where username = #{username} and realname = #{realname} </select>
resulttype是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,list类型,map类型等。现总结一下再解释
resulttype:
1、基本类型:resulttype=基本类型
2、list类型:resulttype=list中元素的类型
3、map类型:
单条记录:resulttype =map
多条记录:resulttype =map中value的类型
对于对象类型resulttype直接写对象的全类名就可以了
实例:
hotelmapper接口
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.hotel; public interface hotelmapper { //返回值类型为hotel public hotel gethotel(integer i); }
hotelmapper.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.pjf.mybatis.dao.hotelmapper"> <!--resulttype直接写对象的全类名 --> <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel"> select * from hotel where id=#{id} </select> </mapper>
测试类:
package com.pjf.mybatis; import java.io.ioexception; import java.io.inputstream;import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import com.pjf.mybatis.dao.hotelmapper; import com.pjf.mybatis.po.hotel; public class testhotel { public sqlsessionfactory sqlsessionfactory() throws ioexception { // mybatis的配置文件 string resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader() inputstream is = resources.getresourceasstream(resource); // 构建sqlsession的工厂 sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is); return sessionfactory; } // 查 @test public void gethotel() throws ioexception { sqlsessionfactory sessionfactory = sqlsessionfactory(); sqlsession session = sessionfactory.opensession(); hotelmapper hotelmapper = session.getmapper(hotelmapper.class); system.out.println(hotelmapper.getclass()); //直接返回hotel对象,打印出来 hotel hotel = hotelmapper.gethotel(1001); system.out.println(hotel); session.close(); } }
返回值为list类型,resulttype为list中对象的类型,如list<hotel>,resulttype为hotel
实例:
hotelmapper接口
package com.pjf.mybatis.dao; import java.util.list; import com.pjf.mybatis.po.hotel; public interface hotelmapper { // 返回值为list public list<hotel> gethotel(integer i); }
hotelmapper.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.pjf.mybatis.dao.hotelmapper"> <!-- 返回值为list,resulttype为list中元素的全类名 --> <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel"> select * from hotel where price>#{price} </select> </mapper>
测试类:
package com.pjf.mybatis; import java.io.ioexception; import java.io.inputstream; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import com.pjf.mybatis.dao.hotelmapper; import com.pjf.mybatis.po.hotel; public class testhotel { public sqlsessionfactory sqlsessionfactory() throws ioexception { // mybatis的配置文件 string resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader() inputstream is = resources.getresourceasstream(resource); // 构建sqlsession的工厂 sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is); return sessionfactory; } // 查 @test public void gethotel() throws ioexception { sqlsessionfactory sessionfactory = sqlsessionfactory(); sqlsession session = sessionfactory.opensession(); hotelmapper hotelmapper = session.getmapper(hotelmapper.class); system.out.println(hotelmapper.getclass()); // 返回值为list list<hotel> list = hotelmapper.gethotel(1000); for (hotel hotel : list) { system.out.println(hotel); } session.close(); } }
a、返回单条记录的map,key为属性,值为属性值。resulttype为map
hotelmapper接口
package com.pjf.mybatis.dao; import java.util.map; import com.pjf.mybatis.po.hotel; public interface hotelmapper { // 返回值为map,key为属性名,value为属性值 public map<string, object> gethotel(integer i); }
hotelmapper.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.pjf.mybatis.dao.hotelmapper"> <!-- 返回值为map,resulttype为map --> <select id="gethotel" resulttype="map"> select * from hotel where id=#{id} </select> </mapper>
测试类:返回id=1001的酒店
package com.pjf.mybatis; import java.io.ioexception; import java.io.inputstream; import java.util.map; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import com.pjf.mybatis.dao.hotelmapper; import com.pjf.mybatis.po.hotel; public class testhotel { public sqlsessionfactory sqlsessionfactory() throws ioexception { // mybatis的配置文件 string resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader() inputstream is = resources.getresourceasstream(resource); // 构建sqlsession的工厂 sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is); return sessionfactory; } // 查 @test public void gethotel() throws ioexception { sqlsessionfactory sessionfactory = sqlsessionfactory(); sqlsession session = sessionfactory.opensession(); hotelmapper hotelmapper = session.getmapper(hotelmapper.class); system.out.println(hotelmapper.getclass()); // 返回值为map map<string, object> map = hotelmapper.gethotel(1001); system.out.println(map); session.close(); } }
b、返回多条记录的map,key为任意一属性,值为对象类型。如map<string,hotel>,resulttype为hotel
返回多条记录的map时,key为任意一属性,值为对象类型,不过key需要通过@mapkey("hotelname")指定对象中一个属性名为key
实例:
hotelmapper接口
package com.pjf.mybatis.dao; import java.util.map; import org.apache.ibatis.annotations.mapkey; import com.pjf.mybatis.po.hotel; public interface hotelmapper { // 返回值为map,key需要通过@mapkey("属性名")来指定javabean中的一个属性名为key,value为对象 @mapkey("hotelname") public map<string, hotel> gethotel(integer i); }
hotelmapper.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.pjf.mybatis.dao.hotelmapper"> <!-- 返回值为map,resulttype为对象的全类名 --> <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel"> select * from hotel where price>#{price} </select> </mapper>
测试类:返回价格>1000以上的酒店
package com.pjf.mybatis; import java.io.ioexception; import java.io.inputstream; import java.util.map; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import com.pjf.mybatis.dao.hotelmapper; import com.pjf.mybatis.po.hotel; public class testhotel { public sqlsessionfactory sqlsessionfactory() throws ioexception { // mybatis的配置文件 string resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader() inputstream is = resources.getresourceasstream(resource); // 构建sqlsession的工厂 sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is); return sessionfactory; } // 查 @test public void gethotel() throws ioexception { sqlsessionfactory sessionfactory = sqlsessionfactory(); sqlsession session = sessionfactory.opensession(); hotelmapper hotelmapper = session.getmapper(hotelmapper.class); system.out.println(hotelmapper.getclass()); // 返回值为map map<string, hotel> map = hotelmapper.gethotel(1000); system.out.println(map); session.close(); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。