一、使用接口查询数据流程
1、建立查询方法接口
package com.jd.area;
public interface IAreaDao {
String getType(String id);
}
2、建立查询类:
package com.jd.test;
import com.jd.area.IAreaDao;
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 java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
//加载全局配置文件
InputStream resources = Resources.getResourceAsStream("mybatis-config.xml");
//SqlSession对象表示一次数据库连接
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(resources);//工厂建立通过配置文件原料,建立SqlsessionFactory原料
sqlSession= ssf.openSession();//建立一个SqlSession对象
IAreaDao IAreaDao = sqlSession.getMapper(com.jd.area.IAreaDao.class);//使用接口的Class对象返回一个接口实现类对象
String type = IAreaDao.getType("1003");
System.out.println(type);
} catch (IOException e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
分步解释:
(1)首先要引入xml资源,即第一步,获取inputStream类型的resource对象,注意是getResourceAsStream
(2)通过使用工厂建立者 SQLSessionFactoryBuilder中的build方法,建立一个SQLSession的工厂 SQLSessionFactory
(3)通过工厂打开一个SQLSession对象
(4)通过SQLSession中的getMapper方法将接口引入,其自动建立一个实现类对象,类型为接口类型
通过调用接口中的方法,传入参数,得到值
area.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">
<!--namespace指定接口全名 -->
<mapper namespace="com.jd.area.IAreaDao">
<!-- ID指定接口中定义的方法名 -->
<select id="getType" resultType="java.lang.String">
select worker_type from worker where worker_id=#{id};
</select>
</mapper>
注意resultType中类型如果是引引用类型(如String)直接使用名称也可
**
二、工业级使用mybitas流程
**
0、建立对应Dao层接口以及对应的Bean
Bean:
package com.jd.vo;
public class Person {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
接口:
package com.jd.person;
import com.jd.vo.Person;
public interface IPerson {
Person getPersonById(String id);//注意这里返回的是Bean
}
1、建立对应Dao层的xml文件(放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.jd.person.IPerson">
<select id="getPersonById" resultType="com.jd.vo.Person">
select name,costType as type from custom where id=#{id};
</select>
</mapper>
注意点:namespace放接口名,id放接口中获取对象方法的名称,resultType为获取对象类型
2、在config文件中建立映射
<mappers>
<!--每一个mapper对应一个mapper文件 -->
<mapper resource="area.xml"></mapper>
<mapper resource="person.xml"></mapper>
</mappers>
3、进行查询
package com.jd.test;
import com.jd.area.IAreaDao;
import com.jd.person.IPerson;
import com.jd.vo.Person;
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 java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
//加载全局配置文件
// InputStream resources = Resources.getResourceAsStream("mybatis-config.xml");
// //SqlSession对象表示一次数据库连接
// SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(resources);//工厂建立通过配置文件原料,建立SqlsessionFactory原料
// sqlSession= ssf.openSession();//建立一个SqlSession对象
// IAreaDao IAreaDao = sqlSession.getMapper(com.jd.area.IAreaDao.class);//使用接口的Class对象返回一个接口实现类对象
// String type = IAreaDao.getType("1003");
// System.out.println(type);
InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(resource);
sqlSession=ssf.openSession();
IPerson Iperson = sqlSession.getMapper(com. jd.person.IPerson.class);
Person person = Iperson.getPersonById("1001");
System.out.println(person.getName());
} catch (IOException e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
三、特殊查询方法
1、模糊查询
启用if标签,注意在传进来参数之前要加上%
代码:
<select id="getStudentsByPage" resultType="com.zzu.student.vo.Student">
select * from student where 1=1
<if test="name!=null and name!=''">
and name like #{name}
</if>
<if test="mobile!=null and mobile!=''">
and mobile like #{mobile}
</if>
limit #{index},#{pageSize}
</select>
2、使用where标签替换where 1=1
<select id="getStudentsByPage" resultType="com.zzu.student.vo.Student">
select * from student
<where>
<if test="name!=null and name!=''">
and name like #{name}
</if>
<if test="mobile!=null and mobile!=''">
and mobile like #{mobile}
</if>
</where>
limit #{index},#{pageSize}
</select>