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);
    }

}

六、测试:

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

Spring+mybatis+postgresql整合

时间: 2024-10-15 02:22:34

Spring+mybatis+postgresql整合的相关文章

Spring+Mybatis+Maven 整合配置

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans default-autowire="byName" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:sche

SpringMvc+Spring+Mybatis+Maven整合

一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database - mydb ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET

SpringMVC+Spring+Mybatis+Maven整合-使用的工具

STS(Spring Tool Suite):是免费的一款基于Eclipse的Spring开发的定制版IDE,其官方下载地址为 http://www.springsource.org/downloads/sts-ggts 我选择的是现在最新的4.3.1版本,64位. 安装自不必细说. Tomcat:自己百度 JDK:自己谷歌 Mybatis generator:代码生成工具,下一篇中将详细介绍使用 http://code.google.com/p/mybatis/wiki/Downloads?t

Spring+Mybatis+Druid 整合Demo

Spring+Mybatis+Druid 整合Demo Spring+Mybatis+Druid+PageHelper整合参考. 链接地址:http://blog.csdn.net/vbirdbest/article/details/72821114 原文地址:https://www.cnblogs.com/harry335/p/8524894.html

SpringMVC MyBatis PostgreSQL整合

继续记录学习过程欧~~ 昨天不小心把辛辛苦苦做的SpringMVC MyBatis PostgreSQL代码给删除掉了,哎~想undo,结果不允许 不过塞公失马焉知非福 今天再来一遍就是了,就当是巩固了,不过确实把一些遗留的问题给解决了. 昨天首先遇到的一个问题就是,由于配置文件路径不对,导致Web应用程序初始化错误,导致无法加载, 我还傻乎乎的通过浏览器访问,结果心灰意冷,因为以前都是初始化没有问题,真正访问程序的时候出问题, 此种情况还会在eclipse里面显示出错误信息再进行定位.但是现在

idea+springmvc+spring+mybatis+maven整合返回json数据webapi

首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring-test, spring-orm, commons-dbcp, mybatis, mybatis-spring, mysql-connector-java, commons-fileupload, jstl,jackson-core, jackson-databind, jackson-mappe

SSM(Spring MVC +Spring+Mybatis)整合——maven工程

所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&q

springmvc spring mybatis框架整合

web.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <servlet> <servlet-name>SpringMVC</servlet-name> <serv

Spring+Mybatis+SpringMVC整合

这是来到博客园的第一篇文章,做java开发也好几年了,想从现在开始,在这里将自己会的.学到的做个系统的整理,便于自己掌握的更深.也便于以后复习.还便于给刚入门的攻城师们一点点参考(嘿嘿)--好了,废话不多说,第一篇文章就从最基本的javaWeb开发的三大框架整合开始. 至于为什么是从Spring+Mybatis+SpringMVC开始,而不是Spring+Struts+Hibernate,那是因为现在各个公司使用Spring+Mybatis+SpringMVC的越来越多,而Struts则是使用率