zoukankan      html  css  js  c++  java
  • mybatis学习笔记第一讲

    第一步:先配置mybatis配置

    <?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="Users" type="orm.Users"/> 
        </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/mybatis?characterEncoding=utf-8" />
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
        
        <mappers>
            <mapper resource="orm/Users.xml"/>
        </mappers>
    </configuration>
    

     第二步:创建一个实体对象

    package orm;
    
    public class Users {
    	private Integer id;
    	private String username;
    	private String password;
    	private Integer age;
    	private Integer sex;
    
    	public Users(Integer id, String username, String password, Integer age,
    			Integer sex) {
    		super();
    		this.id = id;
    		this.username = username;
    		this.password = password;
    		this.age = age;
    		this.sex = sex;
    	}
    	//此处必须添加一个无参构造
    	public Users() {
    		
    	}
    
    	//set和get省略
    	@Override
    	public String toString() {
    		// TODO Auto-generated method stub
    		return super.toString();
    	}
    
    }
    

     第三步:配置一个简单的查询参数

    <?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="orm.Users">
        <select id="selectUserByID"  parameterType="int" resultType="orm.Users">
            select * from Users where id = #{id}
        </select>
    </mapper>
    

     第四步:测试(简洁版)

    		 String resString="Configuration.xml"; 
    		 Reader reader = Resources.getResourceAsReader(resString);
    		 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    		 SqlSession session =  sessionFactory.openSession();
    		 String statement ="orm.Users.selectUserByID";
    		 Users users = (orm.Users) session.selectOne(statement,2);
    		 System.out.println(users.getUsername());
    

    第四步:测试(完成版)

    public class Test {
        private static SqlSessionFactory sqlSessionFactory;
        private static Reader reader; 
    
        static{
            try{
                reader    = Resources.getResourceAsReader("Configuration.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 {
            Users users = (Users) session.selectOne("orm.Users.selectUserByID", 2);
            System.out.println(users);
            } finally {
            session.close();
            }
        }
    }
    

      

  • 相关阅读:
    bug的生命周期
    性能测试的流程
    通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。&#160;利用因果图生成测试用例的基本步骤是:
    集成测试中自顶向下集成和自底向上集成两个策略的理解,要谈出它们各自的优缺点和主要适应于哪种类型测试;
    Spring中的八大设计模式
    ssh
    window实用快捷键-win篇
    解决Chrome浏览器“崩溃啦”的问题!
    C语言-格式输出
    AIDA64+RemotePanel 组副屏电脑状态监视器 旧手机废物利用 wifi/数据线 adb驱动
  • 原文地址:https://www.cnblogs.com/koal/p/4418018.html
Copyright © 2011-2022 走看看