今天給大家介紹一位老朋友
當你第一次接觸Java開發的時候,這個老朋友就和你形影不離,當你要進行ORM的時候,單表的增刪改查,這位老朋友給了你極大的幫助,不知道你想到他了嗎?對,這就是通用mapper,這也是對于這位老朋友最簡單的介紹
如果你是新來做客的程序猿,我給你詳細的介紹一下它;你要是老牌程序員,我們來重新認識一下
代碼結構

庫表

配置文件
在applicationContext會話工廠里配置通用mapper插件。
<!--配置SqlSessionFactory,通過Spring來管理會話工廠--><bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--配置數據源:因為要使用SqlSession操作數據庫--><property name="dataSource" ref="dataSource"></property><!--加載mybatis的全局配置文件--><!--<property name="configLocation" value="classpath:mybatis.xml"></property>--><!--Spring起別名--><property name="typeAliasesPackage" value="com.me.pojo"></property><!-- 通用mapper插件的配置 --><property name="plugins"><array><!--pagehelper分頁配置。 --><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><value>helperDialect=mysqloffsetAsPageNum=true<!-- 防止出現小于第一頁,大于最后一頁的異常情況出現。 -->reasonable=true</value></property></bean><bean class="com.github.abel533.mapperhelper.MapperInterceptor"><property name="properties"><value><!-- 主鍵自增回寫方法,默認值MYSQL -->IDENTITY=MYSQLmappers=com.github.abel533.mapper.Mapper</value></property></bean></array></property></bean>
UserInfoMapper.java
不用配置pojo類的接口,mapper文件也極大簡化了。只需繼承Mapper(applicationContext.xml里配置的)就可以。Mapper里封裝了很多對單表操作的方法。
import com.github.abel533.mapper.Mapper;
import com.me.pojo.UserInfo;public interface UserInfoMapper extends Mapper<UserInfo> {}
UserInfoServiceImpl.java
@Service
public class UserInfoServiceImpl implements UserInfoService {@Autowiredprivate UserInfoMapper userInfoMapper;@Overridepublic List<UserInfo> select(UserInfo userInfo) {return userInfoMapper.select(userInfo);}
}
測試類
@Autowiredprivate UserInfoService userInfoService;@Testpublic void test(){UserInfo user=new UserInfo();user.setSex("男");List<UserInfo> userInfos=userInfoService.select(user);System.err.println(userInfos.toString());}
但是,這一些是Mapper的基礎操作,在Mapper中,有一個很重要的概念,動態代理實現,這里也給大家展示一下
優點
開發者只需聲明mapper接口(也就是dao接口),無需聲明接口的實現類,而由mybatis框架創建接口的代理對象,就和實現類類似。
規范
- 映射文件mapper.xml和接口名稱一致
- 映射文件的namespace是接口的全路徑
- 映射文件的sql statement的id是接口的方法名稱
- 映射文件的輸入參數類型和接口方法的參數類型一致
- 映射文件的輸出結果類型和接口方法的返回類型一致
mybatis.xml加載映射文件
<mappers><!--<mapper resource="UsersMapper.xml"></mapper>--><!--批量加載映射文件--><package name="com.me.mapper"/></mappers>
接口:UsersMapper.class
import com.me.pojo.Users;public interface UsersMapper {public Users selectById(int id);
}
mapper文件:UsersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.me.mapper.UsersMapper"><select id="selectById" parameterType="int" resultType="Users">select * from users where id=#{id}</select>
</mapper>
測試類:MapperTest
import com.me.mapper.UsersMapper;
import com.me.pojo.Users;
import com.me.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;public class MapperTest {@Testpublic void test(){SqlSession sqlSession=MybatisUtils.getSqlSessionFactory().openSession();UsersMapper usersMapper=sqlSession.getMapper(UsersMapper.class);Users user =usersMapper.selectById(1);sqlSession.close();}
}
根據map查詢
參數是hashmap。
接口
public List<Users> selectByMap(Map<String,Object> map);
mapper
- 注意接收的第二個參數,如果寫成’%#{addr}%‘無法獲取;如果寫成’${value}%'也無法獲取,因為不是簡單類型。
- “#{?}”中要和傳遞過來map的key一致。
<select id="selectByMap" parameterType="map" resultType="Users">select * from users where sex=#{sex} and address like '${addr}%'
</select>
測試類
Map<String,Object> map=new HashMap<String,Object>();
map.put("sex","男");
map.put("addr","北");
List<Users> usersList=usersMapper.selectByMap(map);
傳遞多參數
傳遞多參數有兩種方式。
- 方法一
接口
@Param()就相當于將參數封裝到map中去
public List<Users> selectByParams(@Param("sex") String sex, @Param("addr") String addr);
mapper
傳遞多參數時無需配置參數類型
<select id="selectByParams" resultType="Users">select * from users where sex=#{sex} and address like '${addr}%'
</select>
測試類
List<Users> usersList=usersMapper.selectByParams("男","北");
- 方法二
接口
public List<Users> selectByParams2( String sex, String addr);
mapper
第二個參數如果寫成’%${1}%'無法獲取。
<select id="selectByParams2" resultType="Users">select * from users where sex=#{0} and address like #{1}
</select>
測試類
List<Users> usersList=usersMapper.selectByParams2("男","%濟%");
好啦,這位老朋友就已經介紹給你認識了,不知道他能不能成為你的好朋友
一個腦回路清奇的程序猿,總是有一些神奇的想法,分享技術經驗給大家,一起學習進步