实现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());
}
}
}