zoukankan      html  css  js  c++  java
  • MyBatis环境配置及入门

    1,新建Web Project ,添加两个jar包( mybatis-3.2.3.jar,mysql-connector-java-5.1.25-bin.jar )

    2,创建测试数据库,表

    -------------------------------------前期准备工作----------------------------------

    3,创建MyBatis配置文件:

      在 src/config 目录下建立Configure.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="User" type="com.yiibai.mybatis.models.User" /> </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://127.0.0.1:3306/yiibai" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <!-- // power by http://www.yiibai.com --> <mapper resource="com/yiibai/mybatis/models/User.xml" /> </mappers>   </configuration>

     4,创建实体类和映射文件

      

    package com.yiibai.mybatis.models;
    
    public class User {
    	private int id;
    	
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    }

    <?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="com.yiibai.mybatis.models.UserMapper">
    	<select id="GetUserByID" parameterType="int" resultType="User">
    		select * from `user` where id = #{id}
        </select>
    </mapper>

    5,备注:

    下面是对这几个配置文件一点解释说明:
    1、配置文件 Configure.xml 是 mybatis 用来建立 sessionFactory,里面主要包含了数据库连接相关内容,还有 java 类所对应的别名,比如:<typeAlias alias="User" type="com.yiibai.mybatis.models.User"/> 这个别名非常重要,在具体的类的映射中,比如:User.xml 中 resultType 就是对应这个。要保持一致,这里的 resultType 还有另外单独的定义方式,后面学习到我们再详细介绍说明。
    2、Configure.xml 里面 的<mapper resource="com/yiibai/mybatis/models/User.xml"/>是包含要映射的类的 xml 配置文件。
    3、在User.xml 文件里面主要是定义各种 SQL 语句,以及这些语句的参数,以及要返回的类型等等。

    6,测试类:

    import java.io.Reader;
    
    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.yiibai.mybatis.models.*;
    
    public class HelloWord {
    	private static SqlSessionFactory sqlSessionFactory;
    	private static Reader reader;
    
    	static {
    		try {
    			reader = Resources.getResourceAsReader("config/Configure.xml");
    			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	public static SqlSessionFactory getSession() {
    		return sqlSessionFactory;
    	}
    public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession(); try { User user = (User) session.selectOne( "com.yiibai.mybatis.models.UserMapper.GetUserByID", 1); if(user!=null){ String userInfo = "名字:"+user.getName()+", 所属部门:"+user.getDept()+", 主页:"+user.getWebsite(); System.out.println(userInfo); } } finally { session.close(); } } }
  • 相关阅读:
    LeetCode对撞指针汇总
    167. Two Sum II
    215. Kth Largest Element in an Array
    2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations
    题解 Educational Codeforces Round 84 (Rated for Div. 2) (CF1327)
    题解 JZPKIL
    题解 八省联考2018 / 九省联考2018
    题解 六省联考2017
    题解 Codeforces Round #621 (Div. 1 + Div. 2) (CF1307)
    题解Codeforces Round #620 (Div. 2)
  • 原文地址:https://www.cnblogs.com/yufeng1102/p/7535508.html
Copyright © 2011-2022 走看看