首页 > 代码库 > Mybatis入门环境搭建

Mybatis入门环境搭建

1.依赖jar包

 

 

mybatis-3.2.2.jar  sqlserver2008.jar

2.代码

技术分享
package ttyouni.model;

public class Student {
    public int ID;
    public String UserName;
    public String Password;
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getUserName() {
        return UserName;
    }
    public void setUserName(String userName) {
        UserName = userName;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }
}
Model
技术分享
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
   -->
 <mapper namespace="ttyouni.model.StudentMapper">
      <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
      使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
     resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
     User类就是users表所对应的实体类
     -->
     <!-- 
         根据id查询得到一个user对象
      -->
     <select id="getStudent" parameterType="int" 
         resultType="ttyouni.model.Student">
         select * from Student where id=#{id}
     </select>
 </mapper>
mapper
技术分享
<?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="Student" type="ttyouni.model.Student"/> 
    </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:1433;DatabaseName=Qxun" />
            <property name="username" value="sa"/>
            <property name="password" value="6665508a"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="ttyouni/model/StudentMapper.xml"/>
    </mappers>
</configuration>
Configuration.xml
技术分享
package ttyouni.main;

import java.io.InputStream;

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

import ttyouni.model.Student;

public class AppMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //mybatis的配置文件
        String resource = "Configuration.xml";
        //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
        InputStream is = AppMain.class.getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
        //Reader reader = Resources.getResourceAsReader(resource); 
        //构建sqlSession的工厂
        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        /**
         * 映射sql的标识字符串,
         * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
         * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
         */
        String statement = "ttyouni.model.StudentMapper.getStudent";//映射sql的标识字符串
        //执行查询返回一个唯一user对象的sql
        Student student = session.selectOne(statement, 1);
        System.out.println(student.getUserName());
    }

}
main

3.文件结构

技术分享

 

Mybatis入门环境搭建