您的位置:首页 > 编程语言 > Java开发

Spring框架(九)MyBatis分页实现. 关联表查询和映射. Spring+MyBatis应用

2018-12-18 09:15 691 查看
版权声明:cxyo.cn https://blog.csdn.net/weixin_44075537/article/details/85037162

MyBatis

目录
分页实现
1. 使用分页SQL实现(通用方法)
2. 使用MyBatis分页器RowBounds
3. 使用PageHelper组件扩展MyBatis分页(推荐)
关联表查询和映射
单行关联查询< association>/@One
多行关联查询< collection>/@Many
Spring+MyBatis应用
1. 引入spring、mybatis开发环境
2. 编写MyBatis文件
3. Spring配置
4. 创建Spring容器,从容器获取Mapper对象操作

分页实现

1.使用分页SQL实现(通用方法)

@Select("select * from (select t.*,rownum r from (select * from dept order by deptno) t) where r>=#{begin} and r<=#{end}") public List<Dept> findPage(@Param("begin")int begin,@Param("end")int end);

2.使用MyBatis分页器RowBounds

@Select("select * from dept order by deptno") public List<Dept> findAll();  SqlSession session = factory.openSession(); RowBounds bounds = new RowBounds(3,3);//从0开始抓取3条 List<Dept> list = session.selectList(     "cn.xdl.dao.DeptMapper.findAll",null,bounds);

3.使用PageHelper组件扩展MyBatis分页(推荐)
引入pagehelper工具包

在sqlmap-config.xml配置

<plugins>     <plugin interceptor="com.github.pagehelper.PageHelper">     </plugin> </plugins>

在mapper映射器对象方法前调用

SqlSession session = factory.openSession(); DeptMapper deptDao = session.getMapper(DeptMapper.class); //设置分页处理 Page page = PageHelper.startPage(2, 3);//显示第1页,一页3条 List<Dept> list = deptDao.findAll();

关联表查询和映射
单行关联查询< association>/@One

1.使用多表查询SQL一次性加载数据(性能好、复杂)

@Select({     "select ",     "   e.empno,e.ename,e.deptno,",     "   d.dname,d.loc",     "from emp e ",      "   join dept d on(e.deptno=d.deptno)" }) @Results({     @Result(column="empno",property="empno"),     @Result(column="ename",property="ename"),     @Result(column="deptno",property="deptno"),     @Result(column="deptno",property="dept.deptno"),     @Result(column="dname",property="dept.dname"),     @Result(column="loc",property="dept.loc") }) public List<Emp> find1();

2.使用多个单表查询SQL多次执行加载数据(性能低、简单)

@Select("select * from emp order by empno") @Results({     @Result(column="empno",property="empno"),     @Result(column="ename",property="ename"),     @Result(column="deptno",property="deptno"),     @Result(property="dept",column="deptno",         one=@One(select="cn.xdl.dao.DeptMapper.findById")) }) public List<Emp> find2();

多行关联查询< collection>/@Many

1.使用多个单表查询SQL多次执行加载

@Select("select * from dept where deptno=#{no}") @Results({     @Result(column="deptno",property="deptno"),     @Result(column="dname",property="dname"),     @Result(column="loc",property="loc"),     @Result(property="emps",javaType=List.class,column="deptno",         many=@Many(select="cn.xdl.dao.EmpMapper.findByDeptNo")) }) public Dept findById1(@Param("no")int deptno);

2.使用多表查询SQL一次性加载数据
需要使用XML配置写法完成。

Spring+MyBatis应用

1.引入spring、mybatis开发环境
spring ioc、aop、dao
dbcp连接池、ojdbc驱动包
mybatis工具包
mybatis-spring整合包
sqlmap-config.xml
applicationContext.xml

2.编写MyBatis文件
实体类
SQL定义
Mapper接口

3.Spring配置

dbcp连接池配置

<!-- dbcp连接池 --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">     <property name="username" value="SCOTT"></property>     <property name="password" value="TIGER"></property>     <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"></property>     <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> </bean>

SqlSessionFactoryBean

<!-- 在spring创建SqlSessionFactory对象,提供SqlSession --> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">     <property name="dataSource" ref="dbcp"></property>     <property name="configLocation" value="classpath:sqlmap-config.xml"></property> </bean>

MapperScannerConfigurer

<!-- 在spring创建Mapper映射器对象,id以接口名首字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     <property name="sqlSessionFactory" ref="factory"></property>     <property name="basePackage" value="cn.xdl.dao"></property> </bean>

4.创建Spring容器,从容器获取Mapper对象操作

@Test public void test1(){     String conf = "applicationContext.xml";     ApplicationContext ac =          new ClassPathXmlApplicationContext(conf);     DeptMapper deptDao =          ac.getBean("deptMapper",DeptMapper.class);     List<Dept> list = deptDao.findAll();     for(Dept dept:list){         System.out.println(dept.getDeptno()+" "+dept.getDname());     } }
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: