mybatis與spring的整合

首先在項(xiàng)目上導(dǎo)入所需的jar:(后面有視頻資源,源碼和jar的下載)
mybatis_spring.png
然后是創(chuàng)建好bean,我這里是用Student為例
public class Student 
{
    //對應(yīng)上數(shù)據(jù)庫的信息
    private String id;
    private String name;
    private String sex;
    private String age;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex
                + ", age=" + age + "]";
    }
}
然后是配置student.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="Student.Message.IStudent.IStudent">
    <resultMap type="Student.Message.bean.Student" id="Student">
   <!-- 擁有主鍵的話就用ID標(biāo)簽 -->
    <id column="ID" jdbcType="INTEGER" property="id"/> 
     <result column="NAME" jdbcType="VARCHAR" property="name"/>
     <result column="SEX" jdbcType="VARCHAR" property="sex"/>
     <result column="AGE" jdbcType="INTEGER" property="age"/>
    </resultMap>
    <select id="showStudent" parameterType="Student.Message.bean.Student" resultMap="Student">
        select * from student
    </select>
  </mapper>
mybatis_spring1.png
接下來是student.xml各種sql的實(shí)現(xiàn)接口
public interface IStudent 
{
     //顯示全部學(xué)生信息
    public List<Student> showStudent();
}
配置總的mybatis文件
<configuration>
<!-- 配置log4j文件 -->
    <settings>  
        <setting name="logImpl" value="LOG4J"/>  
    </settings> 
  <mappers>
    <!-- Student.xml的全路徑 -->
    <mapper resource="Student/Message/config/Student.xml"/>
  </mappers>
</configuration>
配置spring文件

這里引用了外部的文件來配置連接數(shù)據(jù)庫
使用context的命名空間采章,注意:開頭的網(wǎng)址运嗜,是用來引入各種命名空間

//這是db.properties文件
user=root
password=root
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    <context:property-placeholder location="classpath:db.properties" />
    
    <!-- 
        獲取數(shù)據(jù)源
     -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverclass}"></property>
        <property name="jdbcUrl" value="${jdbcurl}"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource屬性指定要用到的連接池-->
        <property name="dataSource" ref="dataSource"></property>
        <!-- configLocation屬性連接mybatis的核心配置文件 -->
        <property name="configLocation" value="Configuration.xml"></property>
    </bean>
    
    <bean id="showstudent" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--mapperInterface屬性指定映射器接口,用于實(shí)現(xiàn)此接口并生成映射器對象
            value:接口的全類名    
        -->
        <property name="mapperInterface" value="Student.Message.IStudent.IStudent"></property>
        <!--sqlSessionFactory屬性指定要用到的SqlSessionFactory實(shí)例-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    
</beans>
最后是測試類
public class Main 
{

    public static void main(String[] args) 
    {
        //用來接收從數(shù)據(jù)
        List<Student> studentlist=new ArrayList<Student>();
        //創(chuàng)建IOC容器
        //ClassPathXmlApplicationContext是ApplicationContext的實(shí)現(xiàn)類悯舟,從類路徑來加載配置文件
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.從ioc容器中獲取bean實(shí)例
        //利用id來定位IOC容器中的bean
        IStudent istudent=(IStudent) ctx.getBean("showstudent");
        studentlist=istudent.showStudent();
        for(Student s:studentlist)
        {
            System.out.println(s.toString());
        }
    }
}
結(jié)果成功實(shí)現(xiàn)
搜狗截圖17年07月20日1639_5.png

源碼下載(http://pan.baidu.com/s/1bKHLls) 密碼:jlv1

spring學(xué)習(xí)
mybatis學(xué)習(xí)

(http://pan.baidu.com/s/1i5n9ElR) 密碼為:u07n

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末担租,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抵怎,更是在濱河造成了極大的恐慌奋救,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件便贵,死亡現(xiàn)場離奇詭異菠镇,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)承璃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門利耍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人盔粹,你說我怎么就攤上這事隘梨。” “怎么了舷嗡?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵轴猎,是天一觀的道長。 經(jīng)常有香客問我进萄,道長捻脖,這世上最難降的妖魔是什么锐峭? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮可婶,結(jié)果婚禮上沿癞,老公的妹妹穿的比我還像新娘。我一直安慰自己矛渴,他們只是感情好椎扬,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著具温,像睡著了一般蚕涤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上铣猩,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天揖铜,我揣著相機(jī)與錄音,去河邊找鬼达皿。 笑死蛮位,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鳞绕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尸曼,長吁一口氣:“原來是場噩夢啊……” “哼们何!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起控轿,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冤竹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后茬射,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹦蠕,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年在抛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钟病。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡刚梭,死狀恐怖肠阱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情朴读,我是刑警寧澤屹徘,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站衅金,受9級特大地震影響噪伊,放射性物質(zhì)發(fā)生泄漏簿煌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一鉴吹、第九天 我趴在偏房一處隱蔽的房頂上張望姨伟。 院中可真熱鬧,春花似錦拙寡、人聲如沸授滓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽般堆。三九已至,卻和暖如春诚啃,著一層夾襖步出監(jiān)牢的瞬間淮摔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工始赎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留和橙,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓造垛,卻偏偏與公主長得像魔招,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子五辽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理办斑,服務(wù)發(fā)現(xiàn),斷路器杆逗,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,811評論 6 342
  • 蘇錦瞳閱讀 120評論 0 0
  • 這次旅行帶了一個(gè)寫ルンです乡翅,第一次玩,雖然成本略高罪郊,不過拍出來真的是很有感覺蠕蚜。 第二次來麗江,7年前對麗江的印象是...
    小靴子筆記閱讀 169評論 0 0
  • 大膽假設(shè)悔橄,小心求證 不一定最愛的人在一起靶累,不開始就不會失去。 強(qiáng)求會苦了自己
    鳶尾J閱讀 142評論 0 0