Spring+mybatis+postgresql整合

最近做了一个项目,需要使用Spring+mybatis+postgresql,下面记录一下整合步骤:

一、准备JAR包:

我使用的是maven,所以直接晒出pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test.qunar.com</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>web</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1102-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>8.0.9</version>
        </dependency>
    </dependencies>
</project>

二、编写Spring.xml

<?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-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-autowire="byName">
    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:component-scan base-package="com.qunar.study"></context:component-scan>
    <import resource="classpath:spring-mybatis.xml"/>
</beans>

三、边写Spring-mybatis.xml

<?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:beans="http://www.springframework.org/schema/beans"
    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-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="byName">

    <!-- 引入属性文件 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close" autowire="no">
        <property name="fairQueue" value="false" />
        <property name="minIdle" value="1" />
        <property name="maxIdle" value="5" />
        <property name="maxActive" value="5" />
        <property name="initialSize" value="1" />
        <property name="testOnBorrow" value="true" />
        <property name="validationQuery" value="select 1" />
        <property name="validationInterval" value="500000" /><!-- 5min -->
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="30" />
        <property name="driverClassName" value="${ticket.database.driver}" />
        <property name="url" value="${ticket.database.url}" />
        <property name="username" value="${ticket.database.username}" />
        <property name="password" value="${ticket.database.password}" />
    </bean>

    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mapper.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qunar.study.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sessionFactory" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

四、生成代码,这个可以参考上一篇文章:http://www.cnblogs.com/liqiu/p/3869294.html

五、写Service代码:

package com.qunar.study.service.impl;

import com.qunar.study.entity.Users;
import com.qunar.study.mapper.UsersMapper;
import com.qunar.study.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("usersService")
public class UsersService implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;
    
    public Users getUserById(Integer id) {
        return usersMapper.selectByPrimaryKey(id);
    }

}

六、测试:

具体代码我就不贴出来了,可以直接下载:https://files.cnblogs.com/liqiu/webbak.zip

posted @ 2014-07-26 10:34  李秋  阅读(13325)  评论(2编辑  收藏  举报