欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis配置SQL语句的方式

程序员文章站 2022-05-23 13:11:11
...
配置MAPPER的方式有四种,不过现在只用了两种。
一种是通过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.lonton.mybatis.filemapper">
	
	<select id="findPage" parameterType="map" resultType="fileinfo">
		select * from file_info  limit #{begin},#{size} 
	</select>
	<select id="findPageOrder" parameterType="map" resultType="fileinfo">
		select * from file_info  order by #{column} #{order} limit #{begin},#{size} 
	</select>
	
	<select id="findAll" resultType="fileinfo">
		select * from file_info 
	</select>
	
	<select id="search" parameterType="String" resultType="fileinfo">
		select * from file_info  where name like #{name}
	</select>
	
	<select id="selectCount" resultType="int">
		select count(*)  from file_info 
	</select>

</mapper>


通过接口+注解:
package com.lonton.testMyBatis.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


import com.lonton.testMyBatis.pojo.Fileinfo;

public interface FileinfoDao {

	@Select("select * from file_info")
	public List<Fileinfo> findAll();
	
	@Select("select * from file_info limit #{begin},#{size}")
	public List<Fileinfo> findPage(@Param("map")int page,int pageSize);//注解参数的意思是传给MyBatis API的参数
	
	
	public List<Fileinfo> findPageOrder(int page,int pageSize,String column,String order);
	
	@Select("select count(*) from file_info")
	public int getCount();
	
	@Select("select * from file_info where name like #{name}")
	public List<Fileinfo> search(@Param("name") String name);
	
}



最后的实现类是这样的:

package com.lonton.testMyBatis.daoImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lonton.testMyBatis.dao.BaseDao;
import com.lonton.testMyBatis.dao.FileinfoDao;
import com.lonton.testMyBatis.pojo.Fileinfo;

public class FileinfoDaoImpl extends BaseDao implements FileinfoDao{

	@Override
	public List<Fileinfo> findAll() {
		return sessionFactory.openSession().selectList("com.lonton.mybatis.filemapper.findAll");
	}

	@Override
	public List<Fileinfo> findPage(int page, int pageSize) {
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("begin", (page-1)*pageSize);
		map.put("size", pageSize);
		//XML
		//return sessionFactory.openSession().selectList("com.lonton.mybatis.filemapper.findPage", map);
		//interface
		return sessionFactory.openSession().selectList("com.lonton.testMyBatis.dao.FileinfoDao.findPage",map);
	}

	@Override
	public int getCount() {
		/*
		 *  XML MAPPER 
		 *  路径是 namespace+id
		 */
		//return sessionFactory.openSession().selectOne("com.lonton.mybatis.filemapper.selectCount");
		
		/*
		 * interface MAPPER
		 * 路径应该是:包名+类名+方法
		 */
		return sessionFactory.openSession().selectOne("com.lonton.testMyBatis.dao.FileinfoDao.getCount");
	}

	@Override
	public List<Fileinfo> search(String name) {
		return sessionFactory.openSession().selectList("com.lonton.mybatis.filemapper.search", "%"+name+"%");
	}

	@Override
	public List<Fileinfo> findPageOrder(int page, int pageSize, String column,
			String order) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("begin", (page-1)*pageSize);
		map.put("size", pageSize);
		map.put("column", column);
		map.put("order", order);
		return sessionFactory.openSession().selectList("com.lonton.mybatis.filemapper.findPageOrder", map);
	
	}

}

相关标签: mybatis 配置