java SqlServer/MyBatis SqlMapConfig 数据源设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10398831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:54:02  来源:igfitidea点击:

SqlServer/MyBatis SqlMapConfig datasource setup

javasql-servermybatis

提问by JWiley

I'm working through this MyBatis tutorial, and am down to an issue with setting up the dataSource within the SqlMapConfig.xml. The tutorial is using MySql, like every other tutorial existing on the internet it seems, but I'm using SqlServer 2008.

我正在学习这个 MyBatis 教程,并且遇到了在 SqlMapConfig.xml 中设置数据源的问题。该教程使用的是 MySql,就像 Internet 上存在的所有其他教程一样,但我使用的是 SqlServer 2008。

Please help me convert the below code to connect to a local SqlServer, or help me understand how to setup environments/how the SqlMapConfig works with the connection factory.

请帮助我转换以下代码以连接到本地 SqlServer,或者帮助我了解如何设置环境/SqlMapConfig 如何与连接工厂一起工作。

Links to info/tutorials would be great too.

信息/教程的链接也很棒。

Thanks!

谢谢!

<?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">

<configuration>

    <typeAliases>
        <typeAlias alias="Contact" type="com.name.model.Contact"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/blog"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
       </environment>
    </environments>

    <mappers>
       <mapper resource="com/name/data/Contact.xml"/>
    </mappers>

</configuration>

UPDATED:

更新:

<?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">

<configuration>

    <typeAliases>
        <typeAlias alias="Contact" type="com.name.model.Contact"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="url" value="jdbc:sqlserver://localhost(or name of server):1433;databaseName=yourdbname; catalogName=sameasdbname"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
            </dataSource>
       </environment>
    </environments>

    <mappers>
       <mapper resource="com/name/data/Contact.xml"/>
    </mappers>

</configuration>

NOTE:I was using the latest JDBC driver 4 with SQL 2008, and had version issues, specifically the error: "Exception data: java.lang.UnsupportedClassVersionError (com/microsoft/sqlserver/jdbc/SQLServerDriver) bad major version at offset=6 ........."

注意:我在 SQL 2008 中使用了最新的 JDBC 驱动程序 4,并且有版本问题,特别是错误:“异常数据:java.lang.UnsupportedClassVersionError (com/microsoft/sqlserver/jdbc/SQLServerDriver) 偏移=6 处的主要版本错误…………”

Switch to the compatible driver sqljdbc.jar over sqljdbc3/sqljdbc4.jar to fix this.

通过 sqljdbc3/sqljdbc4.jar 切换到兼容的驱动程序 sqljdbc.jar 来解决这个问题。

采纳答案by Bogdan

To connect to SQL Server (or any other database out there) you need two basic things:

要连接到 SQL Server(或任何其他数据库),您需要两个基本的东西:

  • an appropriate JDBC driver.
  • appropriate properties when configuring the data saource;
  • 一个合适的 JDBC 驱动程序。
  • 配置数据源时的适当属性;

For the driver I guess you can go for "the official" Microsoft JDBC Driver for SQL Server distribution which you must make available on your application's classpath, then configure the data source properties in MyBatis configuration which involves specifying the driver class (com.microsoft.sqlserver.jdbc.SQLServerDriverif I'm not mistaken) and the connection url (which must start with jdbc:sqlserver://instead of jdbc:mysql://).

对于驱动程序,我想您可以选择“官方”Microsoft JDBC Driver for SQL Server 发行版,您必须在应用程序的类路径上提供该驱动程序,然后在 MyBatis 配置中配置数据源属性,这涉及指定驱动程序类(com.microsoft.sqlserver.jdbc.SQLServerDriver如果我是没记错)和连接 url(必须以jdbc:sqlserver://而不是开头jdbc:mysql://)。

You can find more information on the official page which will provide appropriate linksto help you through it all (just make sure you read the documentation for your version of SQL Server; SQL Server 2012 is out so Microsoft updated their docs).

您可以在官方页面上找到更多信息,该页面将提供适当的链接以帮助您完成所有操作(只需确保您阅读了 SQL Server 版本的文档;SQL Server 2012 已发布,因此 Microsoft 更新了他们的文档)。

Then depending on what types of queries you are running (for a basic tutorial as that one shouldn't be a problem) you must make sure that you use the proper SQL syntax.

然后,根据您正在运行的查询类型(对于基本教程,因为那应该不是问题),您必须确保使用正确的 SQL 语法