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