1. 程式人生 > >mybatis-spring Dao整合

mybatis-spring Dao整合

專案目錄如下:
專案目錄

與spring整合後,mybatisConfig可以簡化成

<?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>

    <settings>
   <!--     <setting name="logImpl" value="LOG4J"/>-->
<setting name="logImpl" value="LOG4J"/> <!--設定延遲載入--> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> <!--設定二級快取--> <setting name="cacheEnabled" value="true"/> </settings
>
<!--建立別名--> <typeAliases> <!--單個別名--> <!--<typeAlias type="com.v7.demo.mybatis.bean.User" alias="User"/>--> <!-- 定義多個 --> <package name="com.demo.mybatis.bean"/> </typeAliases> <!--載入對映檔案-->
<mappers> <!--<mapper resource="sqlmap/user.xml"/>--> <!--載入單個對映檔案 resource從類路徑開 url跟路徑--> <mapper resource="mapper/User.xml"/> <mapper resource="mapper/OrdersCustom.xml"/> </mappers> </configuration>

spring的applicationConext

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
     >

    <!--載入配置檔案-->
    <context:property-placeholder location="db.properties"/>


    <!--配置資料來源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 指定連線資料庫的驅動-->
        <property name="driverClass" value="${jdbc.driver}"/>
        <!-- 指定連線資料庫的URL-->
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <!-- 指定連線資料庫的使用者名稱-->
        <property name="user" value="${jdbc.username}"/>
        <!-- 指定連線資料庫的密碼-->
        <property name="password" value="${jdbc.password}"/>
    </bean>

<!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--載入mybatis的配置檔案-->
        <property name="configLocation" value="mybatis/MConfig.xml" />
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="userDao" class="com.demo.mybatis.dao.UserDaoImpl">
        <property ref="sqlSessionFactory" name="sqlSessionFactory"/>
    </bean>
</beans>

DaoImpl需要繼承SqlSessionDaoSupport,獲取sqlsession使用this.getSqlSession()
與spring整合後不需要手動呼叫sqlsession.close()
UserDaoImpl如下:

    public User findUserById(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();

        User user = (User) sqlSession.selectOne("com.demo.mybatis.mapper.UserMapper.findUserById",id);


        return user;
    }

    public void insertUser(User user) throws Exception {
        SqlSession sqlSession = this.getSqlSession();

         sqlSession.insert("com.demo.mybatis.mapper.UserMapper.insertUser",user);

        sqlSession.commit();



    }

    public void deleteUserById(int id) throws Exception {
        SqlSession sqlSession = this.getSqlSession();

        sqlSession.delete("com.demo.mybatis.mapper.UserMapper.insertUser",id);

        sqlSession.commit();

    }