MBS8: 使用注解代替mapper进行实现类开发
作者:互联网
8.1 简介
底层是反射机制实现与动态代理。
8.2 使用
TestMapper:
package com.yun.test;
import com.yun.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TestMapper {
@Select("select * from user")
List<User> getUserList();
}
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.yun.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--注册mapper-->
<mapper class="com.yun.test.TestMapper"/>
</mappers>
</configuration>
测试接口:
package com.yun.test;
import com.yun.pojo.User;
import com.yun.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class TempTest {
@Test
public void tempTest(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
List<User> userList = testMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
写在最后,使用注解开发,很简便,但是只能用于一些简单的语句,如果是一些复杂的操作,反而会适得其反,使为了简便而生的注解,变得复杂无序。
标签:mapper,TestMapper,MBS8,List,User,yun,import,com,注解 来源: https://blog.csdn.net/qq_44700366/article/details/119901197