天天看點

spring3.2.2+mybatis3.2.3+c3p0項目整合

内部邀請碼:C8E245J (不寫邀請碼,沒有現金送)

國内私募機構九鼎控股打造,九鼎投資是在全國股份轉讓系統挂牌的公衆公司,股票代碼為430719,為“中國PE第一股”,市值超1000億元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

在搭建項目之前,建立資料庫,以及資料庫表,我用的資料庫是mysql 5.6.11

建立資料庫語句為:

spring3.2.2+mybatis3.2.3+c3p0項目整合

CREATE TABLE `orders` (  

  `id` bigint(20) NOT NULL AUTO_INCREMENT,  

  `company_id` varchar(255) DEFAULT NULL,  

  `charge` decimal(18,4) NOT NULL DEFAULT '0.0000',  

  `state` int(11) DEFAULT '0',  

  `remark` varchar(500) DEFAULT NULL,  

  PRIMARY KEY (`id`)  

);  

 搭建項目需要以下的包,

mybatis-3.2.3-SNAPSHOT.jar

mybatis-spring-1.2.0.jar

mysql-connector-java-5.1.24-bin.jar

c3p0-0.9.1.2.jar

commons-logging-1.1.1.jar

spring-aop-3.2.2.RELEASE.jar

spring-aspects-3.2.2.RELEASE.jar

spring-beans-3.2.2.RELEASE.jar

spring-context-3.2.2.RELEASE.jar

spring-context-support-3.2.2.RELEASE.jar

spring-core-3.2.2.RELEASE.jar

spring-expression-3.2.2.RELEASE.jar

spring-jdbc-3.2.2.RELEASE.jar

spring-tx-3.2.2.RELEASE.jar

好了,簡單的準備工作已經準備就緒,現在來建立一個java項目或者java web項目

首先,建立dao接口類。

spring3.2.2+mybatis3.2.3+c3p0項目整合

package com.yunix.dao;  

public interface IOrderDao {  

    public int getOrderCount();  

}  

然後在com.yunix.dao.impl包下建立OrderDaoMapper.xml

内容為:

spring3.2.2+mybatis3.2.3+c3p0項目整合

<?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="com.yunix.dao.IOrderDao">  

    <select id="getOrderCount"  resultType="int">  

    <![CDATA[ 

        select count(1) from orders 

    ]]>  

    </select>  

</mapper>  

 其中namespace為dao接口,id為dao接口中的方法。

建立service接口:

spring3.2.2+mybatis3.2.3+c3p0項目整合

package com.yunix.service;  

public interface IOrderService {  

 建立service實作類:

spring3.2.2+mybatis3.2.3+c3p0項目整合

package com.yunix.service.impl;  

import org.springframework.beans.factory.annotation.Autowired;  

import org.springframework.stereotype.Service;  

import com.yunix.dao.IOrderDao;  

import com.yunix.service.IOrderService;  

@Service  

public class OrderService implements IOrderService {  

    @Autowired  

    private IOrderDao orderDao;  

    @Override  

    public int getOrderCount() {  

        return orderDao.getOrderCount();  

    }  

 在src檔案夾下建立applicationCntext.xml檔案

spring3.2.2+mybatis3.2.3+c3p0項目整合

<beans xmlns="http://www.springframework.org/schema/beans"    

    default-autowire="byName"    

    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-3.0.xsd  

    http://www.springframework.org/schema/context   

    http://www.springframework.org/schema/context/spring-context-3.0.xsd">    

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"   

            destroy-method="close">  

        <property name="driverClass" value="com.mysql.jdbc.Driver" />  

        <property name="jdbcUrl" value="jdbc:mysql://192.168.1.5:3306/test" />  

        <property name="user" value="yunix" />  

        <property name="password" value="123456" />  

        <property name="initialPoolSize" value="1" />  

        <property name="minPoolSize" value="0" />  

        <property name="maxPoolSize" value="10" />  

        <property name="acquireIncrement" value="5" />  

        <property name="maxIdleTime" value="10" />  

        <property name="maxStatements" value="0" />  

    </bean>  

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">    

        <constructor-arg index="0" ref="sqlSessionFactory" />    

    </bean>    

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

        <property name="dataSource" ref="dataSource" />  

        <property name="mapperLocations"  

            value="classpath*:com/yunix/dao/impl/*Mapper.xml" />  

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

        <property name="basePackage" value="com.yunix.dao" />  

    <context:component-scan base-package="com.yunix.service" />  

</beans>  

 以及log4j.properties

spring3.2.2+mybatis3.2.3+c3p0項目整合

log4j.appender.stdout=org.apache.log4j.ConsoleAppender  

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] -%m%n  

log4j.logger.com.ibatis=debug  

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug  

log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug  

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug  

log4j.logger.java.sql.Connection=debug  

log4j.logger.java.sql.Statement=debug  

log4j.logger.java.sql.PreparedStatement=debug,stdout  

至此,項目搭建完成。

到底搭建成不成功,寫一個測試類進行簡單測試一下。我寫的比較簡單,寫了個main方法進行了測試,測試代碼如下:

spring3.2.2+mybatis3.2.3+c3p0項目整合

package test;  

import org.springframework.context.ApplicationContext;  

import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class MyBatisTest {  

    public static void main(String[] args) {  

        @SuppressWarnings("resource")  

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");    

        IOrderService orderService = (IOrderService)context.getBean("orderService");    

        int count = orderService.getOrderCount();    

        System.out.println("count:" + count);    

 運作結果為:

spring3.2.2+mybatis3.2.3+c3p0項目整合

count:11