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整合的更多相关文章
- struts2 + spring + mybatis 框架整合详细介绍
struts2 + spring + mybatis 框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...
- SpringMvc+Spring+Mybatis+Maven整合
一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database ...
- JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合
搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例 a)准备 ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
- idea+springmvc+spring+mybatis+maven整合返回json数据webapi
首先看一张目录结构图: : 创建步骤: 1.创建maven webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...
- spring mybatis springmvc整合
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- SSM(Spring MVC +Spring+Mybatis)整合——maven工程
所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需 ...
- SpringMVC Spring MyBatis 框架整合 Annotation MavenProject
项目结构目录 pom.xml jar包管理 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
随机推荐
- 【转】IntelliJ IDEA关联SVN
http://blog.csdn.net/xdd19910505/article/details/52756417 问题描述: IntelliJ IDEA安装之后,使用SVN进行提交或更新以及检出代码 ...
- Git:基础要点
直接快照,而非比较差异. 近乎所有操作都可本地执行. 在Git 中的绝大多数操作都只需要访问本地文件和资源,不用连网.但如果用CVCS 的话,差不多所有操作都需要连接网络.因为Git 在本地磁盘上就保 ...
- Java 公历转农历,然后农历减一年(或者几天或者任意天),再把这个日期转成公历
由于系统的提醒有可能是农历的今天或指定时间要用quartz 实现定时任务 公历转农历,然后农历减一年(或者几天或者任意天),再把这个日期转成公历. 网上很多农历转公历的程序都有问题,QQ.百度的也有 ...
- 通过Gson解析Json数据
Json是一种数据格式,便于数据传输.存储.交换:Gson是一种组件库,可以把java对象数据转换成json数据格式. gson.jar的下载地址:http://search.maven.org/#s ...
- Material Designer的低版本兼容实现(八)—— Flat Button
除了中规中矩的矩形按钮外,5.0中将按钮扁平化,产生了一个扁平按钮——Flat Button.这个按钮降低了很多存在感,主要用于在对话框,提示栏中.让整个界面减少层级.今天说的就是它的用法. 这 ...
- [Web 前端] MobX
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法 而MobX提供了一种存储,更新 状态 的方法 React 和 MobX都在优化着软件开发中相同的问题. R ...
- [转]Memcache的原理和命中率的总结
From : http://blog.csdn.net/hbzyaxiu520/article/details/19546969 1 Memcache是什么Memcache是danga.c ...
- ConcurrentHashMap和HashMap的区别
(1)ConcurrentHashMap对整个桶数组进行了分段,而HashMap则没有 (2)ConcurrentHashMap在每一个分段上都用锁进行保护,从而让锁的粒度更精细一些,并发性能更好,而 ...
- 阿里jstorm和storm区别
转自:https://www.cnblogs.com/cn-leodream/p/6497277.html 看介绍文档貌似挺好:https://github.com/alibaba/jstorm ...
- go语言之进阶篇通过switch实现类型断言
1.通过switch实现类型断言 示例: package main import "fmt" type Student struct { name string id int } ...