mybatis支持的数据库postgis吗

MyBatis3和Spring4基于接口编程的例子 - CSDN博客
MyBatis3和Spring4基于接口编程的例子
本博文已过时!!!通过JavaConfig集成MyBatis例子见这篇博文:
本例子使用的数据库为PostgreSQL。
项目源代码下载地址为
Maven创建Web项目,架构如下图所示
Maven引用如下图所示
Spring配置文件applicationContext.xml如下
&?xml version="1.0" encoding="UTF-8"?&
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:aop="http://www.springframework.org/schema/aop"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"&
&Spring公共配置文件 &
base-package="com.wu.mybatis" /&
location="classpath:application.properties" /&
id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close" lazy-init="false"&
name="driverClass" value="${jdbc.driver}" /&
name="jdbcUrl" value="${jdbc.url}" /&
name="username" value="${jdbc.username}" /&
name="password" value="${jdbc.password}" /&
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
name="dataSource" ref="dataSource"/&
name="configLocation"&
&classpath:mybatis-config.xml&
id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&
name="dataSource" ref="dataSource" /&
transaction-manager="transactionManager"/&
class="org.mybatis.spring.mapper.MapperScannerConfigurer"&
name="basePackage" value="com.wu.mybatis" /&
name="annotationClass" value="com.wu.mybatis.dao.Mapper"&&
application.properties文件如下
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://127.0.0.1:5432/postgres
jdbc.username=postgres
jdbc.password=postgres
dao包中的两个接口如下
package com.wu.mybatis.
import java.io.S
public interface BaseDao&T, PK extends Serializable& {
int insert(T o);
T selectById(PK id);
package com.wu.mybatis.
public @interface Mapper {
user包中,User类如下
package com.wu.mybatis.
public class User {
private String userN
UserMapper接口如下
package com.wu.mybatis.
import com.wu.mybatis.dao.BaseD
import com.wu.mybatis.dao.M
public interface UserMapper extends BaseDao&User, Integer&{
UserService接口如下
package com.wu.mybatis.
public interface UserService {
int save(User o);
User findById(Integer id);
UserServiceImpl类如下
package com.wu.mybatis.
import javax.annotation.R
import org.springframework.stereotype.S
import org.springframework.transaction.annotation.T
@Transactional
public class UserServiceImpl implements UserService{
private UserMapper userM
public int save(User o) {
return userMaper.insert(o);
public User findById(Integer id) {
return userMaper.selectById(id);
UserMapper.xml文件配置如下
&?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"&
namespace="com.wu.mybatis.user.UserMapper"&
id="insert" parameterType="user"&
insert into user_mybatis (username,password) values
(#{userName,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
id="selectById" parameterType="int" resultType="user"&
select id,username,password from user_mybatis where id=#{id}
mybatis-config.xml文件配置如下
&?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"&
name="cacheEnabled" value="false" /&
name="useGeneratedKeys" value="true" /&
name="defaultExecutorType" value="REUSE" /&
type="com.wu.mybatis.user.User" alias="user"/&
web.xml文件配置如下
&?xml version="1.0" encoding="UTF-8"?&
xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"&
&org.springframework.web.context.ContextLoaderListener&
&contextConfigLocation&
&classpath:applicationContext.xml&
建表语句如下
CREATE TABLE "public"."user_mybatis" (
"id" int4 DEFAULT nextval('user_mybatis_id_seq'::regclass) NOT NULL,
"username" varchar(32) COLLATE "default",
"password" varchar(32) COLLATE "default"
WITH (OIDS=FALSE);
ALTER TABLE "public"."user_mybatis" ADD PRIMARY KEY ("id");
测试类ServiceTest如下
package com.wu.mybatis.
import org.junit.BeforeC
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import com.wu.mybatis.user.U
import com.wu.mybatis.user.UserS
public class ServiceTest {
private static ApplicationContext act = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
act = new ClassPathXmlApplicationContext("applicationContext.xml");
public void test() throws Exception{
UserService service=(UserService)act.getBean("userServiceImpl");
User o = new User();
o.setUserName("admin1");
o.setPassword("pass1");
service.save(o);
MyBatis和Spring基于接口编程,核心dao只有一个,即该例中的Dao,里面可以写一些通用接口。然后面向对象编程中,可以在实体Mapper中扩展自用接口。service中调用dao层接口时,由MyBatis代理实现。
以上是一个简单例子,代码都贴出来了,比较详细,有些地方可能有些简陋,也可作为MyBatis入门例子,仅供参考。
项目源代码下载地址为
本文已收录于以下专栏:
相关文章推荐
对于整合Spring及Mybatis不作详细介绍,可以参考: MyBatis
3 User Guide Simplified Chinese.pdf,贴出我的主要代码如下:
UserMappe...
使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方。之前没有记录SSM整合...
Spring JPA MyBatis
这边记录一套比较高效的基础接口编程,在springmvc+mybatis架构下的基础接口,结合了所有的增删改查,节省很多代码的书写,使代码更加简洁明了,结合上泛型的使用。
1.BaseDao接口,结...
maven创建web项目,架构如下图
maven引用如下图,其中jdbc-driver由于版权问题在maven仓库找不到,是我自己本地注册的。
spring配置文件appl...
最近想玩儿一下maven,本以为这东西和svn或者cvs是差不多的存在,结果颠覆了。。。
所以,还是自己上手搭建一套基础环境体验一下吧
基本环境:
他的最新文章
讲师:何宇健
讲师:董岩
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Chapter 6. Using PostGIS Geometry: Building ApplicationsChapter 6. Using PostGIS Geometry: Building ApplicationsTable of Contents6.1. Using MapServerThe Minnesota MapServer is an internet web-mapping server which
conforms to the OpenGIS Web Mapping Server specification.The MapServer homepage is at .The OpenGIS Web Map Specification is at .6.1.1. Basic UsageTo use PostGIS with MapServer, you will need to know about how to
configure MapServer, which is beyond the scope of this documentation.
This section will cover specific PostGIS issues and configuration
details.To use PostGIS with MapServer, you will need:Version 0.6 or newer of PostGIS.Version 3.5 or newer of MapServer.MapServer accesses PostGIS/PostgreSQL data like any other
PostgreSQL client -- using the libpq interface. This means that
MapServer can be installed on any machine with network access to the
PostGIS server, and use PostGIS as a source of data. The faster the connection
between the systems, the better.Compile and install MapServer, with whatever options you
desire, including the "--with-postgis" configuration option.In your MapServer map file, add a PostGIS layer. For
example:LAYER
CONNECTIONTYPE postgis
NAME "widehighways"
# Connect to a remote spatial database
CONNECTION "user=dbuser dbname=gisdatabase host=bigserver"
PROCESSING "CLOSE_CONNECTION=DEFER"
# Get the lines from the 'geom' column of the 'roads' table
DATA "geom from roads using srid=4326 using unique gid"
# Of the lines in the extents, only render the wide highways
FILTER "type = 'highway' and numlanes &= 4"
# Make the superhighways brighter and 2 pixels wide
EXPRESSION ([numlanes] &= 6)
COLOR 255 22 22
# All the rest are darker and only 1 pixel wide
EXPRESSION ([numlanes] & 6)
COLOR 205 92 82
ENDIn the example above, the PostGIS-specific directives are as
follows:CONNECTIONTYPEFor PostGIS layers, this is always "postgis".CONNECTIONThe database connection is governed by the a 'connection
string' which is a standard set of keys and values like this
(with the default values in &&):user=&username& password=&password&
dbname=&username& hostname=&server&
port=&5432&An empty connection string is still valid, and any of
the key/value pairs can be omitted. At a minimum you will
generally supply the database name and username to connect
with.DATAThe form of this parameter is "&geocolumn& from
&tablename& using srid=&srid& using unique &primary key&" where the column is the spatial column to
be rendered to the map, the SRID is SRID used by the column and the primary key is the table primary key (or any
other uniquely-valued column with an index).You can omit the "using srid" and "using unique" clauses and MapServer will automatically determine the
correct values if possible, but at the cost of running a few extra queries on the server for each map
draw.PROCESSINGPutting in a CLOSE_CONNECTION=DEFER if you have multiple layers reuses existing connections instead of closing them.
This improves
Refer to for
for a more detailed explanation.
FILTERThe filter must be a valid SQL string corresponding to
the logic normally following the "WHERE" keyword in a SQL
query. So, for example, to render only roads with 6 or more
lanes, use a filter of "num_lanes &= 6".In your spatial database, ensure you have spatial (GiST)
indexes built for any the layers you will be drawing.CREATE INDEX [indexname] ON [tablename] USING GIST ( [geometrycolumn] );If you will be querying your layers using MapServer you will
also need to use the "using unique" clause in your DATA statement.MapServer requires unique identifiers for each spatial record
when doing queries, and the PostGIS module of MapServer uses the
unique value you specify in order to provide these unique
identifiers. Using the table primary key is the best practice.6.1.2. Frequently Asked Questions6.1.2.1. 6.1.2.2. 6.1.2.3. 6.1.2.4. 6.1.2.5. 6.1.2.1.When I use an EXPRESSION in my map file,
the condition never returns as true, even though I know the values
exist in my table.Unlike shape files, PostGIS field names have to be
referenced in EXPRESSIONS using lower
case.EXPRESSION ([numlanes] &= 6)6.1.2.2.The FILTER I use for my Shape files is not working for my
PostGIS table of the same data.Unlike shape files, filters for PostGIS layers use SQL
syntax (they are appended to the SQL statement the PostGIS
connector generates for drawing layers in MapServer).FILTER "type = 'highway' and numlanes &= 4"6.1.2.3.My PostGIS layer draws much slower than my Shape file layer,
is this normal?In general, the more features you are drawing into a given map,
the more likely it is that PostGIS will be slower than Shape files.
For maps with relatively few features (100s), PostGIS will often be faster.
For maps with high feature density (1000s), PostGIS will always be slower.
If you are finding substantial draw performance problems, it
is possible that you have not built a spatial index on your
table.postgis# CREATE INDEX geotable_gix ON geotable USING GIST ( geocolumn );
postgis# VACUUM ANALYZE;6.1.2.4.My PostGIS layer draws fine, but queries are really slow.
What is wrong?For queries to be fast, you must have a unique key for your
spatial table and you must have an index on that unique
key.You can specify what unique key for mapserver to use with
the USING UNIQUE clause in your
DATA line:DATA "geom FROM geotable USING UNIQUE gid"6.1.2.5.Can I use "geography" columns (new in PostGIS 1.5) as a source for
MapServer layers?Yes! MapServer understands geography columns as being the same as
geometry columns, but always using an SRID of 4326. Just make sure to include
a "using srid=4326" clause in your DATA statement. Everything else
works exactly the same as with geometry.DATA "geog FROM geogtable USING SRID=4326 USING UNIQUE gid"6.1.3. Advanced UsageThe USING pseudo-SQL clause is used to add some
information to help mapserver understand the results of more complex
queries. More specifically, when either a view or a subselect is used as
the source table (the thing to the right of "FROM" in a
DATA definition) it is more difficult for mapserver
to automatically determine a unique identifier for each row and also the
SRID for the table. The USING clause can provide
mapserver with these two pieces of information as follows:DATA "geom FROM (
table1.geom AS geom,
table1.gid AS gid,
table2.data AS data
FROM table1
LEFT JOIN table2
ON table1.id = table2.id
) AS new_table USING UNIQUE gid USING SRID=4326"USING UNIQUE &uniqueid&MapServer requires a unique id for each row in order to
identify the row when doing map queries. Normally it identifies
the primary key from the system tables. However, views and subselects don't
automatically have an known unique column. If you want to use MapServer's
query functionality, you need to ensure your view
or subselect includes a uniquely valued column, and declare it with USING UNIQUE.
For example, you could explicitly select nee of the table's primary key
values for this purpose, or any other column which is guaranteed
to be unique for the result set."Querying a Map" is the action of clicking on a map to ask
for information about the map features in that location. Don't
confuse "map queries" with the SQL query in a
DATA definition.USING SRID=&srid&PostGIS needs to know which spatial referencing system is
being used by the geometries in order to return the correct data
back to MapServer. Normally it is possible to find this
information in the "geometry_columns" table in the PostGIS
database, however, this is not possible for tables which are
created on the fly such as subselects and views. So the
USING SRID= option allows the correct SRID to
be specified in the DATA definition.6.1.4. ExamplesLets start with a simple example and work our way up. Consider the
following MapServer layer definition:LAYER
CONNECTIONTYPE postgis
NAME "roads"
CONNECTION "user=theuser password=thepass dbname=thedb host=theserver"
DATA "geom from roads"
COLOR 0 0 0
ENDThis layer will display all the road geometries in the roads table
as black lines.Now lets say we want to show only the highways until we get zoomed
in to at least a 1:100000 scale - the next two layers will achieve this
effect:LAYER
CONNECTIONTYPE postgis
CONNECTION "user=theuser password=thepass dbname=thedb host=theserver"
PROCESSING "CLOSE_CONNECTION=DEFER"
DATA "geom from roads"
MINSCALE 100000
FILTER "road_type = 'highway'"
COLOR 0 0 0
CONNECTIONTYPE postgis
CONNECTION "user=theuser password=thepass dbname=thedb host=theserver"
PROCESSING "CLOSE_CONNECTION=DEFER"
DATA "geom from roads"
MAXSCALE 100000
CLASSITEM road_type
EXPRESSION "highway"
COLOR 255 0 0
COLOR 0 0 0
ENDThe first layer is used when the scale is greater than 1:100000,
and displays only the roads of type "highway" as black lines. The
FILTER option causes only roads of type "highway" to
be displayed.The second layer is used when the scale is less than 1:100000, and
will display highways as double-thick red lines, and other roads as
regular black lines.So, we have done a couple of interesting things using only
MapServer functionality, but our DATA SQL statement
has remained simple. Suppose that the name of the road is stored in
another table (for whatever reason) and we need to do a join to get it
and label our roads.LAYER
CONNECTIONTYPE postgis
CONNECTION "user=theuser password=thepass dbname=thedb host=theserver"
DATA "geom FROM (SELECT roads.gid AS gid, roads.geom AS geom,
road_names.name as name FROM roads LEFT JOIN road_names ON
roads.road_name_id = road_names.road_name_id)
AS named_roads USING UNIQUE gid USING SRID=4326"
MAXSCALE 20000
TYPE ANNOTATION
LABELITEM name
ANGLE auto
COLOR 0 192 0
TYPE truetype
FONT arial
ENDThis annotation layer adds green labels to all the roads when the
scale gets down to 1:20000 or less. It also demonstrates how to use an
SQL join in a DATA definition.6.2. Java Clients (JDBC)Java clients can access PostGIS "geometry" objects in the PostgreSQL
database either directly as text representations or using the JDBC
extension objects bundled with PostGIS. In order to use the extension
objects, the "postgis.jar" file must be in your CLASSPATH along with the
"postgresql.jar" JDBC driver package.import java.sql.*;
import java.util.*;
import java.lang.*;
import org.postgis.*;
public class JavaGIS {
public static void main(String[] args) {
java.sql.C
* Load the JDBC driver and establish a connection.
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/database";
conn = DriverManager.getConnection(url, "postgres", "");
* Add the geometry types to the connection. Note that you
* must cast the connection to the pgsql-specific connection
* implementation before calling the addDataType() method.
((org.postgresql.PGConnection)conn).addDataType("geometry",Class.forName("org.postgis.PGgeometry"));
((org.postgresql.PGConnection)conn).addDataType("box3d",Class.forName("org.postgis.PGbox3d"));
* Create a statement and execute a select query.
Statement s = conn.createStatement();
ResultSet r = s.executeQuery("select geom,id from geomtable");
while( r.next() ) {
* Retrieve the geometry as an object then cast it to the geometry type.
* Print things out.
PGgeometry geom = (PGgeometry)r.getObject(1);
int id = r.getInt(2);
System.out.println("Row " + id + ":");
System.out.println(geom.toString());
s.close();
conn.close();
catch( Exception e ) {
e.printStackTrace();
}The "PGgeometry" object is a wrapper object which contains a
specific topological geometry object (subclasses of the abstract class
"Geometry") depending on the type: Point, LineString, Polygon, MultiPoint,
MultiLineString, MultiPolygon.PGgeometry geom = (PGgeometry)r.getObject(1);
if( geom.getType() == Geometry.POLYGON ) {
Polygon pl = (Polygon)geom.getGeometry();
for( int r = 0; r & pl.numRings(); r++) {
LinearRing rng = pl.getRing(r);
System.out.println("Ring: " + r);
for( int p = 0; p & rng.numPoints(); p++ ) {
Point pt = rng.getPoint(p);
System.out.println("Point: " + p);
System.out.println(pt.toString());
}The JavaDoc for the extension objects provides a reference for the
various data accessor functions in the geometric objects.6.3. C Clients (libpq)...6.3.1. Text Cursors...6.3.2. Binary Cursors...Chapter}

我要回帖

更多关于 mybatis 支持c3p0吗 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信