码迷,mamicode.com
首页 > 数据库 > 详细

java+maven+sqlserver使用mybatis

时间:2018-09-04 14:02:04      阅读:542      评论:0      收藏:0      [点我收藏+]

标签:mapper   oid   org   color   gen   static   分享图片   文章   source   

@参考文章,在此基础上略作修改

1,新建maven项目导入依赖

技术分享图片
<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
    </dependencies>
View Code

2,com.mybatis包下新建Yanan.java

技术分享图片
package com.mybatis;

public class Yanan {
    private int iid;
    private String ifullname;
    private String iloginname;
    private String ipassword;

    public Yanan() {
    }

    public Yanan(int id, String name) {
        this.iid = id;
        this.ifullname = name;
    }

    public String getIfullname() {
        return ifullname;
    }

    public int getIid() {
        return iid;
    }

    public void setIid(int iid) {
        this.iid = iid;
    }

    public void setIfullname(String ifullname) {
        this.ifullname = ifullname;
    }

    public String getIloginname() {
        return iloginname;
    }

    public void setIloginname(String iloginname) {
        this.iloginname = iloginname;
    }

    public String getIpassword() {
        return ipassword;
    }

    public void setIpassword(String ipassword) {
        this.ipassword = ipassword;
    }
}
View Code

3,根目录下新建Yanan.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">    
    
<mapper namespace="Yanan">    
    <select id="selectUser" parameterType="int" resultType="Yanan">    
        SELECT * FROM Yanan WHERE id = #{id}    
    </select>    
        
    <select id="selectUsers" resultType="Yanan">    
        SELECT * FROM Yanan    
    </select>    
        
</mapper>
View Code

4,根目录下新建configuration.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">    
<configuration>    
    <typeAliases>    
        <typeAlias alias="Yanan" type="com.mybatis.Yanan"></typeAlias>    
    </typeAliases>    
        
    <environments default="development">    
        <environment id="development">    
            <transactionManager type="JDBC"></transactionManager>    
            <dataSource type="POOLED">    
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />    
                <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=test" />    
                <property name="username" value="sa" />    
                <property name="password" value="yanan" />    
            </dataSource>    
        </environment>    
    </environments>    
        
    <mappers>    
        <mapper resource="Yanan.xml"/>    
    </mappers>    
    
</configuration>
View Code

5.com.controller包下新建测试文件

技术分享图片
package com.controller;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.Yanan;

public class Test {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String resource = "configuration.xml";    
        Reader reader = Resources.getResourceAsReader(resource);    
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);    
        SqlSession session = ssf.openSession();    
        try{    
            Yanan user = session.selectOne("selectUser", 1);    
            System.out.println(user.getIfullname());    
            System.out.println(user);    
            System.out.println("--------------分隔线---------------");    
            
            List<Yanan> users = session.selectList("selectUsers");    
            for(int i=0; i<users.size(); i++) {    
                System.out.println(users.get(i).getIfullname());    
            }    
                
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {    
            session.close();    
        }    
    }

}
View Code

源码包下载:JavaMybatis.rar

项目结构:

技术分享图片

 

java+maven+sqlserver使用mybatis

标签:mapper   oid   org   color   gen   static   分享图片   文章   source   

原文地址:https://www.cnblogs.com/yanan7890/p/9583622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!