MyBatis mapper代理方式
生活随笔
收集整理的這篇文章主要介紹了
MyBatis mapper代理方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
3.
mapper代理方式(程序員只需mapper接口(相當于Dao的接口))
3.1? ?思路:1.程序員只需要寫mapper接口(相當于Dao接口),mybatis可以自動生成mapper接口實現類的代理對象
2.程序員需要編寫mapper.xml映射文件
3.開發規范:1.在mapper.xml中namespace等于mapper接口的地址
<!-- namespace命名空間 ,作用對sql進行分類化管理,理解sql隔離,namespace等于mapper接口地址 注意:使用mapper代理的方法進行開發,namespace具有特殊的意義
-->
<mapper namespace="top.haoyeyin.mapper.StudentMapper">
2.mapper接口中方法名和mapper.xml中的statement中的id一致
?3.mapper.java接口中的輸入參數類型和mapper.xml中statement的parameterType的指定?類型一致 4.mapper.java接口中方法的返回類型和mapper.xml中resultType指定的類型一致
public Student findStudentById(int id) throws Exception;
總結:1.mapper.java public interface StudentMapper {
//根據ID查詢學生信息
public Student findStudentById(int id) throws Exception;
2.mapper.xml? <mapper namespace="top.haoyeyin.mapper.StudentMapper">
<!-- 映射文件中配置多種sql語句 -->
<!-- 通過select語句查詢,id用于標識映射文件中的sql,所以id為statement的ID
將來sql語句封裝到mapperstatement對象中
paramterType:指定輸入參數類型,這里ID指定int型
#{}:占位符
#{id}:id表示輸入的參數,id是參數名稱,如果輸入是簡單類型#{}中的參數名可以任意,可以是
value或其名稱
resultType:指定sql輸出結果對應映射java對象,select指定的 resulType表示單條記錄
所映射的Java對象
-->
<select id="findStudentById" parameterType="int" resultType="top.haoyeyin.pojo.Student">
select * from student where id=#{id}
</select>
3.測試方法
package top.haoyeyin.mapper;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
public class StudentMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws Exception{
BasicConfigurator.configure();
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創建會話工廠sessionFactory,傳入SqlMapConfig.xml配置信息
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindStudentById() {
SqlSession sqlSession=sqlSessionFactory.openSession();
//創建StudentMapper對象
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//調用studentMapper的方法
try {
studentMapper.findStudentById(4);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.1? ?思路:1.程序員只需要寫mapper接口(相當于Dao接口),mybatis可以自動生成mapper接口實現類的代理對象
2.程序員需要編寫mapper.xml映射文件
3.開發規范:1.在mapper.xml中namespace等于mapper接口的地址
<!-- namespace命名空間 ,作用對sql進行分類化管理,理解sql隔離,namespace等于mapper接口地址 注意:使用mapper代理的方法進行開發,namespace具有特殊的意義
-->
<mapper namespace="top.haoyeyin.mapper.StudentMapper">
2.mapper接口中方法名和mapper.xml中的statement中的id一致
?3.mapper.java接口中的輸入參數類型和mapper.xml中statement的parameterType的指定?類型一致 4.mapper.java接口中方法的返回類型和mapper.xml中resultType指定的類型一致
public Student findStudentById(int id) throws Exception;
總結:1.mapper.java public interface StudentMapper {
//根據ID查詢學生信息
public Student findStudentById(int id) throws Exception;
2.mapper.xml? <mapper namespace="top.haoyeyin.mapper.StudentMapper">
<!-- 映射文件中配置多種sql語句 -->
<!-- 通過select語句查詢,id用于標識映射文件中的sql,所以id為statement的ID
將來sql語句封裝到mapperstatement對象中
paramterType:指定輸入參數類型,這里ID指定int型
#{}:占位符
#{id}:id表示輸入的參數,id是參數名稱,如果輸入是簡單類型#{}中的參數名可以任意,可以是
value或其名稱
resultType:指定sql輸出結果對應映射java對象,select指定的 resulType表示單條記錄
所映射的Java對象
-->
<select id="findStudentById" parameterType="int" resultType="top.haoyeyin.pojo.Student">
select * from student where id=#{id}
</select>
3.測試方法
package top.haoyeyin.mapper;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
public class StudentMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws Exception{
BasicConfigurator.configure();
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創建會話工廠sessionFactory,傳入SqlMapConfig.xml配置信息
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindStudentById() {
SqlSession sqlSession=sqlSessionFactory.openSession();
//創建StudentMapper對象
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//調用studentMapper的方法
try {
studentMapper.findStudentById(4);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
總結
以上是生活随笔為你收集整理的MyBatis mapper代理方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis开发Dao的方法
- 下一篇: 4.MyBatis全局配置文件SqlMa