Getting started with persistence layer using mybatis + SQLServer

Source: Internet
Author: User
This article describes how to use mybatis to connect to the SQLServer database. 1. Download the free version of SQLServer2014Express in www. microsoft. comen-usserver-cloudproductssql-server-editionssql-server-express.aspx for learning. Then, install and configure the default Administrator sa password. 2

This article describes how to use mybatis to connect to the SQLServer database. 1. Download SQL Server 2014 Express free edition in http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx for learning. Then, install and configure the default Administrator sa password. 2

This article describes how to use mybatis to connect to the SQLServer database.

1. Download SQL Server 2014 Express free edition in http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/sql-server-express.aspx for learning.

Then, install and configure the default Administrator sa password.

2. in SQL Server Configuration Manager, enable TCP/IP and restart SQLServer. Then we canUseSome database clients try to connect to the database.

3. In http://www.microsoft.com/zh-CN/download/details.aspx? Id = 11774 download the jdbc driver of SQLServer. There are simple examples and three versions of jar packages.

4. Now I have a spring + springmvc + mybatis project.UseThe database is mysql. Now I want to change to SQLServer.
First, I will introduce one of the jdbc. jar packages into the project. Here IUseIs sqljdbc4.jar.
(In debugging
Cannot load JDBC driver class 'com. microsoft. sqlserver. jdbc. SQLServerDriver'
Or Cannot create PoolableConnectionFactory (this server version is not supported. The target Server must be SQL Server 2000 or later.
And so on, it may beUseJar package version is incorrect)

Then modify jdbc. properties:

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

Modify mybatis-config.xml


  
      
           
        
   
  

Modify these two files and configure them. Then debug them with junit.

In AccountMapper. xml, write


  
  
   
    INSERT INTO b_account    (    userId,    password    )VALUES    (    #{userId},    #{password}    )
   
  

Create pojo and database tables.

Write the following debugging code in Test. java:

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

The test is passed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.