今天给大家介绍一位老朋友
当你第一次接触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=mysql
offsetAsPageNum=true
<!-- 防止出现小于第一页,大于最后一页的异常情况出现。 -->
reasonable=true
</value>
</property>
</bean>
<bean class="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="properties">
<value>
<!-- 主键自增回写方法,默认值MYSQL -->
IDENTITY=MYSQL
mappers=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 {
@Autowired
private UserInfoMapper userInfoMapper;
@Override
public List<UserInfo> select(UserInfo userInfo) {
return userInfoMapper.select(userInfo);
}
}
测试类
@Autowired
private UserInfoService userInfoService;
@Test
public 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 mapper
PUBLIC "-//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 {
@Test
public 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("男","%济%");
好啦,这位老朋友就已经介绍给你认识了,不知道他能不能成为你的好朋友
一个脑回路清奇的程序猿,总是有一些神奇的想法,分享技术经验给大家,一起学习进步