本文主要是介绍mybatis实现business的全表查询(三种方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现business的全表查询 select * from business
方法一:
1.定义父接口
package com.neusoft.Idao;import com.neusoft.pojo.Business;import java.util.List;public interface OpBusinessDao {public List<Business> findBusi();
}
2.定义子的实现
<?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.neusoft.Idao.OpBusinessDao"><select id="findBusi" resultType="com.neusoft.pojo.Business">select * from business</select></mapper>
3.配置映射文件
4.代码实现
import com.neusoft.Idao.OpBusinessDao;
import com.neusoft.pojo.Business;
import com.neusoft.service.OpBusiService;
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;
import java.util.List;public class TestBusi {public static void main(String[] args) {// 1.加载配置文件InputStream inputStream = null;try {inputStream =Resources.getResourceAsStream("mybatis.xml");
// 2.获取mybatis 数据库工厂类SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);// 3.获取数据库连接SqlSession sqlSession = ssf.openSession();// 面向接口的mapper调用OpBusinessDao op= sqlSession.getMapper(OpBusinessDao.class);//根据父接口类型加载子类对象List<Business> list = op.findBusi();for (Business bb: list) {System.out.println(bb.toString());}} catch (IOException e) {e.printStackTrace();}}
}
方法二(使用Select注解):
1.定义接口
package com.neusoft.Idao;import com.neusoft.pojo.Business;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface OpBusinessDao {@Select("select * from business")public List<Business> findBusi();
}
2.配置映射文件
3.测试实现
import com.neusoft.Idao.OpBusinessDao;
import com.neusoft.pojo.Business;
import com.neusoft.service.OpBusiService;
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;
import java.util.List;public class TestBusi {public static void main(String[] args) {// 1.加载配置文件InputStream inputStream = null;try {inputStream =Resources.getResourceAsStream("mybatis.xml");
// 2.获取mybatis 数据库工厂类SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);// 3.获取数据库连接SqlSession sqlSession = ssf.openSession();// 面向接口的mapper调用OpBusinessDao op= sqlSession.getMapper(OpBusinessDao.class);//根据父接口类型加载子类对象List<Business> list = op.findBusi();for (Business bb: list) {System.out.println(bb.toString());}} catch (IOException e) {e.printStackTrace();}}
}
方法三:
1.定义父接口
package com.neusoft.Idao;import com.neusoft.pojo.Business;import java.util.List;public interface OpBusinessDao {public List<Business> findBusi();
}
2.定义子的实现
<?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.neusoft.Idao.OpBusinessDao"><select id="findBusi" resultType="com.neusoft.pojo.Business">select * from business</select></mapper>
3.配置映射文件
4 .定义service层进行整理调用
package com.neusoft.service;import com.neusoft.Idao.OpBusinessDao;
import com.neusoft.pojo.Business;
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;
import java.util.List;public class OpBusiService {static SqlSessionFactory ssf = null;static {InputStream inputStream = null;try {
// 1.加载配置文件inputStream = Resources.getResourceAsStream("mybatis.xml");
// 2.获取mybatis 数据库工厂类ssf = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public List<Business> findBusiBiz(){SqlSession sqlSession = ssf.openSession();// 面向接口的mapper调用OpBusinessDao opBusinessDao = sqlSession.getMapper(OpBusinessDao.class);//根据父接口类型加载子类对象List<Business> list = opBusinessDao.findBusi();for (Business bb: list) {System.out.println(bb.toString());}return list;}
}
5.代码实现
import com.neusoft.pojo.Business;
import com.neusoft.service.OpBusiService;
import java.util.List;public class TestBusi {public static void main(String[] args) {OpBusiService opBusiService = new OpBusiService();List<Business> list = opBusiService.findBusiBiz();for (Business bb: list) {System.out.println(bb.toString());}}
}
这篇关于mybatis实现business的全表查询(三种方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!