• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用mybatis+SQLServer做持久层入门

mysql 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

本篇文章介绍如何用mybatis连接SQLServer数据库。 1、在http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx 下载SQL Server 2014 Express 免费版,用于学习用。 然后,安装并配置好默认管理员sa的密码。 2

本篇文章介绍如何用mybatis连接SQLServer数据库。

1、在http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx 下载SQL Server 2014 Express 免费版,用于学习用。

然后,安装并配置好默认管理员sa的密码。

2、在Sql Server Configuration Manager里面,将TCP/IP启用,并重启SQLServer。然后我们可以使用一些数据库客户端尝试连接数据库。

3、在http://www.microsoft.com/zh-CN/do本文来源gaodai#ma#com搞*!代#%^码$网!wnload/details.aspx?id=11774 下载SQLServer 的jdbc驱动。里面有简单的例子和三个版本的jar包。

4、现在我拿到了一个spring+springmvc+mybatis的项目,之前这个项目使用的数据库是mysql,现在我要换成SQLServer。
首先我将其中一个版本的jdbc.jar包引进项目中,这里我使用的是sqljdbc4.jar。
(调试中出现
Cannot load JDBC driver class ‘com.microsoft.sqlserver.jdbc.SQLServerDriver’
或Cannot create PoolableConnectionFactory (不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本。
等异常,那可能就是使用的jar包版本不对)

然后修改jdbc.properties:

<code>driver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc\:sqlserver\://127.0.0.1\:1433;DatabaseName=billuser=sapassword=</code>

修改mybatis-config.xml

<code><?xml version="1.0" encoding="UTF-8" ?>                </code>

修改这两个文件就基本配好了,接着用junit调试一下。

在AccountMapper.xml里,写入

<code><?xml version="1.0" encoding="UTF-8" ?>INSERT INTO b_account    (    userId,    password    )VALUES    (    #{userId},    #{password}    )</code>

并建立相应的pojo和数据库表。

在Test.java里写以下调试代码:

<code>package net.test.modules; import javax.annotation.Resource;import net.bill.modules.dao.AccountMapper;import net.bill.modules.pojo.Account;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="/applicationContext.xml")public class BaseTest implements ApplicationContextAware{    public ApplicationContext ctxt;    public void setApplicationContext(ApplicationContext arg0)            throws BeansException {        this.ctxt = arg0;    }    @Resource    private AccountMapper accountMapper;    @Test    public void testSql(){        accountMapper.insert(new Account("16", "haha"));    }}</code>

测试通过了。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用mybatis+SQLServer做持久层入门

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址